2011-11-24 147 views
0

是否可以在不刷新页面的情况下提交此表单。我将如何去设置一个可以做到这一点的jQuery代码。提交表单不刷新页面

<?php 
    if(!isset($_POST['sthis'])) 
    { 
?> 
<div id="show"> 
<form method="post" action=""> 
something<input name="sthis" type="text" id="sthis" /> 
<input name="s" type="submit" value="submit" id="submit" /> 
</form> 
</div> 
<?php 
    } 
    else 
    { 
    if(isset($_POST['sthis'])) 
    $sthis = $_POST['sthis']; 
    if(empty($sthis)){echo 'put something in this box';} 
    else echo 'ready'; 
    } 

?> 

回答

1

您需要AJAX为:

 
$.ajax({ 
    type: "POST", 
    url: "your url here", 
    data: "sthis=someValue", 
    success: function(response) { 
    alert(response); 
    } 
}); 

希望它可以帮助

+0

我会试试这个谢谢 – user836910

+0

我也喜欢使用AJAX,但我不确定这个片段是否提供足够的内容来拦截提交按钮,序列化表单中的数据,并向请求发送服务器。 –

1

这是如何使用jQuery:

HTML

<form id="frmSample"> 
... 
</form> 

<div id="divResult"></div> 

JQuery的

$(function(){ 
    $("#frmSample").submit(function(){ 
    //some code here 

    $("#divResult").load("yourPhpCode.php", {variableName: value, variableName1: value}); 
    }); 
}); 

退房the documentation

+0

我会试试这个谢谢 – user836910