2015-08-19 44 views
0

使用$ .post(),我試圖將$.cookie('pear')的值發送到我的PHP表單中,它將使用它進行一些操作。但問題是,PHP表單似乎無法獲得$.cookie('pear')的值。

要明確,$.cookie('pear')肯定有價值。我做了alert($.cookie('pear'))仔細檢查,它肯定顯示的值爲$.cookie('pear')

查看我的代碼如下。感謝幫助指出它有什麼問題。也許我的$ .post()的實現出了問題?

jQuery的

submit_fruit.on({ 

    click: function() { 

     $('#market').click(); 

     var pears = $.cookie('pears') 

     alert(pears) // this works. it alert the value of the cookie 'pears' 

     $.post('fruitsubmission.php', { pears: pears }, function(data) { 

    }); // but this troublesome piece of $.post() doesn't seem to send 'pears' over to the PHP form. 

} 

}); 

PHP

$fruit = $_POST['pears']; 

echo $fruit; 

-- some mySQL data submissions stuff here -- 

但我不能拿到從$。員額()調用發送過來的值 '梨'。

錯誤報告

Notice: Undefined index: pears in ******* on line 10 
+0

爲什麼在$ .post('#fruitsubmission.php',...''中使用'#'字符?像這樣使用它會影響發送請求的頁面。你的意圖是嗎? –

+0

對不起,這是我的疏忽,我的意思是沒有標籤,我仍然得到相同的錯誤報告。 – blurgoon

+0

如果你設置var varars =「這是一個測試」;你是否得到相同的錯誤? –

回答

0

我能夠重現PHP Notice: Undefined index當我設置pears爲空數組或空對象:

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
<script> 
$(document).ready(function() { 
    // var pears = {}; 
    var pears = new Array(); 
    $.post('fruitsubmission.php', { pears: pears }, function(data) { 
     alert(data); 
    }); 
}); 
</script> 

當我將它設置成類似var pears = "this is a test";,它按預期工作,所以也許你得到的錯誤是由腳本的其他部分引起的。

試着在你的環境中隔離這個錯誤,你可以從上面的腳本開始,並隨時添加其他元素。