如何使用回显隐藏PHP未定义的错误消息?



我是PHP的新手,我正在尝试拥有一个div,它在用户使用echo提交用户信息后输出用户信息。

这是我现在的代码:

<label class="input-fields" name="cust-name">
Name <input type="text" id="cust[name]" placeholder="E.g. John Doe" required>
</label>
<label class="input-fields" name="cust-email">
Email <input type="email" id="cust[email]" placeholder="E.g. john@mail.com" required>
</label>
<label class="input-fields" name="cust-mobile">
Mobile <input type="text" id="cust[mobile]" placeholder="E.g. (04) 6172 5705" required>
</label>
<div class="order-info">Name: <?php echo $_POST["cust-name"]; ?></div>
<div class="order-info">Email: <?php echo $_POST["cust-email"]; ?></div>
<div class="order-info">Mobile: <?php echo $_POST["cust-mobile"]; ?></div>

它没有给出上面示例中的错误,但在页面上它给了我一个错误,上面写着"注意:未定义的索引"。如何修复/隐藏此问题?谢谢!

发生这种情况有两个原因:

首先,您为标签而不是输入本身提供了name属性。标签需要for属性,以便它们知道它们对应于哪个输入。

<label class="input-fields" for="cust-name">
Name <input type="text" name="cust-name" id="cust[name]" placeholder="E.g. John Doe" required>
</label>

其次,您没有首先检查变量是否已设置。您需要在isset语句中包装显示信息的 HTML:

<?php if (isset($_POST["your-submit-button-name"])) { ?>
<div class="order-info">Name: <?php echo $_POST["cust-name"]; ?></div>
<div class="order-info">Email: <?php echo $_POST["cust-email"]; ?></div>
<div class="order-info">Mobile: <?php echo $_POST["cust-mobile"]; ?></div>
<?php } ?>

相关内容

最新更新