【发布时间】:2016-02-13 14:50:41
【问题描述】:
嘿,伙计们,这应该很简单,我只是错过了一步。
所以我有一个表单,我想确保在转到处理我的值并将它们发送到我的电子邮件的下一页之前添加所有值。如果有人不添加值,我也会收到错误消息。重新加载页面时会显示错误消息。
我遇到的问题是您必须重新加载页面才能让 Superglobal Post 包含任何值。由于某种原因,在重新加载页面之前它不会添加它们。因此,如果您填写所有 5 个输入字段,则必须重新加载页面,然后再次提交以将其发送到我的 send_form_email.php 脚本,因为此时会添加值。如果任何值为空,我希望它在逻辑上重新加载(这将显示错误消息,告诉用户输入字段必须有内容),如果所有值都已正确添加,则自动将用户发送到 send_form_email.php。
这几乎把我的头发拉了出来,所以如果有人能帮助我了解我缺少哪一块拼图,我将不胜感激!
<form name="contactform" method="post" action="<?php echo $value; ?>">
<table width="450px">
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="firstname" type="text" name="first_name" maxlength="50" size="30" placeholder="name"><br>
<span class="error">* <?php echo $firstErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="lastname" type="text" name="last_name" maxlength="50" size="30">
<span class="error">* <?php echo $lastErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="email" type="text" name="email" maxlength="80" size="30">
<span class="error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="telephone" type="text" name="telephone" maxlength="30" size="30">
<span class="error">* <?php echo $telephoneErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<textarea class="inputs" placeholder="comments" name="comments" maxlength="1000" cols="25" rows="6"></textarea>
<span class="error">* <?php echo $commentsErr;?></span>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit" style="background-color: #0F6D87;
font-family: Exo-Light;
color: #000000;
width: 75px;
font-weight: bold;
border-color: #003D69;
border-style: outset;
font-size: .8em;
box-shadow: 2px 2px 2px rgba(0, 34, 97, 0.6);">
<INPUT TYPE="RESET">
</td>
</tr>
</table>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (!empty($_POST["first_name"]) && !empty($_POST["last_name"]) && !empty($_POST["email"]) && !empty($_POST["telephone"]) && !empty($_POST["comments"])) {
$value ="http://cdubach.com/inc/send_form_email.php";
} elseif (empty($_POST["first_name"]) || empty($_POST["last_name"]) || empty($_POST["email"]) || empty($_POST["telephone"]) || empty($_POST["comments"])){
$value = "#";
}
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
echo $_POST['submit'] . " = Submit <br>";
echo $_POST["first_name"] . " = First Name <br>";
echo $_POST["last_name"] . " = Last Name <br>";
echo $_POST["email"] . "= Email <br>";
echo $_POST["telephone"] . "= Telephone <br>";
echo $_POST["comments"] . "= Comments <br>";
echo var_dump($_Post) . "= Dump <br>";
echo $value . " = Value <br>" ;
echo $_SERVER["PHP_SELF"];
header('Content-Type: text/plain');
var_dump(htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES, 'UTF-8'));
echo "<br>";
echo htmlspecialchars("<a href='test'>Test</a>", ENT_XHTML, 'UTF-8');
echo "<br>";
$str = "A 'quote' is <b>bold</b>";
/* */
//convert from utf8
$str = utf8_decode($str);
//translate HMTL entities
$trans = get_html_translation_table(HTML_ENTITIES);
$str = strtr($str, $trans);
echo htmlspecialchars($str);
echo "<br>";
echo htmlentities($str, ENT_QUOTES);
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//Check First Name Field if Nothing Post Error
if (empty($_POST["first_name"])) {
$firstErr = "Name is required";
} else {
$firstErr = test_input($_POST["name"]);
}
//Check Last Name Field if Nothing Post Error
if (empty($_POST["last_name"])) {
$lastErr = "Last Name is required";
} else {
$lastErr = test_input($_POST["last_name"]);
}
//Check Email Field if Nothing Post Error
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$emailErr = test_input($_POST["email"]);
}
//Check Telephone Field if Nothing Post Error
if (empty($_POST["telephone"])) {
$telephoneErr = "Telephone is Required";
} else {
$telephoneErr = test_input($_POST["telephone"]);
}
//Check Comments Field if Nothing Post Error
if (empty($_POST["comments"])) {
$commentsErr = "Comments is Required";
} else {
$commentsErr = test_input($_POST["comments"]);
}
//Check Comments
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
【问题讨论】:
-
你可以使用 if(isset($_POST)&&(!empty($_POST)))
标签: php forms validation post submit