Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax Listen Event on a Button Click then Runs a Php file
    primarykey
    data
    text
    <p>I need the AJAX to listen to if a button is click. Then if it is I need it to run a PHP script. I am having troubles by the AJAX not correctly listening to the Button Click and so it never runs the script.<br /> Any Error in my code you may see?<br /> Any advice on how I should do this?</p> <p>The Button:</p> <pre><code>&lt;input id="button_1" type="button" value="favorites1" onclick="favfunct();" /&gt; </code></pre> <p>The AJAX that calls it is: (<code>ajaxlisten.js</code>)</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { // Make sure the elements are loaded on the page // Listen for a click event on the button $('#button_1').click(favfunct); }); function favfunct(e) { // Stop the page from "following" the button (ie. submitting the form) e.preventDefault(); e.stopPropagation(); // Call an AJAX function to the proper page $.ajax("js/addtofavorites.php", { // Pass our data to the server data: { "get" : "runfunction", "action" : "favorites1" }, // Pass using the appropriate method method: "POST", // When the request is completed and successful, run this code. success: function (response) { // Successfully added to favorites. JS code goes here for this condition. alert ("successfully loaded") } }); } &lt;/script&gt; </code></pre> <p>Php File (<code>addtofavorites.php</code>)</p> <pre><code>&lt;?php $con = mysql_connect("localhost","root","student"); if ($_POST["action"] = 'favorites1') { if (!$con); { die('Could not connect: ' . mysql_error()); } mysql_select_db("tvid", $con); $sql="INSERT INTO tv (userid, favorites) VALUES (345,77);" if (!mysql_query($sql,$con)); { die('Error: ' . mysql_error()); } echo "Your Video was Added To Your Favorites"; mysql_close($con); } ?&gt; </code></pre>
    singulars
    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.
 

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