PHP 在验证错误后保留输入的值
我有一个需要验证的简单表单.
我用 empty()
函数做到了这一点.如果验证未通过,它会向用户发出警报.创建此警报后,所有输入的值都会消失.
I have a simple form that needed validation.
I did this with the empty()
function. If the validation doesn't pass it gives the user an alert. As soon as this alert is created all entered values are gone.
我想保留它们.
这就是我所做的:
<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
<table>
<tr>
<td>
Name:<input type="text" id="name" name="name">
</td>
</tr>
<tr>
<td>
<input class="submit-button" type="submit" value="Send" name="submit">
</td>
</tr>
</table>
</form>
<?php
if($_POST["submit"]){
if (!empty ($_POST["name"])){
// do something
}else{
?>
<script type="text/javascript">
alert('U heeft niet alle velden ingevuld. Graag een volledig ingevuld formulier versturen');
</script>
<?php
}
?>
推荐答案
将输入的值作为默认值传递给输入:
Pass that entered value as a default value to input:
<input type="text" id="name" name="name" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>">
相关文章