我需要你的帮助,了解一些表单/php代码:
- 2第一个字段代表name第二个字段代表fam.name
- 如果"用户"同时填写了两个字段,则会与他填写的内容相呼应
- 如果"用户"没有填写任何字段,他会返回带有错误消息的表单
- 如果他只填写2个字段中的1个,也是一样的
- 我的问题是,如果"用户"只填写一个字段并发送(提交),我希望他像现在一样收到错误消息,但也保存他的输入(他填写字段的日期),这样他只需要填写另一个字段,而不需要再次填写这两个字段
-
我尝试了session_start();但是失败了。
<?php $errormessage=""; //check if 1 of 2 fields is empty if(empty($_POST['vorname'])){ $errormessage=$errormessage." First field ,"; } if(empty($_POST['nachname'])){ $errormessage=$errormessage." Second field ,"; } //Submit starts if (isset($_POST['go'])) { if (!empty($_POST['vorname'])&& !empty($_POST['nachname'])){ echo "First field : ".$_POST["vorname"]." "; echo " 2nd field: ".$_POST["nachname"]; }//end empty //ELse if inputs empty if (!empty($errormessage)) { ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <input type="text" name="vorname" /> <input type="text" name="nachname" /> <input type="submit" name="go" /> <?php echo "error: ".$errormessage; ?> </form> <?php } //ende if errormessage }//ende if isset go else {//start side ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <input type="text" name="vorname" /> <input type="text" name="nachname" /> <input type="submit" name="go" /> </form> <?php} //end else?>
如果用户只为一个字段指定了一些信息并发送它,为什么不将$_POST var中正确填充的数据粘贴到您的一个输入的值属性中:
<input type="text" name="vorname" value="<?php echo $correct_data ?>"/>
并显示额外的错误消息,就像你做的那样?
我的意思是,您可以将旧的$_POST['vorname']和$_POST['nachname']保存到两个额外的变量中,如果其中一个$_POST字段为空,则其中一个变量为空,然后将这些变量以html形式添加到输入的值属性中:
$errormessage = "";
$old_vorname = empty($_POST['vorname']) ? "" : $_POST['vorname'];
$old_nachname = empty($_POST['nachname']) ? "" : $_POST['nachname'];
....
//ELse if inputs empty
if (!empty($errormessage)) { ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" value="<?php echo $old_vorname; ?>"/>
<input type="text" name="nachname" value="<?php echo $old_nachname; ?>"/>
<input type="submit" name="go" />
<?php echo "error: ".$errormessage; ?>
</form>
html输入元素的值属性包含它的实际内容,您可以在网页上看到或编辑。