Reputation: 697
I have a form; I am using jQuery to post this form, but now I wonder how I can "reset" the form when I posted done?
My jQuery and PHP code:
<?php
chdir('../');
include("_mysql.php");
include("_settings.php");
include("_functions.php");
chdir('admin');
if (isset($_POST['rubric'])) {
if (empty($_POST['rubric']) && empty($_POST['content'])) {
echo '<div class="alert alert-error alert-box">Du måste fylla i alla fält.</div>';
}
else {
$rubric = $_POST['rubric'];
$sql = "INSERT INTO ".PREFIX."news(`date`, `poster`)
VALUES('".time()."', '".$userID."')";
mysql_query($sql);
$id = mysql_insert_id();
$sql2 = "INSERT INTO ".PREFIX."news_contents(`newsID`, `headline`, `content`)
VALUES('".$id."', '".$rubric."', '".$_POST['content']."')";
mysql_query($sql2);
echo '<div class="alert alert-box alert-success">Klart, nyheten postad.</div>';
}
}
?>
$("#form").submit(function(event) {
event.preventDefault();
var poster = $.post('addnews.php', $(this).serialize());
poster.done(function(data) {
$(".result").html(data);
});
});
Does anyone know how I can do this? Because now when I post a new the rubric and content is still there, and I dont really know how to unset the variables.
And I really still want my error, and accept message left if possible.
Upvotes: 1
Views: 5404
Reputation: 11475
You can use form reset() method like
document.getElementById("form").reset();
or in jQuery
$('#form').find('input:text, input:password, input:file, select, textarea').val('');
$('#form').find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
Upvotes: 0
Reputation: 1075427
HTMLFormElement
s have a reset
function, so:
$("#form").submit(function(event) {
var form = this; // <=== Remember the form element
event.preventDefault();
var poster = $.post('addnews.php', $(this).serialize());
poster.done(function(data) {
$(".result").html(data);
form.reset(); // <=== Use `reset`
});
});
Upvotes: 5