2016-11-28 52 views
0

我有一個網頁,可以更新與AJAX生活的表。我在onlick函數中遇到了一些問題。無法讀取null的屬性值。 onclick沒有價值

每當我點擊更新按鈕它顯示在控制檯上

無法讀取空的特性 '值'(...)BB @ manageaccounts.php:184的onclick @ manageaccounts.php:229

我在我的update.php文件中檢查了它,但代碼很好。我試圖將onclick函數從onclick="(this.id)"更改爲onclick="(this.firstname)",但都沒有工作。

manageaccounts.php

<body> 
    <div class="wrapper"> 
     <div id="data" class="table"></div> 
    </div> 
    <script type="text/javascript"> 
     data(); 

     function data() { 
      var xmlhttp = new XMLHttpRequest(); 
      xmlhttp.open("GET", "update.php?status=disp", false); 
      xmlhttp.send(null); 
      document.getElementById("data").innerHTML = xmlhttp.responseText; 
     } 

     function aa(a) { 
      firstid = "firstname" + a; 
      txtfirstid = "txtfirst" + a; 
      var firstname = document.getElementById(firstid).innerHTML; 
      document.getElementById(firstid).innerHTML = "<input type='text' value='" + firstname + "' id='" + txtfirstid + "'>"; 

      midid = "middlename" + a; 
      txtmidid = "txtmid" + a; 
      var middlename = document.getElementById(midid).innerHTML; 
      document.getElementById(midid).innerHTML = "<input type='text' value='" + middlename + "' id='" + txtmidid + "'>"; 

      lastid = "lastname" + a; 
      txtlastid = "txtlast" + a; 
      var lastname = document.getElementById(lastid).innerHTML; 
      document.getElementById(lastid).innerHTML = "<input type='text' value='" + lastname + "' id='" + txtlastid + "'>"; 

      addid = "address" + a; 
      txtaddid = "txtadd" + a; 
      var address = document.getElementById(addid).innerHTML; 
      document.getElementById(addid).innerHTML = "<input type='text' value='" + address + "' id='" + txtaddid + "'>"; 

      gendid = "gender" + a; 
      txtgendid = "txtgend" + a; 
      var gender = document.getElementById(gendid).innerHTML; 
      document.getElementById(gendid).innerHTML = "<input type='text' value='" + gender + "' id='" + txtgendid + "'>"; 

      contid = "contact" + a; 
      txtcontid = "txtcont" + a; 
      var contact = document.getElementById(contid).innerHTML; 
      document.getElementById(contid).innerHTML = "<input type='text' value='" + contact + "' id='" + txtcontid + "'>"; 

      updateid = "update" + a; 
      document.getElementById(a).style.visibility = "hidden"; 
      document.getElementById(updateid).style.visibility = "visible"; 

     } 

     function bb(b) { 
      var firstid = "txtfirst" + b; 
      var firstname = document.getElementById(firstid).value; 

      var midid = "txtmid" + b; 
      var middlename = document.getElementById(midid).value; 

      var lastid = "txtlast" + b; 
      var lastname = document.getElementById(lastid).value; 

      var addid = "txtadd" + b; 
      var address = document.getElementById(addid).value; 

      var gendid = "txtgend" + b; 
      var gender = document.getElementById(gendid).value; 

      var contid = "txtcont" + b; 
      var contact = document.getElementById(contid).value; 

      update_value(b, firstname, middlename, lastname, address, gender, contact); 

      document.getElementById(b).style.visibility = "visible"; 
      document.getElementById("update" + b).style.visibility = "hidden"; 

      document.getElementById("firstname" + b).innerHTML = firstname; 
      document.getElementById("middlename" + b).innerHTML = middlename; 
      document.getElementById("lastname" + b).innerHTML = lastname; 
      document.getElementById("address" + b).innerHTML = address; 
      document.getElementById("gender" + b).innerHTML = gender; 
      document.getElementById("contact" + b).innerHTML = contact; 
     } 

     function update_value(id, firstname, middlename, lastname, address, gender, contact) { 
      var xmlhttp = new XMLHttpRequest(); 
      xmlhttp.open("GET", "update.php?id=" + id + " & firstname=" + firstname + " & middlename=" + middlename + " & lastname=" + lastname + " &address=" + address + " & gender=" + gender + " &contact=" + contact + " & status=update", false); 
      xmlhttp.send(null); 
     } 

     function delete1(id) { 
      var xmlhttp = new XMLHttpRequest(); 
      xmlhttp.open("GET", "update.php?id=" + id + " & status=delete", false); 
      xmlhttp.send(null); 
      data(); 
     } 
    </script> 
</body> 

update.php具有空值的onclick <input type="button" id="update<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="UPDATE" style="visibility: hidden;" onclick="bb(this.firstname)">

<?php 
    $conn = mysqli_connect('localhost', 'root','root', 'realestate'); 
    if (!$conn) { 
     die("Sorry we're having some problems with the database. :(".mysqli_connect_error()); 
    } 

    $status = $_GET["status"];  
    if ($status == "disp") { 
     $sql1 = "SELECT * FROM agents"; // check the change ere 
     $result=mysqli_query($conn, $sql1); 

     echo "<table>"; 
     echo "<tr> 
      <th>Agent ID</th> 
      <th>Firstname</th> 
      <th>Middlename</th> 
      <th>Lastname</th> 
      <th>Address</th> 
      <th>Gender</th> 
      <th>Contact</th> 
      <th>Action</th> 
     </tr>"; 

     while($row=mysqli_fetch_assoc($result)) { 
      echo "<tr class='da'>"; 
      echo "<td>"; echo $row["id"]; echo "</td>"; 
      echo "<td>"; ?><div id="firstname<?php echo $row["id"]; ?>"><?php echo $row["firstname"]; ?></div><?php echo "</td>"; 
      echo "<td>"; ?><div id="middlename<?php echo $row["id"]; ?>"><?php echo $row["middlename"]; ?></div><?php echo "</td>"; 
      echo "<td>"; ?><div id="lastname<?php echo $row["id"]; ?>"><?php echo $row["lastname"]; ?></div><?php echo "</td>"; 
      echo "<td>"; ?><div id="address<?php echo $row["id"]; ?>"><?php echo $row["address"]; ?></div><?php echo "</td>"; 
      echo "<td>"; ?><div id="gender<?php echo $row["id"]; ?>"><?php echo $row["gender"]; ?></div><?php echo "</td>"; 
      echo "<td>"; ?><div id="contact<?php echo $row["id"]; ?>"><?php echo $row["contact"]; ?></div><?php echo "</td>"; 
      echo "<td>"; ?><button id="<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" onclick="delete1(this.id)">DELETE</button><?php echo "</td>"; 
      echo "<td>"; ?> 
      <input type="button" id="<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="EDIT" onclick="aa(this.id)"> 
      <input type="button" id="update<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="UPDATE" style="visibility: hidden;" onclick="bb(this.firstname)"> 
     <?php echo "</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
} 

if ($status == "update") { 
    $id = $_GET["id"]; 
    $first = $_GET["firstname"]; 
    $mid = $_GET["middlename"]; 
    $last = $_GET["lastname"]; 
    $add = $_GET["address"]; 
    $gend = $_GET["gender"]; 
    $cont = $_GET["contact"]; 
    $first = trim($first); 
    $mid = trim($mid); 
    $last = trim($last); 
    $add = trim($add); 
    $gend = trim($gend); 
    $cont = trim($cont); 

    $sql2 = "UPDATE agents SET firstname='$first', middlename='$mid', lastname='$last', address='$add', gender='$gend', contact='$cont' WHERE id=$id"; 
     $result = mysqli_query($conn, $sql2); 
    } 

    if ($status == "delete") { 
     $id = $_GET["id"]; 
     $sql3 = "DELETE FROM agents WHERE id=$id"; 
     $result = mysqli_query($conn, $sql3); 
    } 
?> 
+3

您可能想了解[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – secelite

+0

哪一行是229(如JS所示)? – Richard

+0

@理查德229是結束腳本標記 – user5567987

回答

0

在你的函數BB,你正在做的:

var firstid="txtfirst"+b; 
var firstname = document.getElementById(firstid).value; 

要調用的功能等:

onclick="bb(this.firstname)" 

現在,this.firstname將作爲this指向您的按鈕,按鈕不會有一個屬性叫做firstname是不確定的。

此外,當你不喜歡

onclick="(this.id)" 

然後你的函數裏面,firstid將成爲「txtfirstupdate1」作爲按鈕的ID將是update + id並不僅僅是id。所以,document.getElementById(firstid)實際上是空的,這就是你得到這個錯誤的原因。

+0

嗯奇怪的是,我只是跟着我在YouTube上觀看的教程,我做了他在視頻中做的,但他的代碼在他點擊更新按鈕時工作。 – user5567987

+0

我的不好。它應該是onclick =「(this.name)」不是名字或id,因爲它定義了屬性名稱。 :D – user5567987

+0

新問題它只是更新顯示的記錄,但不在數據庫 – user5567987