2012-08-13 123 views
0

我已经构建了一个简单的表单,我想在不刷新页面的情况下发送,因此我使用jQuery。提交表单时出现问题,但process.php报告数据未转换,并且process.php输出消息“no post received”。 这里的表单文件的代码:PHP jQuery表单提交

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"> 
<title>Insert title here</title> 
</head> 
<script type="text/javascript"> 
$(function(){ 
     $('#submit').click(function(){ 
      var getField = $("#field").val(); 
      $.ajax({ 
      url: 'process.php' , 
      type: 'POST', 
      data: 'data: ' + getField, 
      success: function(result){  
       $('#container').text('<p>' + result + '</p>')  
      } 
      }); 
      return false;  
     }); 
    }); 
</script> 

<body> 

<div id="container"> 
<form method="post" action="process.php"> 
<input name="field" id="field" type="text" /> 
<input type="submit" value="submit" id="submit" /> 
</form> 
</div> 

</body> 
</html> 

这里的process.php代码:

<?php 
if($_POST){ 
    echo $_POST['field']; 
} 
else 
    echo "no post received"; 
?> 

你有什么想法,有什么错了jQuery /形式? 欢迎任何建议, 谢谢

回答

0
data: 'data: ' + getField 

这是张贴getfield命令的值process.php“数据”,而不是“场”,所以你需要得到的$ _POST值[“数据”],尽管变量命名“数据”通常不是一个好主意。你可能也想看看jQuery post函数,这个函数就是你正在做的。