You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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,提问作者Βασίλης Χατζηπαντελή

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 00:50:25