I have a jquery/ajax form that has event.preventDefault(). The problem is that my input field doesn’t go empty once I press enter. Usually when you press enter in an input field, it will do the ‘action’ in the form and the input field empties. But now when I type something, my form doesn’t reset to empty when I press enter. I know that event.preventDefault() doesn’t do the normal “action” in the form, but I’m trying to not refresh my page and just use ajax so it will load without page refresh.
——————–jquery——————– (below)
$('[placeholder]').parents('form').submit(function(event) {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
});
var input = $('input.sel').val();
dataList = 'list=' + input;
$.ajax({
type: "POST",
url: "ajax_table_edit.php",
data: dataList,
cache: false,
success: function(html)
{
$('.selector').html(data);
}
});
event.preventDefault();
});
—————ajax_table_edit.php———– (below)
if(isset($_POST['list'])){
$list = $_POST['list'];
$id = $_POST['id'];
$id = ereg_replace("[^0-9]", "", $id);
$sql = mysql_query("INSERT INTO table (column) VALUES ('".$list."')");
echo "success";
}
At the same time. I’m trying to echo out success, but that’s not working either. Can someone help me seek the solution to this problem?
Thank you for your time!
You’ll have to blank the fields yourself with
jQuery: $("#field").val='';And its not outputting your message, because you’re not grabbing the right variable on success.. you define it as html which may be reserved anyways… try calling it data instead because that is what you’re using inside the function anyways.
Also, why do you even have that
$idcode in the php side? looks like it doesn’t even get used. And, just because its ajax doesn’t mean you’re safe from SQL injection, you need to sanitize that$_POST['list']variable.