下面的PHP还将=""
写入html代码,而不是仅按预期写入变量值。如何从HTML代码中去除=""
的废话?
PHP:
while ($row = $result->fetch_assoc()) {
$select = "";
unset($id, $name);
$id = $row['customerID'];
$name = $row['name'];
if ($_SESSION['customerID'] == $id){
$select = 'selected';
}
echo '<option value="'. $id.'"'. "$select" .'>'.$name.'</option>';
}
最后一行带有"选定"的HTML结果:
<form><select onchange="showUser(this.value)" name="id">
<option value="">Select Customer</option>
<option value="0">Customer2</option>
<option selected="" value="1">Customer1</option></select></form>
预期结果:
<form><select onchange="showUser(this.value)" name="id">
<option value="">Select Customer</option>
<option value="0">Customer2</option>
<option selected value="1">Customer1</option></select></form>
下面的PHP还将
=""
写入html代码,而不是像预期的那样只写入变量值。
每当if
条件失败时,您将在value
属性之后看到此=""
。你的代码应该是这样的:
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['customerID'];
$name = $row['name'];
$option = "<option value='{$id}'";
if ($_SESSION['customerID'] == $id){
$option .= " selected";
}
$option .= ">{$name}</option>";
echo $option;
}