简体   繁体   中英

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. 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. 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. 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

 $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

<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.

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

Making this change allowed the PHP script to read the submitted form.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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