Note that there are some explanatory texts on larger screens.

plurals
  1. POajax doesn't load my php file in the background
    primarykey
    data
    text
    <p>I'm having a problem with my ajax call. I'm submitting some info via php to mySQL, the submission part works perfectly, it's adding the data to the database, but the ajax function isn't loading that php in the background, it's loading it in the window and showing the php file results.</p> <p>Here's the HTML code.</p> <pre><code>&lt;form action="upload.php" class="addItem" method="post"&gt; Firstname:&lt;br&gt;&lt;input type="text" class="firstname" name="firstname" /&gt;&lt;br&gt; Lastname:&lt;br&gt;&lt;input type="text" class="lastname" name="lastname" /&gt;&lt;br&gt; Age:&lt;br&gt;&lt;input type="text" class="age" name="age" /&gt;&lt;br&gt; Photo:&lt;br&gt;&lt;input type="file" name="image" accept="image/jpeg" /&gt;&lt;br&gt;&lt;br&gt; &lt;input type="submit" class="submitItem" value="Add Row" /&gt; &lt;/form&gt; &lt;a href="logout.php"&gt;Logout&lt;/a&gt; &lt;/div&gt; &lt;script&gt; $(document).ready(function(){ $(".submitItem").click(function(){ // Start AJAX send jQuery.ajax({ // Enter below the full path to your "send" php file url: "upload.php", type: "POST", data: data, cache: false, success: function (html) { // If form submission is successful if ( html == 1 ) { $('.successMessage').show(200); $('.successMessage').delay(2000).hide(); } // Double check if maths question is correct else { $('.errorMessage').show(200); $('.errorMessage').delay(2000).hide(); } } }); }); }); &lt;/script&gt; </code></pre> <p>Here's the PHP code</p> <pre><code>&lt;?php $name = $_POST['firstname']; $surname = $_POST['lastname']; $age = $_POST['age']; if(($name === "") || ($surname === "") || ($age === "")){ echo "please fill in all fields"; } else { $con = mysql_connect("localhost","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } if ($sql) { echo "1"; } else{echo "error";} } mysql_close($con); ?&gt; </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