将PHP表单中的字段从必需更改为可选

时间:2016-07-30 20:56:15

标签: php html twitter-bootstrap

对于PHP来说,我是一个完整的新手。我下载了一个我在网上找到的表格,包含在我的Bootstrap网站中。这是一个非常简单的形式,任何人都可以用来向我发送消息。我设法设置Wamp来测试PHP但是当我将Phone字段留空时它会给我一条错误消息,告诉我请回去纠正错误。我想这样做,如果有人遗漏了电话号码,它仍会发送电子邮件。谢谢。

HTML

<form name="contactform" method="post" action="index.php" class="form-vertical">
      <div class="form-group">
          <label for="inputName" class="control-label">Name</label>
              <input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last">
      </div>
      <div class="form-group">
          <label for="inputEmail" class="control-label">Email*</label>
          <input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required">
      </div>
      <div class="form-group">
        <label for="inputPhone" class="control-label">Phone Number</label>
        <input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional">
      </div>
      <div class="form-group">
        <label for="inputMessage" class="control-label">Message</label>
        <textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea>
      </div>
      <div class="form-group">
        <button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button>
      </div>
    </form>

PHP

<?php
    /* Set e-mail recipient */
    $myemail = "myaccount@gmail.com";

    /* Check all form inputs using check_input function */
    $name = check_input($_POST['inputName'], "First and Last");
    $email = check_input($_POST['inputEmail'], "Required");
    $phone = check_input($_POST['inputPhone'], "Optional");
    $message = check_input($_POST['inputMessage'], "Brief Description");

    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
        show_error("Invalid e-mail address");
    }
    /* Let's prepare the message for the e-mail */

    $subject = "Contact Message from mywebsite.net";

    $message = "

    Someone has sent you a message using your contact form:

    Name: $name
    Email: $email
    Phone: $phone

    Message:
    $message

    ";

    /* Send the message using mail() function */
    mail($myemail, $subject, $message);

    /* Redirect visitor to the thank you page */
    header('Location:contact.html');
    exit();

    /* Functions we used */
    function check_input($data, $problem='')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            show_error($problem);
        }
        return $data;
    }

    function show_error($myError)
    {
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
        exit();
    }
?>

4 个答案:

答案 0 :(得分:1)

请更改此行:

$phone = check_input($_POST['inputPhone'], "Optional");

为:

$phone =  check_input($_POST['inputPhone']);

这种方式show_error($problem);不会被调用。

答案 1 :(得分:0)

因为函数check_input包含函数show_error

功能show_error有错误时会exit()

所以,你的手机输入== NULL =&gt;所以请error并致电exit()

此案例的解决方案

请勿通过电话号码输入检查是否需要。

答案 2 :(得分:0)

PHP

  <?php
function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}


/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}



/* Set e-mail recipient */
$myemail = "myaccount@gmail.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = $_POST['inputPhone'];
$message = check_input($_POST['inputMessage'], "Brief Description");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Contact Message from mywebsite.net";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Phone: $phone

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();


?>

答案 3 :(得分:0)

您正面临此错误,因为您正在使用行中的$_POST['inputPhone']功能验证电话号码,例如check_input

$phone = check_input($_POST['inputPhone'], "Optional");

您可以通过多种方式避免此错误:

$phone = $_POST['inputPhone']; //not secure

OR

$phone = check_input($_POST['inputPhone'], "");

OR

$phone = filter_var($_POST['inputPhone'],FILTER_SANITIZE_STRIPPED);