2014-10-03 80 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