PHP Post参数传递验证当值为空时

发布于 2024-12-10 12:05:06 字数 5068 浏览 2 评论 0原文

在发布服务器端验证的选择值时遇到问题。当用户没有做出选择时,仍然在“选择...”上选择它。它仍然传递empty()方法,尽管该值为空。

<?php

// Email Properties
$email_to = "[email protected]";
$email_subject = "3DMark3t Contact:";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$select = $_POST['select'];

function formError($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// Make sure all fields are not empty
if(empty($first_name) || empty($last_name) || empty($email) ||
    empty(telephone) || empty($comments) || empty($select)) {
    // Return error
    formError('Please fill in all of the fields.');       
}

// Regex Tests
$error_message = "";

// Expressions
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($email_exp, $email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp, $first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp, $last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(!empty($error_message)) {
    formError($error_message);
}

$email_message = "3DMark3t Contact: \n";
$email_message .= "First Name: {$first_name} \n";
$email_message .= "Last Name: {$last_name} \n";
$email_message .= "Email: {$email} \n";
$email_message .= "Telephone: {$telephone} \n";
$email_message .= "Select One: {$select} \n";
$email_message .= "Comments: {$comments} \n";

// Create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

header("Location /");

HTML:

<form name="htmlform" method="post" action="php/html_form_send.php">
    <table width="450px">
        <tr>
            <td valign="top">
                <label for="first_name">First Name *</label>
            </td>
            <td valign="top">
                <input  type="text" name="first_name" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="last_name">Last Name *</label>
            </td>
            <td valign="top">
                <input  type="text" name="last_name" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="email">Email Address *</label>
            </td>
            <td valign="top">
                <input  type="text" name="email" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="telephone">Telephone Number</label>
            </td>
            <td valign="top">
                <input  type="text" name="telephone" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="select">Select One *</label>
            </td>
            <td valign="top">
                <select name="select">
                    <option value="">Select...</option>
                    <option value="3DModels">3DModels</option>
                    <option value="Graphic Design">Graphic Design</option>
                    <option value="Web Design">Web Design</option>
                    <option value="Tutorials">Tutorials</option>
                    <option value="Report">Report</option>
                    <option value="Requests">Requests</option>
                </select>
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="comments">Comments *</label>
            </td>
            <td valign="top">
                <textarea  name="comments" maxlength="1000" cols="32.5" rows="6"></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center">
             <input type="submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

I'm having an issue when the posting a selection value for server side validation. When the user does not make a selection, therefore it is still selected on "Select...". It still passes the empty() method, although the value is empty.

<?php

// Email Properties
$email_to = "[email protected]";
$email_subject = "3DMark3t Contact:";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$select = $_POST['select'];

function formError($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// Make sure all fields are not empty
if(empty($first_name) || empty($last_name) || empty($email) ||
    empty(telephone) || empty($comments) || empty($select)) {
    // Return error
    formError('Please fill in all of the fields.');       
}

// Regex Tests
$error_message = "";

// Expressions
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($email_exp, $email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp, $first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp, $last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(!empty($error_message)) {
    formError($error_message);
}

$email_message = "3DMark3t Contact: \n";
$email_message .= "First Name: {$first_name} \n";
$email_message .= "Last Name: {$last_name} \n";
$email_message .= "Email: {$email} \n";
$email_message .= "Telephone: {$telephone} \n";
$email_message .= "Select One: {$select} \n";
$email_message .= "Comments: {$comments} \n";

// Create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

header("Location /");

HTML:

<form name="htmlform" method="post" action="php/html_form_send.php">
    <table width="450px">
        <tr>
            <td valign="top">
                <label for="first_name">First Name *</label>
            </td>
            <td valign="top">
                <input  type="text" name="first_name" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="last_name">Last Name *</label>
            </td>
            <td valign="top">
                <input  type="text" name="last_name" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="email">Email Address *</label>
            </td>
            <td valign="top">
                <input  type="text" name="email" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="telephone">Telephone Number</label>
            </td>
            <td valign="top">
                <input  type="text" name="telephone" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="select">Select One *</label>
            </td>
            <td valign="top">
                <select name="select">
                    <option value="">Select...</option>
                    <option value="3DModels">3DModels</option>
                    <option value="Graphic Design">Graphic Design</option>
                    <option value="Web Design">Web Design</option>
                    <option value="Tutorials">Tutorials</option>
                    <option value="Report">Report</option>
                    <option value="Requests">Requests</option>
                </select>
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="comments">Comments *</label>
            </td>
            <td valign="top">
                <textarea  name="comments" maxlength="1000" cols="32.5" rows="6"></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center">
             <input type="submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2024-12-17 12:05:06

您正在测试是否设置了 $_POST['formGender'],但对于空字符串上的 isset(),PHP 将返回 TRUE。表单上没有选择意味着 $_POST['formGender'] == "",并且在 isset() 检查中被视为“设置”。

相反,测试它是否是 empty()都会验证变量是否像 isset() 一样定义以及它是否具有非空值。

if(empty($_POST['formGender']))
{
  $errorMessage .= "<li>You forgot to select your Gender!</li>";
}

还要在这里测试 empty() 而不是 !isset()

 if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
 if(empty($_POST['select']) )

Update

我不太确定你想用这个来完成什么。但是,3DModels、图形设计等不会成为$_POST数组的一部分。它们是 $_POST['select'] 的潜在值。我不明白你在这里的意图是什么。

if(empty($_POST['select']) )
{
  $var3dmodels = $_POST['3DModels'];
  $vargraphic_design = $_POST['Graphic Design'];
  $varweb_design = $_POST['Web Design'];
  $vartutorials = $_POST['Tutorials'];
  $varreport = $_POST['Report'];
  $varrequests = $_POST['Requests'];
}

你的问题根源:

我终于明白你的错误在哪里了。您正在为 $_POST['select'] 使用 $errorMessage,而不是 $error_message

if(empty($_POST['select']))
{
  // Oops...
  $errorMessage .= 'The selection you made does not appear to be valid.<br />';

  // Should be
  $error_message .= 'The selection you made does not appear to be valid.<br />';
}

您应该启用 error_reporting PHP.ini 中的 code> 和 display_errors 仅用于开发和测试。使用像 $errorMessage .= ... 这样的未初始化变量会发出注意,你会看到屏幕上的错误。对于生产代码,请关闭此功能。

// Report all errors and notices
error_reporting(E_ALL);
ini_set('display_errors', 1);

You are testing if $_POST['formGender'] is set, but PHP will return TRUE for isset() on an empty string. No selection on your form means that $_POST['formGender'] == "", and that is considered to be "set" in the isset() check.

Instead test if it is empty() which will both verify if the variable is defined like isset() and if it has a non-empty value.

if(empty($_POST['formGender']))
{
  $errorMessage .= "<li>You forgot to select your Gender!</li>";
}

Also test for empty() here instead of !isset()

 if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
 if(empty($_POST['select']) )

Update

I'm not really sure what you are trying to accomplish with this. But, 3DModels, Graphic Design, etc will not be part of the $_POST array. They are potential values of $_POST['select']. I don't understand what your intent is here.

if(empty($_POST['select']) )
{
  $var3dmodels = $_POST['3DModels'];
  $vargraphic_design = $_POST['Graphic Design'];
  $varweb_design = $_POST['Web Design'];
  $vartutorials = $_POST['Tutorials'];
  $varreport = $_POST['Report'];
  $varrequests = $_POST['Requests'];
}

The source of your problem:

I finally see where your error is. Instead of $error_message, you are using $errorMessage for $_POST['select']:

if(empty($_POST['select']))
{
  // Oops...
  $errorMessage .= 'The selection you made does not appear to be valid.<br />';

  // Should be
  $error_message .= 'The selection you made does not appear to be valid.<br />';
}

You should turn on error_reporting and display_errors in PHP.ini for development and testing only. An uninitialized variable used like $errorMessage .= ... would issue a notice, and you'd see the error on screen. Turn this off for production code.

// Report all errors and notices
error_reporting(E_ALL);
ini_set('display_errors', 1);
我不吻晚风 2024-12-17 12:05:06

试试这个:

   if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) ||
     $_POST['formGender'] == '')

Try this:

   if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) ||
     $_POST['formGender'] == '')
猫弦 2024-12-17 12:05:06

使用 javascript/jquery 评估输入字段的值怎么样?然后,如果未填写所需的输入字段,您将能够在提交时返回 false。

How about valudating the value of the input fields using javascript/jquery? Then you would be able to return false on submit, if the input fields required are not filled out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文