2014-10-03 84 views
0

我正在寻找将ajax中的cityField的值传递给$ _POST ['cityField']。一切看起来都在秩序,但该变量不传递到PHP上nextpage.phpAjax变量不传递给php

<script type="text/javascript"> 
$('#city').blur(function() { 
    var cityField=$('#city').val(); 
    $.ajax({ 
     type:"post", 
     url:"nextpage.php", 
     data:{'cityField':cityField }, 
     dataType:"html", 
     success:function(data) { 
      alert("You typed:"+cityField); 
     } 
    }); 
}); 
</script> 
+1

什么事?你可以发布你的PHP网站的方法? – 2014-10-03 11:56:44

+1

在nextpage.php中显示whats还有如何验证该值没有通过? – mithunsatheesh 2014-10-03 11:57:07

+1

您正在提醒成功事件中的“cityField”。警报“数据”以查看服务器响应。 – nocarrier 2014-10-03 11:58:37

回答

0

不要使用“车市场”为目标。因为数据包含格式为data {object:value}的参数。 使用此:

<script type="text/javascript"> 

$('#city').blur(function() { 
var cityField=$('#city').val(); 
$.ajax({ 
    type:"post", 
    url:"nextpage.php", 
    data:{myobj:cityField }, 
    dataType:"html", 
    success:function(data) { 
     alert("You typed:"+cityField); 
    }); 
</script> 

,然后检索在服务器端MyObj中。

0

在您的成功函数中,您将在名为data的变量中获取由nextpage.php发送的数据。您正在提醒cityfield变量。

因此改变你的成功的功能类似于这样的事情:

.... 
success: function (data) { 
    console.log('This is the answer of nextpage.php : '+ data; 
} 
... 

除此之外,也许是将数据条到对象的$.ajax调用的外部是一个好主意:

var myPostData = {}; 
myPostData.cityField = $('#city').val(); 

.... 
data: myPostData, 
.... 
1

它很简单。改为:

... 
data:$('#city').serialize() 
... 

或完全使用这个:

<script type="text/javascript"> 
$('#city').blur(function() { 
    var $this=this; 
    $.ajax({ 
     type:"post", 
     url:"nextpage.php", 
     data:$($this).serialize(), 
     dataType:"html", 
     success:function(data) { 
      alert("You typed:"+cityField); 
     } 
    }); 
}); 
</script> 

更多的看到这一点:http://api.jquery.com/serialize/

+0

注意:在我的答案中,#city应该是input元素。你的工作是什么#city? – 2014-10-03 13:33:50