【问题标题】:AJAX Contact form - not working properlyAJAX 联系表 - 无法正常工作
【发布时间】:2015-05-11 22:37:30
【问题描述】:

我在我的网站上创建了一个简单的 AJAX、Jquery、PHP 联系表单,其中包含输入:姓名、电子邮件和消息。问题是,当您将实际电子邮件写入电子邮件字段时,消息永远不会到达我的收件箱。仅当电子邮件输入字段包含 1 个不带 @ 符号的单词时才有效。

我的 HTML:

<p class="form">Name</p>
<input type="text" name="userName" id="userName">
<p class="form">Email</p>
<input id="userEmail" type="email" name="userEmail">
<p class="form">Message</p>
<textarea id="msg" name="msg"></textarea><button onClick="sendContact();"></button>

我的 JavaScript:

function sendContact() {

    jQuery.ajax({
        url: "contact_me.php",
        data:'userName='+$("#userName").val()+'&userEmail='+
        $("#userEmail").val()+'&msg='+
        $("#msg").val(),
        type: "POST",
        success:function(){
            sendSuccess();
        },
        error:function (){}
    });
};

我的 PHP:

<?php
$to = "dusset@gmail.com";
$from = $_POST['userEmail'];
$name = $_POST['userName'];
$subject = $name . "has sent you a message from .design";
$message = $name . " wrote the following:" . "\n\n" . $_POST['msg'];
$headers = "From:" . $from;

mail($to,$subject,$message,$headers);?>

您知道可能是什么问题吗?

【问题讨论】:

标签: php jquery ajax


【解决方案1】:

尝试更改将 数据 传递给 Ajax 的方式。一个例子:

function sendContact() {
    jQuery.ajax({
        url: "contact_me.php",
        data: { userName: $("#userName").val(), userEmail: $("#userEmail").val(), msg:  $("#msg").val()},
        type: "POST",
        success:function(){
            sendSuccess();
        },
        error:function (){}
    });
};

【讨论】:

  • 这将正确编码帖子值,这是最初的问题。 data 应该是对象、字符串或数组,it is converted to a query string, if not already a string
猜你喜欢
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多