Php 提交表单时不刷新页面
我正在尝试在不刷新页面的情况下提交此表单。我正在使用jquery;提交后,页面仍会刷新Php 提交表单时不刷新页面,php,jquery,Php,Jquery,我正在尝试在不刷新页面的情况下提交此表单。我正在使用jquery;提交后,页面仍会刷新 $(function(){ $('#submit').click(function(){ $('#container').append('loading'); var sthis = $('#sthis').val(); $.ajax({ url: 'f.php' , type: 'POST', data: 'sthi
$(function(){
$('#submit').click(function(){
$('#container').append('loading');
var sthis = $('#sthis').val();
$.ajax({
url: 'f.php' ,
type: 'POST',
data: 'sthis: ' + sthis,
success: function(result){
$('#container').append('<p>' + result + '</p>')
}
});
return false;
});
});
$(函数(){
$(“#提交”)。单击(函数(){
$('#container')。追加('loading');
var sthis=$('#sthis').val();
$.ajax({
url:'f.php',
键入:“POST”,
数据:'sthis:'+sthis,
成功:函数(结果){
$(“#容器”).append(“”+result+””)
}
});
返回false;
});
});
html页面(f.php)
某物
php页面
<?php
if(isset($_POST['sthis'])){
$sthis = $_POST['sthis'];
if(empty($sthis)) { echo 'put something in this box'; }
else echo 'ready';
}
?>
更改此选项:
<input type="submit" value="submit" id="#submit" />
为此:
<input type="button" value="submit" id="#submit" />
然后连接一个调用ajax函数的事件处理程序,而不是提交表单。在这种情况下,最简单的方法是更改表单标记,如下所示:
<form method="post" action="f.php" onSubmit="return false;">
在表单标记中,只需添加一个
onsubmit=“return false”
。这将阻止表单提交请求并刷新页面。该按钮上的单击事件仍然有效,因此您可以通过javascript控制一切。以下是更多信息:
你的问题就在这里
id=“#提交”应该是id=“提交”,应该可以 还是有问题 第一:在f.php中:
<input name="sthis" type="text" />
将其更改为:
data: {sthis: sthis},
问题是,如果用户在输入框中点击“回车”,他们仍然会触发表单提交事件。同意,如果表单提交,页面将刷新,这可能是由其他原因造成的,但在文本字段中输入时,表单不会做任何事,即使字段为空,但我这样做时表单不会做任何事,即使这片土地是空的,也一定会有别的事情发生。它不应该对jQueryAjax代码有任何影响。您是否收到JS错误?由于action=“f.php”,这不会在收到ajax响应后重新加载f.php页面吗?
<input id="sthis" type="text" />
data: 'sthis: ' + sthis,
data: {sthis: sthis},