Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Unique ID to AJAX Script
    primarykey
    data
    text
    <p>I'm working on my first AJAX script with some PHP pages - it's my first time with AJAX and I've finally got the script to work. I'm also a bit of a Javascript newbie too.</p> <p>I have a PHP website that allows users to search a library catalog and then select/add books to a shopping cart. We've now changed the "Select" link to load via AJAX so the search results page doesn't refresh.</p> <p>To finish the AJAX changes I now need to pass a unique ID for each of the table cells that has the AJAX link to the script. I'm using PHP to generate a unique ID for each table cell by using the $bookID variable as follows:</p> <pre><code>&lt;td class="hidden-narrow" id="&lt;?php echo 'selectRecord'.$bookID; ?&gt;"&gt; &lt;?php if (in_array($bookID, $_SESSION['selectedBooks'])) { echo "Selected"; } else { echo '&lt;a href="select" onclick="return selectRecord('.$bookID.')" class="center"&gt;Select&lt;/a&gt;'; } ?&gt; &lt;/td&gt; </code></pre> <p>I now need to update my script to work with the unique ID's - to get it working I hardcoded it to an ID named "selectRecord" using an example script that I found. Here's my script:</p> <pre><code>function selectRecord(id) { // Allocate an XMLHttpRequest object if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari var xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // Set up the readyState change event handler xmlhttp.onreadystatechange = function() { if ((this.readyState == 4) &amp;&amp; (this.status == 200)) { document.getElementById("selectRecord").innerHTML="Selected"; } } // Open an asynchronous POST connection and send request xmlhttp.open("POST", "selectRecord.php", true); xmlhttp.send("id="+id); return false; // Do not follow hyperlink </code></pre> <p>I gather I need to change this line:</p> <pre><code>document.getElementById("selectRecord").innerHTML="Selected"; </code></pre> <p>but not sure of the syntax to handle unique ID's for each table row cell.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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