以下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>
答案 0 :(得分:0)
以下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;
}