我最近似乎在努力解决一些代码问题。它有一个选择形式,在提交时验证。如果出现错误,则会报告出现错误,并在下方再次显示该表单,供用户更正。但是,如果没有错误且一切都成功,它仍会显示下面的表单。我不希望它在没有错误的情况下显示表单,这可以通过我设置的方式来实现吗?
<?php
if (isset($_POST['submit']))
{
if ($row2[matches] == 3)
{
if ($_POST["matches"] == 1 & $_POST["matches2"] == 1) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
if ($_POST["matches"] + $_POST["matches2"] < 2) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
if ($_POST["matches"] + $_POST["matches2"] == 4) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You cannot have 4 matches!</span>";
}
if ($row2[matches] == 1)
{
if ($_POST["matches"] + $_POST["matches2"] < 1) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
if ($_POST["matches"] + $_POST["matches2"] == 2) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
}
if ($error["matches"] == '')
{
echo "test";
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>?&id=<?php
echo "$id"; ?>">
<select name="matches">
<?php
if ($row2[matches] == 1)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
}
elseif ($row2[matches] == 3)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
}
?>
</select>
<select name="matches2">
<?php
if ($row2[matches] == 1)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
}
elseif ($row2[matches] == 3)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
}
?>
</select>
</fieldset>
<input type="submit" class="md-trigger blue-texture postbit-button-big md-pointer" name="submit" id="submit" value="Submit" />
</form>
答案 0 :(得分:1)
您将错误存储在我$error["matches"]
内的错误中,因此简单的if()
就足够了。
if (empty($error['matches']) == FALSE) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>?&id=<?php echo "$id"; ?>">
<select name="matches">
<?php
if ($row2[matches] == 1) {
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
} elseif ($row2[matches] == 3) {
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
}
?>
</select>
<select name="matches2">
<?php
if ($row2[matches] == 1) {
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
} elseif ($row2[matches] == 3) {
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
}
?>
</select>
</fieldset>
<input type="submit" class="md-trigger blue-texture postbit-button-big md-pointer" name="submit" id="submit" value="Submit" />
</form>
<?php
}
?>
为了良好的实践,让我们在第1行实例化$error
。
$error = array("matches" => array());
答案 1 :(得分:1)
<?php
$validated = false;
if (isset($_POST['submit']))
{
if ($row2[matches] == 3)
{
if ($_POST["matches"] == 1 & $_POST["matches2"] == 1) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
if ($_POST["matches"] + $_POST["matches2"] < 2) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
if ($_POST["matches"] + $_POST["matches2"] == 4) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You cannot have 4 matches!</span>";
}
if ($row2[matches] == 1)
{
if ($_POST["matches"] + $_POST["matches2"] < 1) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
if ($_POST["matches"] + $_POST["matches2"] == 2) $error["matches"] = "<span class='error'><span class='errorbox'><img src='images/other/error2.png' style='vertical-align:middle' /></span>ERROR: You are attempting to report an invalid score!</span>";
}
if ($error["matches"] == '')
{
$validated = true;
}
}
?>
if (! $validated) {
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>?&id=<?php
echo "$id"; ?>">
<select name="matches">
<?php
if ($row2[matches] == 1)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
}
elseif ($row2[matches] == 3)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
}
?>
</select>
<select name="matches2">
<?php
if ($row2[matches] == 1)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
}
elseif ($row2[matches] == 3)
{
echo "<option value='0'>0</option>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
}
?>
</select>
</fieldset>
<input type="submit" class="md-trigger blue-texture postbit-button-big md-pointer" name="submit" id="submit" value="Submit" />
</form>
}