Note that there are some explanatory texts on larger screens.

plurals
  1. POget an id from several item to use them in an AJAX request
    text
    copied!<p>Let me explain you my issue. </p> <p>I have a db with several items in it. I did a while loop to display a hidden input with the id of each item as a value. I'd like to get each id of each item to use them in an AJAX request to check something in an other db table.</p> <p>Here's my while loop: </p> <pre><code>$reqfollow = db_query('SELECT following_id FROM following WHERE follower_id = ?', array($_SESSION['mc']['id'])); while($follow = $reqfollow-&gt;fetch()) { $reqitems = db_query('SELECT * FROM items WHERE user_id = ? ORDER BY date DESC', array($follow['following_id'])); while($items = $reqitems-&gt;fetch()) { ?&gt; &lt;input type="hidden" class="id" value="&lt;?php echo $items['id']; ?&gt;"&gt; //this is where i have the id of each item &lt;?php } ?&gt; </code></pre> <p>I have some jQuery to get those ids: </p> <pre><code>function checkingfetchresult(userid){ $.post("ajax/checkingfetchresult.php", { userid: userid }, //first of all, I check how many item have been displayed, here it is eaqual to 3 function(check){ console.log(check); //So 3 here for (var i = 0; i&lt;check; i++){ var id = $(".id").val(); //I try to get the id of each but I only get the first one console.log(id); //Only the first one } }); } </code></pre> <p>But as it is said in my code, I only get the id of the first item. Maybe I should try to do it with an array but if I do it, how will I be able to only get one id at the time of the array to do my other AJAX request which is: </p> <pre><code>function checkinglikebutton(id){ $.post("ajax/checkinglikebutton.php", { id: id }, function(check){ if (check == 1){ //show the Like button $("#dislike").hide(); } else if (check == 0){ //show the Dislike button $("#like").hide(); } else{ alert('An error has occured'); } }); } </code></pre> <p>I really need help because I'm a bit lost in that.</p> <p>Here's how I can do it with an array:</p> <pre><code>function checkingfetchresult(userid){ $.post("ajax/checkingfetchresult.php", { userid: userid }, function(check){ console.log(check); var id = []; $(".id").each(function(){ id.push($(this).val()); }); console.log(id); }); } </code></pre> <p>But I have 3 times the same array :/</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload