Note that there are some explanatory texts on larger screens.

plurals
  1. POForm posts data twice
    text
    copied!<p>I have a feedback form in a pop-up div that otherwise works fine but processes SQL twice when the form results in error at first instance.</p> <p>This is the html form:</p> <pre><code>&lt;div id="stylized" class="myform"&gt; &lt;form id="form" method="post" name="form"&gt; &lt;p&gt;Report: &lt;select id="fbtype" name="fbtype"&gt; &lt;option&gt;Bug&lt;/option&gt; &lt;option&gt;Suggestion&lt;/option&gt; &lt;option&gt;Discontentment&lt;/option&gt; &lt;option&gt;Appreciation&lt;/option&gt; &lt;/select&gt; &lt;/p&gt; &lt;p&gt;Brief description: &lt;textarea name="comments" id="comments" cols="45" rows="10"&gt;&lt;/textarea&gt; &lt;/p&gt; &lt;span class="error" style="display:none"&gt;Please describe your feedback.&lt;/span&gt; &lt;span class="success" style="display:none"&gt;We would like to thank you for your valuable input.&lt;/span&gt; &lt;input type="button" value="Submit" class="submit" onclick="feedback_form_submit()"/&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>The feedback_form_submit() function is:</p> <pre><code>function feedback_form_submit() { $(function() { $(".submit").click(function() { var fbtype = $("#fbtype").val(); var comments = $("#comments").val(); var dataString = 'fbtype='+ fbtype + '&amp;comments=' + comments; if(fbtype=='' || comments=='' ) { $('.success').fadeOut(200).hide(); $('.error').fadeOut(200).show(); } else { $.ajax({ type: "POST", url: "processfeedback.php", data: dataString, success: function(){ $('.success').fadeIn(200).show(); $('.error').fadeOut(200).hide(); } }); } return false; }); }); </code></pre> <p>}</p> <p>And the processfeedback.php has:</p> <pre><code>include "./include/session_check.php"; include_once "./include/connect_to_mysql.php"; if (isset($_POST['fbtype'])){ $userid =$_SESSION['id']; $fbtype=$_POST['fbtype']; $comments=$_POST['comments']; $sql = mysql_query("INSERT INTO feedback (userid, type, comments) VALUES('$userid','$fbtype','$comments')") or die (mysql_error()); } </code></pre> <p>Could anyone figure out why does the form submits twice? And any suggestion to control this behaviour?</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