简体   繁体   English

如何使javascript联系表单正确提交

[英]How to make javascript contact form submit correctly

html code: Contact Me html代码:与我联系

                <label for="Name">Name:</label>
                <input type="text" name="name" id="Name" accesskey="N" tabindex="1">

                <label for="Email">E-mail:</label>
                <input type="text" name="email" id="Email" accesskey="E" tabindex="1">

                <label for="Phone">Phone Number:</label>
                <input type="text" name="number" id="Number" tabindex="1">

                <label for="Comment">Comments</label>
                <textarea type="text" name="comment" id="Comment" rows="27" cols="70" tabindex="1"></textarea>

                <input id="mySubmit" type="submit" value="Submit">
            </form>
    </fieldset>
</div>

email.php email.php

<?php

if (isset($_POST['submit'])) {

  $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

  if (!$email)
    echo "<script type='text/javascript'>alert('Please enter a valid email address...');history.back();</script>";

  else {
    $to = "randomemail@gmail.com"; //change this to YOUR email address

    $name = (isset($_POST['name'])) ? $_POST['name'] : "anonymous";
    $number = (isset($_POST['number'])) ? $_POST['number'] : "none";
    $comment = (isset($_POST['comment'])) ? $_POST['comment'] : "none";

    $subject = "Message from $name via contact form";

    $message = "Name: $name\nNumber: $number\nEmail: $email\nMessage: $comment";

    $from = "From: " . $name . "<" . $email .">\r\n" .
            "Reply-To: " . $email ."\r\n" .
            "X-Mailer: PHP/" . phpversion();

    if (mail($to, $subject, $message, $from))
      header("Location: thanks.html");

    else
      echo "<script type='text/javascript'>alert('An unknown system error has occurred!');history.back();</script>";
  }
}

?>

when you submit, it loads email.php, but only a white page, not the thanks.html that it should. 提交时,它将加载email.php,但仅加载白页,而不加载该页面。

You forgot to name your submit field: 您忘记命名您的提交字段:

            <input id="mySubmit" type="submit" value="Submit" name="submit">
                                                              ^^^^^^^^^^^^^

Don't use form fields to detect a post. 不要使用表单字段来检测帖子。 Use the 100% reliable: 使用100%可靠:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... }

instead. 代替。 This will ALWAYS be true if a POST was performed. 如果执行了POST,则始终为true。 As you can see with your version, a simple typo or oversight will completely kill your logic. 正如您在版本中看到的那样,简单的错字或疏漏将完全破坏您的逻辑。

First Empty your file and just put header("Location: thanks.html"); 首先清空您的文件,然后放入header("Location: thanks.html"); in your php tags. 在您的php标记中。 If it works then add the other lines progressively. 如果可行,则逐步添加其他行。 you'll see the annoying line. 您会看到烦人的行。 Read about header on PHP reference website. 在PHP参考网站上了解有关标头的信息。 It should be used carefully 应该小心使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM