2016-07-30 114 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); 
相關問題