2017-08-11 74 views
-1

我剛開始學習PHP,並試圖使用PHP創建一個響應聯繫表單。 但問題是,我的PHP代碼被打印,因爲它是在網頁上,因爲它是,而不是所需的格式,因爲我需要它。儘可能打印PHP代碼

這裏是我的代碼:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Contact Us</title> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/start/jquery-ui.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery- 
ui.min.js"></script> 
</head> 
<body> 
<!-- <img src="call.jpg" id="background"> --> 
    <div id="container-fluid"> 
     <div class="row"> 
      <div class="col-sm-offset-1 col-sm-10 contactform"> 
       <h1>Contact Us:</h1> 
       <?php 
        $name = $_REQUEST["name"]; 
        $email = $_REQUEST["email"]; 
        $message = $_REQUEST["message"]; 
        $submit = $_REQUEST["submit"]; 
        $to = "[email protected]"; 
        $var = rand(1,1000); 
        $subject = 'Complaint Registered. Complaint No.: '. echo "$var"; 
        if($submit) 
        { 
         if(!$name) 
          $errors = $errors."<p><strong>Name missing!</strong></p>"; 
         else 
          $name = filter_var($name,FILTER_SANITIZE_STRING); 
         if(!$email) 
          $errors = $errors."<p><strong>Email missing!</strong></p>"; 
         else 
         { 
          $name = filter_var($email,FILTER_SANITIZE_STRING); 
          if(!filter_var($email,FILTER_VALIDATE_EMAIL)) 
          { 
           $errors = $errors."Please give a valid Email-Address!"; 
          } 
         } 
         if(!$message) 
         { 
          $errors = $errors."<p><strong>Message box can't be empty!</strong></p>"; 
         } 
         else 
         { 
          $message = filter_var($message,FILTER_SANITIZE_STRING); 
         } 
         if($errors) 
         { 
          $finalmsg = "<div class='alert alert-danger'> ".$errors "</div>"; 
         } 
         else 
         { 
          $date = date('d MM YY'); 
          $content = "Hi $name. Thank you for your complaint. Your complaint has been registered on $date and your complaint number is: $var"; 
          if(mail($to, $subject, $content)) 
          { 
           $finalmsg = '<div class="alert alert-success">Your mail has been sent and we will REQUEST back to you asap</div>'; 
          } 
          else 
          { 
           $finalmsg = '<div class="alert alert-warning">Error Sending Mail. Try again later!</div>'; 
          } 
         } 
         echo $finalmsg; 
        } 
       ?> 
       <form action="" method="post"> 
        <div class="form-group"> 
         <label for="name">Name &#42: </label> 
         <input type="text" name="name" id="name" placeholder="Enter your Name " class="form-control"> 
        </div> 
        <div class="form-group"> 
         <label for="email">Email &#42:</label> 
         <input type="email" name="email" id="email" placeholder="Enter your Email " class="form-control"> 
        </div> 
        <div class="form-group"> 
         <label for="message">Message &#42:</label> 
         <textarea id="message" name="message" class="form-control" rows="5"></textarea> 
        </div> 
        <input type="submit" name="submit" class="btn btn-success btn-lg" value="Send Message" id="submit"> 
       </form> 
      </div> 
     </div> 
    </div> 
</body> 

另外,如果你幫我在生成隨機數,並在我的代碼中使用它,將是巨大的。我在我的代碼中試過了,請告訴我它是否正確。

謝謝

+6

你的文件是否有.php擴展名? PHP是否安裝並在Web服務器上運行? – ADyson

+0

至於你關於你的隨機數發生器的問題,你認爲什麼是「正確的」?描述你期望它如何表現。那麼從你的測試中告訴我們,它是否符合你的期望?如果是這樣,那麼它可能是正確的你的目的。如果沒有,請告訴我們它做了什麼。 – ADyson

+0

在wamp服務器上運行它我在使用windows –

回答

0

這是頁面由Web服務器提供服務和它有一個.php擴展?

PHP是一個預處理器,這意味着它需要由Web服務器運行。嘗試使用WAMP(Windows)或MAMP(對於Mac),將您的文件重命名爲.php,然後重試。

+0

是的,我的文件有一個有效的.php擴展名。 –