我想禁用文本按钮,使其不可编辑。文本按钮中的数据来自 SQL 查询。当查询返回值时,禁用工作正常。但是,当没有值时,它会显示“disabled="true"”,而不仅仅是灰显。
这是我的代码:
<?php
$con=mysqli_connect("hostname","username","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$IMEI = mysqli_real_escape_string($con, $_POST['IMEI']);
$result = mysqli_query($con,"SELECT * FROM mobile
WHERE IMEI LIKE '$IMEI%'");
while($data = mysqli_fetch_array($result)) {
echo "<input type='text' value=".$data['IMEI']." disabled='true'>";
echo "<input type='text' value=".$data['SecIMEI']." disabled='true'>";
echo "<input type='text' value=".$data['Brand']." disabled='true'>";
echo "<input type='text' value=".$data['Model']." disabled='true'>";
echo "<input type='text' value=".$data['Color']." disabled='true'>";
echo "<input type='text' value=".$data['Remarks']." disabled='true'>";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<fieldset><legend>Search Mobile</legend> <br>
<form method="post">
IMEI: <input type="text" name="IMEI"> </pre><input type="submit" action="searchmob2.php">
</form>
</fieldset>
</body>
</html>
另外,如何在页面加载时隐藏 php 部分?
最佳答案
您的值属性缺少引号。变化:
echo "<input type='text' value=".$data['IMEI']." disabled='true'>";
至:
echo "<input type='text' value='".$data['IMEI']."' disabled='true'>";
此外,您应该在页面主体中循环执行 while 循环 - 此时它将在 HTML header 之前回显它。
关于php - 当 php/sql 查询返回 null 时,禁用 ="true"出现在文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156455/