我有一个表单,可以将信息发送到mysql数据库。错误消息和确认在单独的div中显示在表单的右侧,因此表单操作需要将用户发送到同一页面。我正在回应用户键入的字段,以防他们需要根据出现的错误消息编辑内容。但是,如果表单提交正确,我怎么能这样做,所以php不会在表单中回显?
<?php
$submit = filter_input(INPUT_POST, 'submit');
//form data
$fullname = filter_input(INPUT_POST, 'fullname');
$email = filter_input(INPUT_POST, 'email');
$business = filter_input(INPUT_POST, 'business');
$date = date("Y-m-d");
?>
然后在其他HTML代码之后......
<div class="wrap">
<div id="reg">
<form action='register.php' method='POST'>
<table>
<tr>
<td>
Your full name:
</td>
<td>
<input type='text' name='fullname' value='<?php echo $fullname;?>'>
}
</td>
</tr>
<tr>
<td>
Your email:
</td>
<td>
<input type='text' name='email' value='<?php echo $email;?>'>
</td>
</tr>
<tr>
<td>
Your business:
</td>
<td>
<input type='text' name='business' value='<?php echo $business;?>'>
</td>
</tr>
</table>
</br>
<input type='submit' name='submit' value='Register'>
</form>
</br>
</div>
<?php
if ($submit)
{
//open database
$connect=mysql_connect("localhost","root","Ryweb1994");
mysql_select_db("phapsy");
//check for existence
if($fullname&&$business&&$business)
{
$queryreg = mysql_query("INSERT INTO users VALUES ('','$fullname','$email','$business','$date')");
echo ("<div id='message'><p>You have been registered! We will send you an email with login information. Thank you for your interest in Phapsy!<p></div>");
}
else echo "Please fill in <b>all</b> fields!";
}
?>
</div>
</div>
答案 0 :(得分:1)
使用$submit
变量包裹表单:
+ <?php if($submit) { ?>
<div class="wrap">
...
<form action='register.php' method='POST'>
...
</form>
</div>
+ <?php }; ?>
<?php
if ($submit)
{
//open database
$connect=mysql_connect("localhost","root","Ryweb1994");
mysql_select_db("phapsy");
//check for existence
if($fullname&&$business&&$business)
{
$queryreg = mysql_query("INSERT INTO users VALUES ('','$fullname','$email','$business','$date')");
echo ("<div id='message'><p>You have been registered! We will send you an email with login information. Thank you for your interest in Phapsy!<p></div>");
}
else echo "Please fill in <b>all</b> fields!";
}
?>
</div>
</div>
答案 1 :(得分:1)
由于您说您正在回复确认信息,因此您显然会以某种方式测试成功输入 - 但您不会显示该代码。
像<input type='text' name='fullname' value='<?php if(!$successful){echo $fullname;} ?>'>
之类的东西会这样做
答案 2 :(得分:1)
将处理逻辑放在显示逻辑之前。
设置一个反映数据库交互是否成功的变量。
更新整个表单中的echo语句,只有在交互失败(或验证失败)时才打印一个值:
<?php $successfullyProcessed = false;
//processing logic
$successfullyProcessed = true;
<input type='text' name='fullname' value='<?php if (!successfullyProcessed) echo $fullname;?>'>