Note that there are some explanatory texts on larger screens.

plurals
  1. POmultiple forms on page onclick ajax function not working
    primarykey
    data
    text
    <p>I have an ajax search function that produces a list of names from database. Each name is echoed back as a form button so when user clicks on the name another ajax call will bring up all info related to that name. However, it is not working. I have tried several variations of the ajax function below but either nothing happens at all or the page just gets refreshed with no results. Any ideas on how to get this to work?</p> <p>This is the latest ajax (which does nothing)</p> <pre><code>$(function GetInfo() { $('form').on('click', function (e) { var tourName = $('#tourName').val(); var FirstName = $('#FirstName').val(); var LastName = $('#LastName').val(); alert("PLEASE ENTER A NAME" + FirstName + LastName); $.ajax({ type: "POST", url: 'process.php', data: "tourName=" + tourName + "&amp;firstname=" + firstname + "&amp;lastname=" + lastname, success: function(data){ $("#search_results").html(data); } }); e.preventDefault(); }); }); </code></pre> <p>And this is the php loop that produces the forms (names):</p> <pre><code>$string = ''; if (mysql_num_rows($query)){ while($row = mysql_fetch_assoc($query)){ $FirstName = $row['FirstName']; $LastName = $row['LastName']; $Name = $row['FirstName']." ".$row['LastName']; $string .= "&lt;form method='post' action=''&gt; &lt;input type='hidden' name='FirstName' value='$FirstName'&gt; &lt;input type='hidden' name='LastName' value='$LastName'&gt; &lt;input type='button' class='button' name='person_name' value='$Name' onClick='GetInfo()'&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;\n"; } }else{ $string = "No matches found!"; } mysql_close($con); echo $string; </code></pre> <p>Just incase anyone has the same issue, I got the following code to work:</p> <pre><code> function GetInfo(form) { var person_name = form.person_name.value; var tourName = form.tourName.value; var firstname = form.FName.value; var lastname = form.LName.value; $.ajax({ type: "POST", url: "process.php", data: "person_name=" + person_name + "&amp;tourName=" + tourName + "&amp;firstname=" + firstname + "&amp;lastname=" + lastname, success: function(data){ $("#search_results").html(data); } }); return false; } </code></pre> <p>And in the form </p> <pre><code>$string .= "&lt;form method='post' id='$form'&gt; &lt;input type='hidden' name='tourName' value='$tourneyName'&gt; &lt;input type='hidden' name='FName' value='$FirstName'&gt; &lt;input type='hidden' name='LName' value='$LastName'&gt; &lt;input type='button' class='button' name='person_name' value='$Name' onClick='GetInfo (this.form)'&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;\n"; } }else{ $string = "No matches found!"; } </code></pre>
    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