John Doe
John Doe

Reputation: 305

Why is PHP not retrieving data sent by AJAX?

So I'm trying to make a contact form with a button, not submit button, that runs a function when clicked onClick = 'send()' and on localhost w/ WAMP it works perfectly sending the email with its contents. On a live version, the connection is well and I receive the email but I don't get the respective form field data even though it's the same code I used when testing on WAMP server. Also, in the top of the php file I echoed the POST data and on localhost it would say the information but on live, again, it doesn't.

Any help would be appreciated!

Heres the AJAX:

function send(){

    var name = $("input[name=name]").serialize();
    var email = $("input[name=email]").serialize();
    var textarea = $("textarea").serialize();
    var business = $("input[name=business]").serialize();
    var website = $("input[name=website]").serialize();

    alert(name);
    alert(email);
    alert(textarea);
    alert(business);
    alert(website);

        $.ajax({ 
            url: 'sendmail.php',
            type: 'POST',
            data: {name, email, textarea, business, website},
            success: function(mydata) {
                alert(mydata);
            }
        });

    }

and heres the PHP:

<?php

        echo($_POST['name']);
        // does show as output on localhost w/wamp but on live website its mute

        $message=
        'Full Name: '.$_POST['name'].'<br />
        Email:  '.$_POST['email'].'<br />
        Message: '.$_POST['textarea'].'<br />
        Current Website: '.$_POST['website'].'<br />
        Business Name: '.$_POST['business'].'<br />
        ';

        /* Email Sending Script */

        if (!$mail->send()) {
            echo $mail->ErrorInfo;
        } else {
            die("true");    
        }

?>

NETWORK TABS

Request Headers: http://prntscr.com/ck0btg

Post Data: http://prntscr.com/ck0cni

Upvotes: 0

Views: 121

Answers (4)

John Doe
John Doe

Reputation: 305

So apparently the .htaccess file from the previous website I had is what caused the problem. Once I deleted it, it worked. I think its because I made it so all the content of the site was to be kept for 1 month (script from .htaccess) and that's what caused it not to send.

Upvotes: 0

Haresh Vidja
Haresh Vidja

Reputation: 8496

use .serialize() method if you want to post all fields of Form alternatively In your code you have to pass data with key index, so you can get value of data using that key in your server side script

$.ajax({
    type: "POST",
    url: "sendmail.php",
    dataType: "JSON",
    data: $("Selector_of_Form").serialize(),
    success: function(mydata){
        alert(mydata);
    }
});

Upvotes: 1

Abhishek Dhanraj Shahdeo
Abhishek Dhanraj Shahdeo

Reputation: 1356

Try something like this:

$.ajax({
    type: "POST",
    url: "sendmail.php",
    dataType: "JSON",
    data: {"name":name, "email":email, "textarea":textarea, "business":business, "website":business},
    success: function(mydata){
        alert(mydata);
    }
});

Upvotes: 0

Rebecca Close
Rebecca Close

Reputation: 940

Try encoding your data as JSON before you send it.

$.ajax({
    type: "POST",
    url: "sendmail.php",
    dataType: "JSON",
    data: $.toJSON(postData),
    success: function(mydata){
        alert(mydata);
    }
});

Alternately, JSON.stringify(postData)

Upvotes: 0

Related Questions