Reputation: 395
I want to create an auto refresh table whenever I submitted the form below using jQuery Ajax.
Here is my form
<form id="submitDataToElection">
<table border="0">
<tr>
<td><label for="electionName">Election Name:</label></td>
<td><input type="text" name="electionName" id="electionName" spellcheck="true" required></td>
</tr>
<tr>
<td><label for="electionDate">Election Date:</label></td>
<td><input type="text" name="electionDate" id="electionDate" required></td>
</tr>
<tr>
<td> </td>
<td><input name="Reset" type="reset" value="Reset" id="resetInAddElection"><input type="submit" value="Submit" id="submitInAddElection"></td>
</tr>
</table>
</form>
The Form's jQuery
$("#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");
}
});
});
here is the code for storeDataToElection.php
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");
Here is the table I want to auto-refresh
$result = mysql_query("SELECT * FROM election WHERE is_active = 'yes'");
echo '<hr><table style="font-size:14px;" id="viewElectionTable">';
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 '<tr>';
echo "<td>Election Name: </td><td>".$data['election_name'].'</td>';
echo '</tr><tr>';
echo "<td>Election Date: </td><td>".$month." ".$date[2].", ".$date[0].'</td>';
echo '</tr>';
}
echo '</table><hr>';
Upvotes: 0
Views: 2151
Reputation: 29168
The general idea is to use the file your ajax hits to output the new table HTML. Then use the HTML output returned to your ajax call to replace the existing table on your page.
For example:
Put the table you want to refresh inside a div and give that div an id to reference. In my example below, I'm using
<div id="tableHolder"><!-- TABLE GOES HERE --></div>
In storeDataToElection.php, after you write to your database, output your new table HTML. This HTML output will be returned to your ajax call's "success" function.
Then, you can replace the table with the new content by doing something like this:
success : function(updatedTable) {
...
$('div#tableHolder').html(updatedTable);
...
}
Upvotes: 1