0

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>
  • Is the callback function writing the response in any div for example? – Ricardo Ortega Magaña Nov 03 '16 at 21:07
  • 2
    `echo back any of the posted data` You __echo nothing__ in your php. What do you expect? – u_mulder Nov 03 '16 at 21:08
  • Not sure exactly what you mean, but the callback function simply alerts "Thanks for contacting us!" Yes, the callback function works. –  Nov 03 '16 at 21:09
  • You might want to post the complete PHP file, since you are not echo nothing, and the callback funcion, should "read" the php data, and put it somewhere in the document – Ricardo Ortega Magaña Nov 03 '16 at 21:11
  • @u_mulder sorry, ive been messing with it so much i took the echos out. It doesnt echo anything at all. If i echo just the post it says "Array," but when i try to access the array nothing happens –  Nov 03 '16 at 21:12
  • Please, red this post: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – Ricardo Ortega Magaña Nov 03 '16 at 21:13
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Nov 03 '16 at 21:30
  • You cannot echo an array in PHP. There are many methods to process an array in PHP, you're likely wanting to use `json_encode()` – Jay Blanchard Nov 03 '16 at 21:31
  • Alright guys, I've added the HTML form as well. I just don't know what is going on. I am using a localhost. Jquery is working fine. There are no errors reported. I checked my php.ini file to make sure errors were turned on. –  Nov 03 '16 at 23:21
  • print_r($_POST) returns Array() –  Nov 03 '16 at 23:25
  • when i try to access the array using foreach($array as $key => $value) nothing appears at all –  Nov 03 '16 at 23:33

2 Answers2

0

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.

0

Remove option "contentType: false" from ajax post.

Puneet
  • 468
  • 9
  • 17