Javascript提交不发送POST到php文件

Javascript Submit is not Sending POST to php file

本文关键字:php 文件 POST 提交 Javascript      更新时间:2023-09-26

我有麻烦让我的javascript超链接按钮发布数据到我的php表单电子邮件脚本。我试过几种不同的方法,但似乎都不起作用。任何帮助都是感激的。谢谢你。

我的表单代码:

<form id="form" action="contactform.php" method="POST" enctype="text/plain" >
      <fieldset>
        <label><strong>Your Name:</strong><input id="name" type="text" name="name">       </label>
        <label><strong>Your Phone Number:</strong><input id="phone" type="text" name="phone"></label>
        <label><strong>Your E-mail:</strong><input id="email" type="text" name="email"></label>
        <label><strong>Your Message:</strong><textarea id="message" name="message"></textarea></label>
        <div class="btns"><a href="contacts.html" class="button">Clear</a><a href="#" onclick="document.getElementById('form').submit();" class="button">Send</a></div>
      </fieldset>  
    </form>

Contactform.php:

<?php
    var_dump($_POST);
    if (isset($_POST['form'])) {

    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    $email_from = 'emailaddress@gmail.com';
    $email_subject = "Contact Form Submission";
    $email_body = "Name: $name'n" . "Phone: $phone'n" . "Messge: $message";

    $to= 'emailaddress@gmail.com';
    $headers= "From: $email_from 'r'n";
    $headers.= "Reply-To: $visitor_email 'r'n";
    mail($to,$email_subject,$email_body,$headers);
    echo "Thank you for your interest. I will contact you shortly.";
    }else{
    echo "It didn't work.";
    }

    ?>

此外,我在php的开头放置了var_dump,仅用于调试目的。不会成为最终代码的一部分

enctype="text/plain"从表单标签中移除,PHP不支持。

看到:方法="post"enctype ="文本/plain"不兼容?

虽然我知道这并不能回答你的问题,但是没有理由为此使用Javascript(在上面的例子中),因为HTML输入元素会自动处理这两个函数。

<input type="reset" value="Clear" />
<input type="submit" value="Send" />

我只会做这样的事情来验证数据,然后我会使用表单的onsubmit动作(如果验证失败返回false)。

a标签替换为如下所示

<a href="Javascript:void(0);" 

并替换document.forms["myForm"].submit();

和添加name="myForm"到您的表单。

完整代码

<form id="form" action="contactform.php" method="POST" name="myForm">
    <fieldset>
        <label><strong>Your Name:</strong><input id="name" type="text" name="name">       </label>
        <label><strong>Your Phone Number:</strong><input id="phone" type="text" name="phone"></label>
        <label><strong>Your E-mail:</strong><input id="email" type="text" name="email"></label>
        <label><strong>Your Message:</strong><textarea id="message" name="message"></textarea></label>
        <div class="btns">
            <a href="contacts.html" class="button">Clear</a>
            <a href="Javascript:void(0)" onclick="document.forms['myForm'].submit();" class="button">Send</a></div>
    </fieldset>  
</form>

可以:

<a href="#" onclick="document.form.submit();" class="button">Send</a>

您不需要getElementById,您可以简单地以这种方式引用它。

我测试了这个,它工作....

<html>
<body>
<form id="form" action="contactform.php" method="POST">
      <fieldset>
        <label><strong>Your Name:</strong><input name="name" type="text" name="name">       </label>
        <label><strong>Your Phone Number:</strong><input name="phone" type="text" name="phone"></label>
        <label><strong>Your E-mail:</strong><input name="email" type="text" name="email"></label>
        <label><strong>Your Message:</strong><textarea name="message" name="message"></textarea></label>
        <div class="btns"><a href="contacts.html" class="button">Clear</a><a href="#" onclick="document.forms[0].submit();" class="button">Send</a></div>
      </fieldset>  
    </form>
</body>
</html>

<?php
var_dump($_REQUEST);
echo  "<BR>";
foreach ( $_POST as $key => $value )
{
    echo $key . " " . "=" . " " . $value;
    echo  "<BR>";
}
?>

您可以考虑的其他一些提示是:

  • 不要使用<strong>。相反,使用CSS类,就像使用超链接一样。示例label { font-weight:bold }<label class='strong'>
  • 考虑使用jquery和不显眼的javascript。示例$("form").first().on("submit",function...
  • 不要在输入上应用标签。简单地关闭标签标签,然后使用输入,或者使用<label for="input1">来匹配输入,并在单击文本时启用复选框。
  • 指定一个name属性,因为这是发送到服务器的post…该id用于在浏览器中引用脚本