2016-07-30 79 views
0

当谈到PHP时,我是一个完全新手。我下载了一个我在网上找到的表单,将其包含在我的Bootstrap网站中。这是一个非常简单的形式,任何人都可以用来给我发送消息。我设法设置了Wamp来测试PHP,但是当我将Phone字段留空时,它给了我一个错误消息,告诉我请返回并更正错误。我想这样做,如果有人遗漏了电话号码,它仍然会发送电子邮件。谢谢。将PHP字段中的字段从必需字段更改为可选字段

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 = "[email protected]"; 

    /* 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(); 
    } 
?> 
+0

能否请您发布错误? –

+0

请更正以下错误: 可选 点击后退按钮并重试 – user3786102

回答

1

请改变这一行:

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

到:

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

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

0

因为功能check_input包括功能show_error

和函数show_error有错误时有exit()

所以,你的手机输入== NULL =>所以有error并致电exit()

此案例的解决方案

请勿使用电话号码输入检查需求。

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 = "[email protected]"; 

/* 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(); 


?> 
0

,因为你使用的是check_input功能在该行

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

就可以避免以多种方式这个错误验证的电话号码,即$_POST['inputPhone']您都面临这个错误:

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

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

OR

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