【问题标题】:PHP Forms Trouble with the elusive $_POST难以捉摸的 $_POST 的 PHP 表单问题
【发布时间】: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; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;


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


【解决方案1】:

您需要重新加载第二次才能使脚本正常工作的原因是,第一次运行时未填充表单的操作属性(因为未设置 $value),但在第二次运行时它是(如果 $_POST 通过您设置的检查)并设置为http://cdubach.com/inc/send_form_email.php

如果您在第一次和第二次运行中检查实际的 html 代码,就会看到这一点。

但是,这只是您的脚本存在的问题之一。一些提示:

  • 删除header('Content-Type: text/plain');,该行指示浏览器将页面视为文本,并且不会将其呈现为html。
  • 将整个 html 移动到您的 PHP 脚本之后 - 这样您准备的错误消息就会正常工作
  • 终于解决了“$_POST only if all ok”条件的问题,用客户端 JavaScript 检查脚本的有效性。另一方面,如果您可以访问被调用的脚本($value 指向的脚本),则可以在此处进行检查并在需要时重定向到表单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多