简体   繁体   English

PHP没有阅读我的Ajax帖子。 没有错误,只是空白文件

[英]PHP is not reading my Ajax post. No errors, just blank file

I know this is only a simple post to a php file through Ajax. 我知道这只是通过Ajax发布到php文件的简单文章。 It is something I have done before, but there must be something I am missing this time. 这是我以前做过的事情,但是这次一定有我想念的事情。 I cannot figure out why my PHP file wont read or echo back any of the posted data. 我无法弄清楚为什么我的PHP文件不会读取或回显任何发布的数据。 The PHP code works fine when it is in the same file as the form, but when I move the PHP file to an external source, it ceases to work. 当PHP代码与表单位于同一文件中时,它可以正常工作,但是当我将PHP文件移至外部源时,它将停止工作。 All the data shows up in header, but it is not being read. 所有数据都显示在标头中,但未被读取。

Request Payload 请求有效载荷

name=Form+Name&email=myemail%40email.com&tel=2345557777&web=http%3A%2F%2Fmywebsite.com&msg=This+is+the+message Name

Form data (the call back doesnt return any data) 表单数据(回调不返回任何数据)

contact.on('submit', function(){
        var contactData = contact.serialize();
        console.log(contactData);
        return ajaxPost('mail.php', 'POST', contactData, (data) => {
            console.log(data);
            // Contact form callback
            alert('Thanks for contacting us!');
        });
    });

Ajax Post 阿贾克斯邮报

var ajaxPost = function (x, y, z, callback){
    $.ajax({
        url: x,
        type: y,
        data: z,
        // encode: true,
        processData: true,
        contentType: false,
        dataType: 'html'

    }).done(() => {
        callback();
    });
    event.preventDefault();
};

PHP File PHP文件

 $name = $_POST['name'];
        $email = $_POST['email'];
        $tel = $_POST['tel'];
        $msg = $_POST['msg'];
        $web = $_POST['web'];
        $admin_email = "myemail@email.com";
        mail($admin_email, 'Name: ' . $name . " Email: " . $email, ' '. "Message: " . $msg . " Website: " . $web);
echo $name . $email . $tel;

HTML form HTML表格

<div class="form col-lg-6 col-md-6 col-sm-6 col-xs-12 text-center animated slideInDown">
        <form id="contact" name="contact-form" action="" method="post" datatype="multipart/form-data">
            <h3 class="text-center">Drop us a line</h3>
            <fieldset>
                <input placeholder="Your name" name="name" type="text" tabindex="1" required autofocus>
            </fieldset>
            <fieldset>
                <input placeholder="Your Email Address" name="email" type="text" tabindex="2" required>
            </fieldset>
            <fieldset>
                <input placeholder="Your Phone Number (optional)" name="tel" type="text" tabindex="3" required>
            </fieldset>
            <fieldset>
                <input placeholder="Your Web Site (optional)" name="web" type="text" tabindex="4">
            </fieldset>
            <fieldset>
                <textarea placeholder="Type your message here...." name="msg" tabindex="5" required></textarea>
            </fieldset>
            <fieldset>
                <button type="submit" id="contact-submit">Submit</button>
            </fieldset>
            <a id='number-attn' class="glyphicon glyphicon-phone" href="tel:5555555555">&nbsp;5555555555</a>
        </form>
    </div>
</div>

I'm still not sure why the other way was not working, but I got my PHP file to talk back by using the FormData object. 我仍然不确定为什么其他方法不起作用,但是我使用FormData对象让我的PHP文件可以回话。

        var contactData = new FormData(contact[0]);

Making this change allowed the PHP script to read the submitted form. 进行此更改允许PHP脚本读取提交的表单。

从ajax发布中删除选项“ contentType:false”。

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

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