从html表单发送php中的电子邮件

时间:2016-03-17 06:04:03

标签: php email

我创建了一个html格式的网页,其中只包含3个字段名称,电子邮件和邮件以及一个按钮。用于发送名为status.php的邮件的第二页php。但是当我点击发送邮件按钮时它不会重定向到status.php而不是下载那个页面。为什么会发生这种情况。任何人都可以告诉.. 这是代码:

<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 = 'jafar.nadaf@ajinkyatechnologies.com';
        $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;
?>

2 个答案:

答案 0 :(得分:0)

如果您想在同一页面中执行所有功能并参考以下内容:

<强>的index.php

<?php
    if(isset($_POST['submit']))
        {
            $to = 'jafar.nadaf@ajinkyatechnologies.com';
            $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>

答案 1 :(得分:0)

确保在本地计算机上设置服务器,例如LAMP(Linux,Apache,MySQL和PHP)或XAMPP在运行Windows机器并尝试使用

运行脚本时
  

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

您的文件需要位于服务器的“htdocs”目录中(除非您进行了更多配置并更改了该路径)