0

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.

  • I have also tried .val() instead of .serialize() as shown below but no luck.
  • I have also done data: {name:name, email:email...} as well as data{'name':name, 'email':email...}

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

32
  • 3
    did you check the network and that it have any errors? Commented Sep 19, 2016 at 2:33
  • 1
    @Howzieky he said it works in localhost so it means he already include it. Commented Sep 19, 2016 at 2:51
  • 1
    @JohnDoe did you check your network does it have any error? Commented Sep 19, 2016 at 2:52
  • 1
    Is there any errors in the console? Commented Sep 19, 2016 at 4:08
  • 1
    @Howzieky OP appears to be using ES6 object literal shorthand which is supported by modern browsers so {name, email, textarea, business, website} is actually valid Commented Sep 19, 2016 at 5:30

4 Answers 4

1

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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)

2 Comments

In place of postData I would enter name, email, etc.. correct?
1) To send a JSON payload, the request headers should contain Content-type: application/json. 2) OP would need to change their PHP code to read the JSON payload from php://input. This is not compatible with OP's code that expects application/x-www-form-urlencoded data
0

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);
    }
});

2 Comments

I assume OP's browser supports ES6 object literal shorthand so this is no different
As @Phil says and also I've tried that before, no luck.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.