php和ajax传递表单值

时间:2013-01-03 23:19:02

标签: php ajax

您好我如何从下面的代码中回显PHP文件中的表单输入“date”。 (data: "name=Peter&location=Sheffield" + $('input[name="date"]').val(),

目前我有echo "todays date ".$_POST['date']."<br>";,但它似乎无法正常工作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript"
    src=http://code.jquery.com/jquery-latest.js>    
</script>
<script type="text/javascript">
// This functions starts the ajax to show the output from post.php
function StartAjax(ResultsId){
    $.ajax({
      type: "POST",
      url: "postTest.php",
      cache: false,
      data: "name=Peter&location=Sheffield" + $('input[name="date"]').val(),
      success: function(html, status){
        $("#"+ResultsId).append(html);
        $('#status').append(status);
      } 
    });
}
</script>
</head>
<body>
<h1> Run Club</h1>
testing
<form>
date: <input type="text" name="date"><br>
</form>
<a href="#" onclick="StartAjax('ResultsId');">Click Here to see updates from postTest.php</a>
<div id="ResultsId"></div>
<div id="status"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您必须在查询字符串

中添加日期键
data: "name=Peter&location=Sheffield&date=" + $('input[name="date"]').val(),

或者您应该将对象传递给数据,以便jQuery为您格式化查询字符串。

data: {name: "Peter", location: "Sheffield", date: $('input[name="date"]').val()},

答案 1 :(得分:-1)

function StartAjax(ResultsId){
var date = document.getElementsByName("date").val();
    $.ajax({
      type: "POST",
      url: "postTest.php",
      cache: false,
      data: "name=Peter&location=Sheffield&date=" + date,
      success: function(html, status){
        $("#"+ResultsId).append(html);
        $('#status').append(status);
      } 
    });
}