Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto refresh table whenever a form is submitted via ajax
    text
    copied!<p>I want to create an auto refresh table whenever I submitted the form below using jQuery Ajax.</p> <p><strong>Here is my form</strong></p> <pre><code>&lt;form id="submitDataToElection"&gt; &lt;table border="0"&gt; &lt;tr&gt; &lt;td&gt;&lt;label for="electionName"&gt;Election Name:&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="electionName" id="electionName" spellcheck="true" required&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;label for="electionDate"&gt;Election Date:&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="electionDate" id="electionDate" required&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;input name="Reset" type="reset" value="Reset" id="resetInAddElection"&gt;&lt;input type="submit" value="Submit" id="submitInAddElection"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p><strong>The Form's jQuery</strong></p> <pre><code>$("#submitDataToElection").submit(function(event) { $("#resetInAddElection, #submitInAddElection").attr("disabled",true); event.preventDefault(); var values = $(this).serialize(); $.ajax({ url: "storeDataToElection.php", type: "post", data: values, success: function(data){ $('#addElection').dialog('close'); alert(data); $("#resetInAddElection, #submitInAddElection").attr("disabled",false); $("#electionName,#electionDate").val(""); $("#electionName").focus(); }, error:function(data){ $("#resetInAddElection, #submitInAddElection").attr("disabled",false); alert("failure"); } }); }); </code></pre> <p><strong>here is the code for storeDataToElection.php</strong></p> <pre><code>mysql_connect("localhost", "root", ""); mysql_select_db("automated_election"); $name = $_POST['electionName']; $date = $_POST['electionDate']; mysql_query("UPDATE election SET is_active = 'no' WHERE is_active = 'yes'")or die(mysql_error()); $insert = mysql_query("INSERT INTO election (election_name , election_date , is_active) VALUES('$name', '$date', 'yes')"); if(!$insert) die(mysql_error()); else die("Success"); </code></pre> <p><strong>Here is the table I want to auto-refresh</strong></p> <pre><code>$result = mysql_query("SELECT * FROM election WHERE is_active = 'yes'"); echo '&lt;hr&gt;&lt;table style="font-size:14px;" id="viewElectionTable"&gt;'; while ($data = mysql_fetch_assoc($result)) { $date = explode("-",$data['election_date']); switch($date[1]){ case 1: $month = "January"; break; case 2: $month = "February"; break; case 3: $month = "March"; break; case 4: $month = "April"; break; case 5: $month = "May"; break; case 6: $month = "June"; break; case 7: $month = "July"; break; case 8: $month = "August"; break; case 9: $month = "September"; break; case 10: $month = "October"; break; case 11: $month = "November"; break; case 12: $month = "December"; break; } echo '&lt;tr&gt;'; echo "&lt;td&gt;Election Name: &amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;".$data['election_name'].'&lt;/td&gt;'; echo '&lt;/tr&gt;&lt;tr&gt;'; echo "&lt;td&gt;Election Date: &amp;nbsp;&lt;/td&gt;&lt;td&gt;".$month." ".$date[2].", ".$date[0].'&lt;/td&gt;'; echo '&lt;/tr&gt;'; } echo '&lt;/table&gt;&lt;hr&gt;'; </code></pre>
 

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