我需要先验证错误,然后才能转到表单的操作网址。 当我点击提交时,即使有错误,也会转到/test/policy.php。 我希望它只有在我的表单中没有错误时才会去那里。 到目前为止,这是我的代码:
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['submit']))
{
// Each time theres an error, add an error message to the error array
// using the field name as the key.
if (empty($_POST['first_name']))
$arrErrors['first_name'] = '<br><font color="red">Please provide your first name.</font>';
if (empty($_POST['last_name']))
$arrErrors['last_name'] = '<br><font color="red">Please provide your last name.</font>';
if (empty($_POST['business_name']))
$arrErrors['business_name'] = '<br><font color="red">Please provide your business name.</font>';
}
// If the error array is empty, there were no errors.
// Insert form processing here.
if (count($arrErrors) == 0)
{
$first_name=cleaninput($_POST['first_name']);
$last_name=cleaninput($_POST['last_name']);
$business_name=cleaninput($_POST['business_name']);
//Insert the details into database
}
else
{
// The error array had something in it. There was an error.
// Start adding error text to an error string.
$failure_message= "There was an error in the form";
$strError="";
foreach ($arrErrors as $error)
{
$strError .= $error;
}
}
}
?>
<tr>
<td>
<? if(!empty($success_message))
{
echo "<center><font color='blue'><b>".$success_message."</b></font><br>";
}
if(!empty($failure_message))
{
echo "<center><font color='red'><b>".$failure_message."</b></font><br>";
}
?>
<?
if((empty($_POST)) || !empty($strError))
{
echo "<table align= 'center' width='70%' style='border-top: 1px; border-right: 1px; border-left: 1px; border-bottom: 1px; border-color: black; border-style: solid;'>";
?>
<tr>
<td>
<form action="/test/policy.php" method="post">
<tr height='40'>
<td>
<font color="black"><B> First Name: </B></font>
</td>
<td><input type="text" size ="40" name="first_name" value="<? if(!empty($strError)) {echo cleaninput($_POST['first_name']);}?>" class="advertising-inputbox-style" />
<?php if (!empty($arrErrors['first_name'])) echo $arrErrors['first_name']; ?>
</td>
</tr>
<tr height='40'>
<td><font color="black"><B> Last Name: </B></font></td>
<td><input type="text" size ="40" name="last_name" value="<? if(!empty($strError)) { echo cleaninput($_POST['last_name']);}?>" class="advertising-inputbox-style"/>
<?php if (!empty($arrErrors['last_name'])) echo $arrErrors['last_name']; ?>
</td>
</tr>
<tr height='40'>
<td><font color="black"><B> Email Address: </B></font></td>
<td><input type="text" size ="40" name="email_address" value="<? if(!empty($strError)) { echo cleaninput($_POST['email_address']);}?>" class="advertising-inputbox-style"/>
<?php if (!empty($arrErrors['email_address'])) echo $arrErrors['email_address']; ?>
</td>
</tr>
<tr height='35'>
<td><font color="black"><B> Business Name: </B></font></td>
<td><input type="text" size ="40" name="business_name" value="<? if(!empty($strError)) { echo cleaninput($_POST['business_name']);}?>" class="advertising-inputbox-style" />
<?php if (!empty($arrErrors['business_name'])) echo $arrErrors['business_name']; ?>
</td>
</tr>
<tr height='35'>
<td></td>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</form>
<?}?>
</td>
</tr>
</table>
答案 0 :(得分:1)
您可以将操作留空,然后在验证后,您可以使用header
重定向到/test/policy.php。
if (count($arrErrors) == 0) {
header('Location: /test/policy.php');
}
答案 1 :(得分:0)
如果上面的代码在一个文件中,那么只有在没有错误的情况下才必须重定向用户。所以,假设这个文件叫做index.php,然后表单会有action =“index.php”并在提交后自行转到:
<form action="/test/index.php" method="post">
但是在将用户表单数据插入数据库之后,您将用户重定向到/test/policy.php,如下所示:
//Insert the details into database
your database insert code
....
header('Location: /test/policy.php');
die();
答案 2 :(得分:0)
不,您在重定向后无法使用POST,但如果您需要first_name,则已在数据库中使用它。所以在重定向时在GET中发送用户ID,如下所示:
//Insert the details into database
your database insert code
$user_id = mysql_insert_id();
....
header('Location: /test/policy.php?user_id='.$user_id);
die();
在policy.php中从数据库中选择用户字段first_name