Note that there are some explanatory texts on larger screens.

plurals
  1. POForm submit using AJAX is not working
    text
    copied!<p>PHP/Ajax newbie here...I am trying to save the contents of a textarea into MySQL via Ajax. Although the data IS being saved correctly, Ajax isn't quite working. Basically, the page is "reloaded/refreshed" after the data is saved, unlike Ajax. Can you please tell me what is that I am doing wrong?</p> <p>index.html:</p> <pre><code>&lt;form action="save.php" method="post" id="source-form"&gt; &lt;span&gt;&lt;input type="submit" value="Save" /&gt;&lt;/span&gt; &lt;div&gt; &lt;textarea id="editor" name="editor" &gt; &lt;/textarea&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>javascript:</p> <pre><code> $(document).ready(function() { $("#source-form").submit(function(){ $.ajax({ url:"save.php", type:"post", data:$(this).serialize(), success: alert('saved'); }); }); </code></pre> <p>save.php</p> <pre><code>&lt;?php // connect to the database include('connect-db.php'); // get form data, making sure it is valid $submit_date = date('Y-m-d H:i:s'); $content = mysql_real_escape_string(htmlspecialchars($_POST['editor'])); //build query mysql_query("INSERT source SET submit_date='$submit_date',content='$content'") or die(mysql_error()); header('Location: index.html'); ?&gt; </code></pre> <p>Any help on this is appreciated. Thank you.</p> <p><strong>EDIT:</strong> For folks running into the same issue or something similar...here's a great solution from: <a href="http://jquery.malsup.com/form/#getting-started" rel="nofollow">http://jquery.malsup.com/form/#getting-started</a></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