无法读取null的属性值。 onclick没有价值
我有一个网页,可以更新与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);
}
?>
在你的函数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)
实际上是空的,这就是你得到这个错误的原因。
嗯奇怪的是,我只是跟着我在YouTube上观看的教程,我做了他在视频中做的,但他的代码在他点击更新按钮时工作。 – user5567987
我的不好。它应该是onclick =“(this.name)”不是名字或id,因为它定义了属性名称。 :D – user5567987
新问题它只是更新显示的记录,但不在数据库 – user5567987
您可能想了解[最小,完整和可验证示例](http://*.com/help/mcve)。 – secelite
哪一行是229(如JS所示)? – Richard
@理查德229是结束脚本标记 – user5567987