2013-05-05 57 views
0

這是我第一週的PHP(所以不要欺負)。我試圖抓住選擇表單的值並插入到我的數據庫中。插入是好的,但我似乎無法通過的價值。如何在AJAX中獲取選擇表單的值?

腳本:

<script type="text/javascript"> 
    //add to top 10 
    function addTop10() 
    { 
    var show_id = $('#show_id').val(); 
    var user_id = $('#user_id').val(); 
    var rank = $('select.rank').val(); 
     //Storing the value of textbox into a variable 
    if(show_id == '')  //Checking for NULL 
    { 
    $('#propspectDiv').html('Enter A Valid Name'); //Prints the progress text into our Progress DIV 
    $('#show_id').addClass('error');     //Adding the error class to the progress DIV 
    return; 
    } 
    else{ 
    $('#show_id').removeClass('error'); 
    $('#propspectDiv').removeClass('error'); //Removing the error class from the progress DIV 
    $('#propspectDiv').html('Submitting your Request.<img src="ajax.gif" />');//Prints the progress text into our Progress DIV 

    $.ajax({ 
    url : 'top10ajax.php', //Declaration of file, in which we will send the data 
    data:{ 
    "show_id" : show_id, 
    "user_id" : user_id, 
    "rank" : rank   //we are passing the name value in URL 
    }, 
    success : function(data){ 
    window.setTimeout(function(){ 
    $('#propspectDiv').html('Added'); //Prints the progress text into our Progress DIV 
    }, 2000); 
    } 
    }); 
    } 
    } 

    </script> 

和HTML

<div class="row"> 
     <div class="twelve columns"> 
<h4>Add to your top 10 </h4> 
<div class="two columns"> 
    <form> 
<select name="rank"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
    <option value="5">5</option> 
    <option value="6">6</option> 
    <option value="7">7</option> 
    <option value="8">8</option> 
    <option value="9">9</option> 
    <option value="10">10</option> 

</select> 
</div> 
<div class="four columns"> 
     <button class="large button" id="rank" type="button" value="Add to Top 10" onclick="addTop10()"></form> 
</div> 

我在做什麼錯,除了糟糕的格式?

+1

我看不出有任何PHP .. – dbf 2013-05-05 13:17:33

+0

我看到你提到的格式問題 - 在一般情況下,它的修復是一個好主意在發佈之前,爲了使您的代碼更易於閱讀。 – halfer 2013-05-05 13:24:30

+0

我假設'$'是jQuery對象,並已相應地重新簽署了您的問題。 – halfer 2013-05-05 13:27:20

回答

2

select.rank將選擇select類型的元素,它是rank類的成員。

<select name="rank">不是任何類的成員。它沒有類屬性。

您需要改變您的選擇器,使其與元素相匹配,反之亦然。

1

你試圖讓選擇框的值,以錯誤的方式試試這個,而不是

var rank = $("select['name=rank']").val(); 
相關問題