2016-03-17 84 views
0

我已經創建了一個網頁的HTML格式,其中只包含3個字段名稱,電子郵件和消息和一個按鈕。第二頁的PHP發送郵件命名爲status.php.but當我點擊發送郵件按鈕,它不重定向到status.php而不是還會下載page.why這是happening.Anybody可以告訴.. 這裏是代碼:發送電子郵件在PHP中從HTML表格

<form method="post" action="status.php"> 
    <ol> 
     <li> 
     <label for="name">Name (required)&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</label> 
     <input id="name" name="name" /> 
     </li> 
     <br /><li> 
     <label for="email">Email Address (required)&nbsp&nbsp&nbsp&nbsp;</label> 
     <input id="email" name="email" /> 
     </li> 
     <br /><li> 
     <label for="message">Your Message&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</label> 
     <textarea id="message" name="message" rows="8" cols="50"></textarea> 
     </li> 
     <br /><li> 
      <input type="submit" name="submit" value="Send Email"/> 
     </li> 
    </ol> 
</form> 

PHP代碼:

<?php 
    if(isset($_POST['submit'])) 
    { 
     $to = '[email protected]'; 
     $subject = 'Email Test'; 
    } 
    $status = 'Name: ' .$_POST['name']. "\r\n\r\n"; 
    $status .= 'Email: ' .$_POST['email']. "\r\n\r\n"; 
    $status .= 'Message: ' .$_POST['message']; 
    echo $status; 
?> 
+0

你可以嘗試添加onSubmit屬性到你的窗體,

+4

你應該使用Absolute路徑執行例如。 http://localhost/first.html如果您使用相對路徑eg.file:///var/www/html/first.html,那麼文件下載 – WisdmLabs

+0

^yup。接得好。我錯過了'下載'這個詞 – fusion3k

回答

0

如果您想在同一頁面中完成所有功能並參考以下內容:

的index.php

<?php 
    if(isset($_POST['submit'])) 
     { 
      $to = '[email protected]'; 
      $subject = 'Email Test'; 
     } 
     $status = 'Name: ' .$_POST['name']. "\r\n\r\n"; 
     $status .= 'Email: ' .$_POST['email']. "\r\n\r\n"; 
     $status .= 'Message: ' .$_POST['message']; 
     echo $status; 
    ?> 

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
     <ol> 
      <li> 
      <label for="name">Name (required)&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</label> 
      <input id="name" name="name" /> 
      </li> 
      <br /><li> 
      <label for="email">Email Address (required)&nbsp&nbsp&nbsp&nbsp;</label> 
      <input id="email" name="email" /> 
      </li> 
      <br /><li> 
      <label for="message">Your Message&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</label> 
      <textarea id="message" name="message" rows="8" cols="50"></textarea> 
      </li> 
      <br /><li> 
       <input type="submit" name="submit" value="Send Email"/> 
      </li> 
     </ol> 
    </form> 
+0

Thanx ....但它仍然下載該頁面.... –

+0

oops ..現在你需要檢查你的服務器,如果它支持PHP。 –

0

確保您設置了一個服務器在本地機器上,例如LAMP(Linux操作系統,Apache,MySQL和PHP)或XAMPP運行Windows的機器,並嘗試與

http://localhost/path/to/your/script

你的文件需要被定位在 'htdocs目錄' 的目錄中運行腳本時,你的服務器(除非你做了一些更多的配置,並改變了路徑)