PHP表单地址输入框仅显示首词问题求助(附代码)
问题解决:输入框仅显示地址第一个词
问题原因
你的代码里,input标签的value属性值没有用引号包裹。当地址字符串包含空格时,浏览器会将空格后的内容识别为新的HTML属性,因此只会渲染第一个空格前的内容。
解决方法
给value属性值加上双引号(或单引号),同时建议用htmlspecialchars()处理变量,避免XSS漏洞和特殊字符导致的HTML解析错误。
修改后的代码示例
$Name="John"; $Surname="Borg"; $Address="Leoforos Lemesou, 74"; echo ' <form method="post"> <label for="name">Name:</label><br> <input type="text" id="name" name="name" value="' . htmlspecialchars($Name) . '" /><br> <label for="surname">Surname:</label><br> <input type="text" id="surname" name="surname" value="' . htmlspecialchars($Surname) . '" /><br> <label for="address">Address</label><br> <input type="text" id="address" name="address" value="' . htmlspecialchars($Address) . '" /><br> <input type="submit" name="edit" value="Edit"> </form> ';
更简洁的写法(用双引号包裹HTML)
如果用双引号包裹整个HTML字符串,PHP变量可以直接嵌入,代码可读性更高:
$Name="John"; $Surname="Borg"; $Address="Leoforos Lemesou, 74"; echo " <form method='post'> <label for='name'>Name:</label><br> <input type='text' id='name' name='name' value='" . htmlspecialchars($Name) . "' /><br> <label for='surname'>Surname:</label><br> <input type='text' id='surname' name='surname' value='" . htmlspecialchars($Surname) . "' /><br> <label for='address'>Address</label><br> <input type='text' id='address' name='address' value='" . htmlspecialchars($Address) . "' /><br> <input type='submit' name='edit' value='Edit'> </form> ";
内容的提问来源于stack exchange,提问作者Βασίλης Χατζηπαντελή
相关产品推荐
相关产品推荐

