John Davidson

php - How to execute AJAX while loading a table

0 comments
Message:


I'm crating a table with a foreach for a PHP variable and I want every time the foreach cycle executes the AJAX gets loaded. There's my code:


@foreach ($themes as $theme)
<tr>
<td> <img alt="Image placeholder" src="{{$theme->themeimg}}" width="30" height="30"> {{$theme->name}} </td>
<td> <input name='nCertificates' type="text" class="form-control" placeholder="" id='nCertificates' disabled> </td>
</tr>
@endforeach




function getThemeData(id) {

$.ajax({
type: 'GET',
url: '/admin/themes/ntests/'+id,
data: {'_token': $("input[name='_token']").val()},
success: function (data) {

console.log(data);

$('#nCertificates').attr('placeholder', data).val(data);

}
});

}


My Answer:
To execute the AJAX call for each iteration of the foreach loop, you can add a data attribute to each row in the table that contains the theme id. Then, you can call the getThemeData function with the theme id as a parameter when the page is loaded.

Here's an updated version of your code:

@foreach ($themes as $theme)

Image placeholder {{$theme->name}


@endforeach



In this code, the getThemeData function is called for each row in the table when the page is loaded. The theme id is passed as a parameter to the function, and the AJAX call is made to retrieve the data for that specific theme. The data is then displayed in the input field for that row.

Rate this post

4 of 5 based on 8921 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us