2013-05-03 67 views
0

讓這個簡單的代碼將一個數組發佈到PHP腳本的安寧。問題是沒有數據發送到服務器端。問題是什麼?

JS CODE

// Ajax call for sending song wish 
var wishName = $('#wish-name').val(); 
var wishEmail = $('#wish-email').val(); 
var wishSong = $('.wish-input input').val(); 
var postData = {name: wishName, email: wishEmail, song: wishSong}; 

function postSongSuccess(data) { 
    if(data == 1) { 
     console.log(data); 
    } 
    else if(data == 0) { 
     console.log(data); 
    } 
} 

$('#send-song').click(function() { 
    $.post("./wp-content/plugins/wish-song/wishSong.php", postData, postSongSuccess); 
    alert(wishName); 
    console.log(postData); 
}); 

HTML CODE

<div class="wish-input-half"> 
    <label>Ditt för- och efternamn</label> 
    <input id="wish-name" type="text"> 
</div> 
<div class="wish-input-half"> 
    <label>Din e-postadress</label> 
    <input id="wish-email" type="text"> 
</div> 
<div class="clearfix"></div> 
<div class="wish-input"> 
    <label>Skriv artist och låttitel</label> 
    <input id="wish-song" type="text"> 
</div> 
<div class="wish-input-submit"> 
    <input id="send-song" type="submit"> 
</div> 
+3

你怎麼確定沒有數據發送到服務器? – Quentin 2013-05-03 13:00:40

+0

您確定您的請求曾經到達服務器嗎?您是否將它發送到正確的目的地?你可以在服務器端處理這個post請求的方法中回顯一些東西,你會在控制檯中得到消息回顯嗎? – ant 2013-05-03 13:00:42

+3

看起來像'var wishName = $('#wish-name')。val();'是在頁面加載時執行的,不一定是用戶提供的值。當用戶想要發送數據時,您必須先讀取值,而不是事先。 – 2013-05-03 13:03:15

回答

0

看起來var wishName = $('#wish-name').val();是當頁面加載,不一定當值被改變/由用戶提供執行。

您必須在用戶想要發送數據時讀取值,而不是預先寫入。

function postSongSuccess(data) { 
    if(data == 1) { 
     console.log(data); 
    } 
    else if(data == 0) { 
     console.log(data); 
    } 
} 

$('#send-song').click(function() { 
    $.post("./wp-content/plugins/wish-song/wishSong.php", { 
     name: $('#wish-name').val(), 
     // ... 
    }, postSongSuccess); 
});