2012-06-08 16 views
0
只發送的第一個值

我做的AJAX功能使用表單和輸入類型=隱藏插入做到數據庫,但是當我在同一頁面放置超過形式,它始終是在第一種形式不止一種形式以相同的輸入ID與阿賈克斯

這裏僅舉第一個值是代碼:

Ajax的功能

<script language="javascript" type="text/javascript"> 
    //Browser Support Code 
    function ajaxFunction(){ 
     var ajaxRequest; // The variable that makes Ajax possible! 
     try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
     } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
       try{ 
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
       } catch (e){ 
        // Something went wrong 
        alert("Your browser broke!"); 
        return false; 
       } 
      } 
     } 
     // Create a function that will receive data sent from the server 
     ajaxRequest.onreadystatechange = function(){ 
     if(ajaxRequest.readyState == 4){ 
      var ajaxDisplay = document.getElementById("ajaxDiv"); 
      ajaxDisplay.innerHTML = ajaxRequest.responseText; 
     } 
    } 
    var in1 = document.getElementById("in1").value; 
    var queryString1 = "?in1=" + in1; 

    //ajaxRequest.open("GET", "/sites/all/php/check.php" + queryString, true); 
    ajaxRequest.open("GET", "/sites/all/php/check.php" + queryString1, true); 
    ajaxRequest.send(null); 
} 
//–> 
</script> 

這裏是HTML表單

<form name="myForm1"> 
     <input type="hidden" value="1" id="in1"> 
     <a onclick="ajaxFunction()" class="folloo"></a> 
     <form name="myForm2"> 
       <input type="hidden" value="2" id="in1"> 
       <a onclick="ajaxFunction()" class="folloo"></a> 
       <form name="myForm3"> 
        <input type="hidden" value="3" id="in1"> 
        <a onclick="ajaxFunction()" class="folloo"></a> 
       </form> 
     </form> 
    </form> 

這裏是check.php代碼做DB查詢

<?php 
    //Connect to MySQL Server 
    //connect to your database ** EDIT REQUIRED HERE ** 
    mysql_connect("localhost","admin","123456") or die('Cannot connect to the database 
       because: ' . mysql_error()); 

    //specify database ** EDIT REQUIRED HERE ** 
    mysql_select_db("dbname") or die("Unable to select database"); 
    //select which database we're using 

    // Retrieve data from Query String 
    $in1 = $_GET['in1']; 

    // Escape User Input to help prevent SQL Injection 
    $in1 = mysql_real_escape_string($in1); 

    //Build and run a SQL Query on our MySQL tutorial 
    if($in1){ 
     mysql_query("INSERT INTO test (name) 
        VALUES ('" . $in1. "')"); 
    } 

?> 

看跌當我點擊任何鏈接,它總是發送第一個值。

CAH誰能告訴mewhat是這裏的問題?

回答

1

idmust be unique on the page。你已經表明你有多個input type="hidden"元素具有相同id值("in1")。如果你有同樣的id值,當你試圖尋找它超過一個元素(例如,使用getElementById),大多數瀏覽器會給你的第一個,但它是不確定的行爲,因爲標記/ DOM是無效

解決方法是修復的ID,以便他們在所有獨特的,或不使用ID和使用的東西是唯一的要求,如nameclass   —不過,你會需要用querySelectorAll或類似的方法檢索它,並處理你有多個匹配元素的事實。

+0

document.getElementsByName(「」)。value返回未定義的值 –

+0

@ManMann:'getElementsByName'返回**元素**,複數形式爲'NodeList'。 'value'是*個人*元素的屬性。 (另外,我假設你沒有像上面顯示的那樣向它傳遞一個空字符串?)如果你給它一個帶有元素名稱的字符串,然後查看結果列表的「長度」,並索引到它與'[0]','[1]'等,你應該能夠找到每個這些'價值'屬性。 –

+0

:我做了更改,但仍返回undefined值:( –