如何添加PHP验证到我的联系人

如何添加PHP验证到我的联系人

问题描述:

我用css,html,javascript和一个小小的php做了一个网站。我把它托管在托管网站上,但是我在表单的服务器端验证方面存在问题。如何添加PHP验证到我的联系人

这只是一个简单的联系形式:

名 姓 电子邮件 消息(文本域) 提交按钮

我有JavaScript验证,它工作正常。除非输入了正确数量的字符,否则表单根本不会提交,但显然,如果用户禁用了js,则该表单无用。我也终于得到了表格,通过电子邮件发送给我的个人电子邮件...但显然我需要服务器端验证。

今天是我第一天真的进入php,我希望今天完成php验证,但是我观看的所有视频中,我只是更加困惑。所有我想要做的只是让它,以便如果用户已禁用JavaScript无论出于何种原因,或者如果他将字段留空,表单将不会提交...

Anyeay,我正在研究这一切今天的一天,打算让表格发送到我的电子邮件,我终于完成了。然后意识到我没有服务器端验证。我花了几个小时研究它,并尝试它无济于事。任何人都可以给我上面描述的那种形式的php验证代码吗?这是我的企业网站,所以越早我可以有一个工作联系表格,更好的...

这是我做的html联系表。

<form action="message_sent.php" method="POST"  onSubmit="return validateTextbox();"> 

<p class="message"> 
<div class="row"> 

<label for="firstname"><!--First Name (Required):--> </label> 
<input type="text" id="firstname" name="first_name" size="40" placeholder="First Name (Required)"></br> </br> </div> 


<div class="row"> 

<label for="lastname"><!--Last Name (Required):--> </label> 
<input type="text" id="lastname" name="last_name" size="40" placeholder="Last Name (Required)"/> </br> </br></div> 

<div class="row"> 
<label for="email"><!--Your Email (Required):--></label> 
<input type="email" id="email" name="email" size="40" placeholder="Email (Required)"/> </br> </br></div> 

<!--<p class="yourMessage">Your Message (10 Character Minimum):</p>--> 
<textarea id="theform" rows="30" cols="80" name="message" placeholder="Your Message (10 Character Minimum):"></textarea></br> 

</p> 

<input type="submit" name="submit" onclick="return val();" value="SUBMIT"> 

</form> 

</body> 
</html> 

这里是JavaScript验证:

/*============================ CONTACT US PAGE ==========================*/ 

function validateTextbox() { 


var box = document.getElementById("firstname"); 
var box2 = document.getElementById("lastname"); 
var box3 = document.getElementById("email"); 
var box4 = document.getElementById("theForm"); 



if (box.value.length < 1 || box2.value.length < 1 || box3.value.length < 5){ 
alert("You must enter a value");  

box.focus(); 
box.style.border = "solid 3px red"; 
    box2.focus(); 
    box2.style.border = "solid 3px red"; 
     box3.focus(); 
     box3.style.border = "solid 3px red"; 


    return false; 
    } 


} 



     function val(){ 

      if(document.getElementById("theform").value.length < 10 
     && document.getElementById("theform").value.length > 0){ 

      alert("You must enter at least 50 characters. Tell us what you need so we can better assist you."); 
      return false; 

     } 


     else if(document.getElementById("theform").value.length === 0){ 

      alert("You cannot submit an empty form."); 
      theform.focus(); 
      theform.style.border = "solid 3px red"; 
      return false; 
     } 



    } 


      /*function val(){ 

     if(document.getElementById("theform").value==null || document.getElementById("theform").value==""){ 
      alert("The Message field cannot be blank."); 

      return false;            
    } 


     */ 




    /* 



*/ 




/*=======================|| box4.value.length < 70) ================================================ */ 


    /*========= CONTACT PAGE========================================*/ 

    /* 
function contactPage(){ 


alert("This Contact Form is NOT OPERATIONAL."); 

这里是PHP提交成功页面...我曾经有过的形式发送到我的邮箱代码:

<?php 



$first_name=$_POST['first_name']; 
$last_name=$_POST['last_name']; 
$email=$_POST['email']; 
$message=$_POST['message']; 

$to="[email protected]"; 
$subject="A visitor has sent you a new message"; 
$body="You have received a message from: \n\n 
First Name: $first_name\n 
Last Name: $last_name\n 
Email: $email\n\n 
MESSAGE: $message"; 

mail($to, $subject, $body); 

print "<p>Message Sent! <a href='index.html'>Click.  here</a> to return to the homepage</p>" 


?> 
+1

请发表您的POST代码,并显示到目前为止你已经尝试过的情况。我们可以纠正并加强它。 –

这只是一个基本的空检查验证。

PHP中的验证并不是非常困难。

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if(trim($_POST['firstname']) === ''){ 
     echo 'Please enter firstname'; 
    } 
} 

您还可以了解一些验证这里 http://www.w3schools.com/php/php_form_validation.asp

+0

好的,我在第一篇文章中添加了我的代码。这是html,javascript和pbp代码 – Shutter

简单而完整的例子:

<!DOCTYPE HTML> 
<html> 
    <head> 
     <style> 
      .error {color: #FF0000;} 
     </style> 
    </head> 
    <body> 
     <?php 

      //Define variable as empty to avoid "undefined variable error". 
      $FnameErr = $LnameErr = $emailErr = ""; //Each variable contain error string of specific field. 
      $fname = $lname = $email = $message = ""; //Main inputed data of specific field 


      if ($_SERVER["REQUEST_METHOD"] == "POST") { //check if request is posted. 

       //First name check 
       if (empty($_POST["fname"])) {   // check if empty then assign a error string in spacific variable 
       $FnameErr = "First Name is required"; 
       }else{         // if not empty then store as right data 
       $fname = filter_data($_POST["fname"]); //filter with unexpected special char and trim. 
       } 

       //Last name 
       if (empty($_POST["lname"])) {   // check if empty then assign a error string in spacific variable 
       $LnameErr = "Last Name is required"; 
       }else{         // if not empty then store as right data 
       $lname = filter_data($_POST["lname"]); //filter with unexpected special char and trim. 
       } 

       //email 
       if (empty($_POST["email"])) {   // check if empty then assign a error string in spacific variable 
       $emailErr = "Email is required"; 
       } else { 
       if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // validate email with php build-in fucntion. 
        $emailErr = "Invalid email format"; 
       }else{            // if not empty and valide email then store as right data 
        $email = filter_data($_POST["email"]);   //filter with unexpected special char and trim. 
       } 
       } 

       //message with no validation 
       if (empty($_POST["message"])) { 
       $message = ""; 
       } else { 
       $message = filter_data($_POST["message"]); 
       } 


       //Database query 
       if($FnameErr =="" && $LnameErr =="" && $emailErr==""){ 
       //MYSQL insert statement that you know 
       // $sql = "INSERT INTO tablename .................."; values = $fname; $lname; $email; $message; 
       } 

      } 


      // A function to trim data and remove special charecter 
      function filter_data($data) { 
       $data = trim($data); 
       $data = stripslashes($data); 
       $data = htmlspecialchars($data); 
       return $data; 
      } 
      ?> 


     <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
      First Name: <input type="text" name="fname" value=""> 
      <span class="error">* <?php echo $FnameErr;?></span><br><br> 

      Last Name: <input type="text" name="lname" value=""> 
      <span class="error">* <?php echo $LnameErr;?></span><br><br> 

      E-mail: <input type="text" name="email" value=""> 
      <span class="error">* <?php echo $emailErr;?></span> 
      <br><br> 

      Message: <textarea name="message" rows="5" cols="40"></textarea> 
      <br><br> 

      <input type="submit" name="submit" value="Submit"> 
     </form> 

     <?php 
      echo "<h2>Your Input:</h2>"; 
      echo $fname; 
      echo "<br>"; 

      echo $lname; 
      echo "<br>"; 
      echo $email; 
      echo "<br>"; 
      echo $message; 
     ?> 
    </body> 
</html> 
+1

如果您对此处的每个循环入口点都有规范的解释,那么对于初学者和其他人来说,这个答案会变得更加丰富。 – zanderwar

+0

谢谢@Zanderwar,我必须尝试。 – AHJeebon