Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, you could use the callback you get from your backend file. Try this instead.</p> <p><strong>EDIT:</strong> As Pendo mention below, you should use JSON when returning data from <code>2.php</code>. </p> <p><strong>Edited version</strong></p> <p><strong>Index.php</strong></p> <pre><code>&lt;script type="text/javascript"&gt; $(function(){ // EDIT $('#comment').hide(); $("#submit").click(function(){ var formvalue = $("#content").val(); $.ajax({ type: "POST", url: "2.php", data: 'content='+formvalue, dataType: 'json', // EDIT set dataType to json success: function(ret){ $("#content").val(''); if(ret['success']) { //Fade in $('#comment').html('Insert Success!').fadeIn(1000); //Fade Out setTimeout(function(){ $('#comment').html('').fadeOut(1000); },1500); }else{ // Fade in $('#comment').html('Insert Failed!').fadeIn(1000); // Fade out setTimeout(function(){ $('#comment').html('').fadeOut(1000); },1500); } } }); }); }); </code></pre> <p></p> <pre><code>&lt;textarea name="content" id="content" cols="30" rows="3"&gt;&lt;/textarea&gt; &lt;br /&gt; &lt;input type="button" id="submit" name="submit" value="Add comment" /&gt; &lt;div id="comment"&gt;&lt;/div&gt; </code></pre> <p><strong>2.php</strong></p> <pre><code>&lt;?php $connection = mysql_connect('localhost', 'root' , 'root') or die(mysql_error()); $selection = mysql_select_db('my_content', $connection); $content = $_POST['content']; /* Set return data to false as default */ $json_ret = array('success'=&gt;false); $sql = "INSERT INTO msg (content) VALUES ('".$content."')"; if(mysql_query($sql)) { /* insert success.. add true */ $json_ret['success'] = true; } echo json_encode($json_ret); ?&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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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