2015-06-09 201 views
3

我在Python編程方面有一些以前的經驗,但是我被要求爲某人構建一個網頁,所以我試圖用一個引導框架,html和php來構建一個基本頁面)。用PHP驗證掙扎

一切似乎都是從沒有驗證的訂閱表單中完全按照需要appart,所以我認爲這是最好的impliment一些,但形式似乎並沒有反應,並仍然允許腳本完成併發送電子郵件甚至與一個空的表格。

這是因爲窗體是'Modal'嗎?我在這方面還是一個新手,我很抱歉如果這已被覆蓋,但我找不到幫助我的答案。

我附上了模態表格html和php代碼。命名

<!-- Modal form --> 
<div class="modal fade" id="Subscribe" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title" id="myModalLabel">Subscribe for updates!</h4> 
     </div> 
     <div class="modal-body"> 
      <div class="container-fluid"> 
       <form method="post" action="Submit.php"> 
       <div class="row"> 
        <div class="col-xs-12"> 
         <div class="input-group" style="margin-bottom: 5px;"> 
          <span class="input-group-addon" id="first">First Name</span> 
          <input type="text" class="form-control" placeholder="John" name="first" aria-describedby="First-Name"> 
          <span class="input-group-addon error" style="color: red">* <?php echo $firstErr;?></span> 
         </div> 
        </div> 
       </div> 
       <div class="row"> 
        <div class="col-xs-12"> 
         <div class="input-group" style="margin-bottom: 5px;"> 
          <span class="input-group-addon" id="last">Surname</span> 
          <input type="text" class="form-control" placeholder="Smith" name="surname" aria-describedby="Surname"> 
          <span class="input-group-addon error" style="color: red">* <?php echo $lastErr;?></span> 
         </div> 
        </div> 
       </div> 
       <div class="row"> 
        <div class="col-xs-12"> 
         <div class="input-group" style="margin-bottom: 5px;"> 
          <span class="input-group-addon" id="email">Email</span> 
          <input type="email" class="form-control" placeholder="[email protected]" name="email" aria-describedby="Email"> 
          <span class="input-group-addon error" style="color: red">* <?php echo $fromErr;?></span> 
         </div> 
        </div> 
       </div> 
       <div class="row"> 
        <div class="col-xs-12"> 
         <div class="input-group" style="margin-bottom: 5px;"> 
          <span class="input-group-addon" id="notes">Notes</span> 
          <textarea class="form-control" rows="3" placeholder="Please put anything else you want to say here" name="notes" aria-describedby="Notes"> 
      </textarea> 
         </div> 
        </div> 
       </div> 
       <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="submit" name="submit" value="submit" class="btn btn-primary">Send</button> 
       </div> 
      </form> 
      </div> 
     </div> 
    </div> 
    </div> 
</div> 

文件「Submit.php

<?php 

// define variables and set to empty values 
$fromErr = $firstErr = $lastErr = $notesErr = ""; 
$from = $first_name = $last_name = $notes = ""; 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

if (isset($_POST['submit'])) { 
    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
    } else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $emailErr = "Invalid email format"; 
    } 
    } 
    if (empty($_POST["first"])) { 
    $firstErr = "Name is required"; 
    } else { 
    $first_name = test_input($_POST["first"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) { 
     $firstErr = "Only letters and white space allowed"; 
    } 
    } 
    if (empty($_POST["surname"])) { 
    $lastErr = "Surname is required"; 
    } else { 
    $last_name = test_input($_POST["surname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) { 
     $lastErr = "Only letters and white space allowed"; 
    } 
    } 
    $notes = test_input($_POST["notes"]); // this is the senders message 

    $to = "[email protected]"; // this is your Email address 
    $subject = "Website subscription from " . $first_name . " " . $last_name; //Subject line 

    $message = "First name: " . $first_name . "<br>" . "Surname: " . $last_name . "<br>" . "Email: " . $from . "<br>" . "Notes: " . $notes; 

    $headers = "From: " . $from . "\r\n"; 
// $headers .= "CC: [email protected]\n"; option to CC 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

    mail($to,$subject,$message,$headers); 
    header('Location: ./index.html#Thanks'); // redirect back to main page with 'Thanks' Modal open 
} 
?> 

我將非常感謝您的幫助!

編輯:

只是櫃面有人想知道怎麼我完全結束了,我需要的是:@leepowers答案是朝着正確的方向邁出了一大步,這使我能夠檢查是否有任何錯誤,並防止發送電子郵件,如果有的話,然後我瞭解了'會話',我將錯誤存儲在會話中(可能以非常混亂的方式),因此我可以在返回到表單時重用它們。

// Gather all errors into an array 
$errors = array($fromErr, $firstErr, $lastErr); 
// Remove any empty error messages from the array 
$errors = array_filter($errors); 
// An array with more than zero elements evaluates `true` in a boolean context 
if ($errors) { 
    $_SESSION["fromErr"] = $fromErr; 
    $_SESSION["firstErr"] = $firstErr; 
    $_SESSION["lastErr"] = $lastErr; 
    die(header('Location: ./index.html#Subscribe')); 
} else { // send email 

在我的HTML頁面,我有一些JS指導「重定向」到一個模式:

<script> 
$(document).ready(function() { 

    if(window.location.href.indexOf('#Thanks') != -1) { 
    $('#Thanks').modal('show'); 
} else if(window.location.href.indexOf('#Subscribe') != -1) { 
    $('#Subscribe').modal('show'); 
    } 
}); 
</script> 

,現在我的每個輸入在我的形式有一個專門的錯誤:

<div class="input-group" style="margin-bottom: 5px;"> 
         <span class="input-group-addon" id="email">Email</span> 
         <input type="email" class="form-control" placeholder="[email protected]" name="email" aria-describedby="Email"> 
         <span class="input-group-addon error" style="color: red">* <?php echo $_SESSION["fromErr"];?></span> 

謝謝大家的幫助。

+2

你對'$ lastErr'或'$ firstErr'沒做任何事情,所以電子郵件只是發送。嘗試添加一個條件,要求在郵件($ to,$ subject,$ message,$ headers);'之前這兩個條件都是空的。 – chris85

+0

我不確定這是否是別人的做法,但我通常會將所有錯誤追加到單個數組中,然後在檢查多個條件時檢查是否發生了任何錯誤,只需執行if(count($ errors)> 0 ){handleError(); } else {doSomething(); }' – Mike

+0

另外,'test_input()'到底做了什麼? – Mike

回答

1

代碼需要檢查任何錯誤消息並在發送電子郵件之前停止執行。在驗證檢查後插入以下內容:

// Gather all errors into an array 
$errors = array($emailErr, $firstErr, $lastErr); 
// Remove any empty error messages from the array 
$errors = array_filter($errors); 
// An array with more than zero elements evaluates `true` in a boolean context 
if ($errors) { 
    // Output error messages and exit the script. 
    die(implode("<br>\n", $errors)); 
} 

這是一個非常基本的示例。最好在腳本的頂部初始化並設置$errors數組,然後在出現錯誤時向這個數組添加錯誤消息。

+0

現在看起來如此明顯!我的代碼只是創建錯誤,然後發送電子郵件......我會看到如果我有足夠的時間在我的午休時間修改我的代碼並測試它,謝謝。 – Andy

+0

太棒了!將錯誤輸出到頁面上,這是一個輝煌的開始。是否有沒有辦法將其回到原始窗體的'錯誤'? – Andy

1

試試這個,我沒有驗證數據,你必須自己做。您的$ from也是您已發佈的代碼中的空白。

<?php 
//if "email" variable is filled out, send email 
    if (isset($_REQUEST['email'])) { 

    //Email information 
    $to = "Your_email_id"; 
    $email = $_REQUEST['email']; 
    $first_name=$_REQUEST['first']; 
    $last_name=$_REQUEST['surname']; 
    $name=$first_name.' '.$last_name; 
    $subject ="Your subject"; 
    $message = $_REQUEST['notes']; 

    //send email 
    mail($to, $subject, $message, "From:" . $email." Name:".$name); 

    //Email response 
    echo "Thank you for contacting us!"; 
    } 

    //if "email" variable is not filled out, display the form 
    else { 


    } 
?> 
+0

爲什麼OP應該這樣做?你拿出了驗證。 – chris85

+0

感謝您的回覆,但我無法讓電子郵件發送,驗證是我無法完全正確的。然而,你確實指出了$ from的錯誤,$ email應該是$ from和$ fromErr而不是$ emailErr,所以非常感謝。 – Andy