0

Im having difficulties when parsing an array using Ajax to PHP to send an email with the values from the array.

Ajax code:

    $(document).ready(function(){

            $("#submit-button").click(function(){

                var countryArray = ['Location Zero', 'Location One', 'Location Two'];

                dataString = countryArray; 
                var jsonString = JSON.stringify(dataString);

                $.ajax({
                        type: "POST",
                        url: "sendmail.php",
                        data: {countries: jsonString},
                        success: function (msg) {

                            $("#errors").text("Thank you for getting in touch, we will get back to you!");

                        },
                        error: function (msg) {
                            $("#errors").text("Error sending email, please try again.");

                            alert("error");
                        }
                    });


});

});

PHP code:

<?php


        $to = "[email protected]";
        $countries = json_decode($_POST['countries']);

        $header = "Content-Type: text/html\r\nReply-To: \r\nFrom:  <>";
        $subject = "Email from the Lister customer";

        $body = @"$countries";


        if(mail($to, $subject, $body, $header)) {
            die("true");    
        } else {
            die("There was an error sending the email.");   
        }


?>

But all I'm getting with in the email from $countries is word "Array" instead of the values.

Can anyone help please?

4
  • $countries is an array so use implode(", ", $countries); to print it as a string. Commented Jun 11, 2013 at 14:41
  • @"$countries"? don't suppress errors, and don't use cargo-cult programming... Commented Jun 11, 2013 at 14:57
  • thanks you guys, that all works perfectly now :) Commented Jun 11, 2013 at 15:20
  • possible duplicate of PHP: Implode array values? Commented Jun 11, 2013 at 20:51

3 Answers 3

3

$countries is an array. If you want it to be displayed as a list in your $body, you can do:

$body = implode(', ', $countries);

Please also try not to suppress (@) PHP errors, it'll cause you more headaches in the future.

Sign up to request clarification or add additional context in comments.

Comments

0
<?php


    $to = "[email protected]";
    $countries = json_decode($_POST['countries']);

    $header = "Content-Type: text/html\r\nReply-To: \r\nFrom:  <>";
    $subject = "Email from the Lister customer";

    $body = implode(", ", $countries);


    if(mail($to, $subject, $body, $header)) {
        die("true");    
    } else {
        die("There was an error sending the email.");   
    }
?>

1 Comment

@YogeshSuthar I know, it just wouldn't let me accept it straight away. I had to wait a while. But all done now :)
0

If you're using jquery, try using .serializeArray() instead of stringify.

Also, when receiving the $_POST['contries'] variables, you need to implode it. Try this:

$(document).ready(function(){
    $("#submit-button").click(function(){
        var countryArray = ['Location Zero', 'Location One', 'Location Two'];
        $.ajax({
            type: "POST",
            url: "sendmail.php",
            data: {countries: countryArray.serializeArray()},
            success: function (msg) {
                $("#errors").text("Thank you for getting in touch, we will get back to you!");
            },
            error: function (msg) {
                $("#errors").text("Error sending email, please try again.");
                alert("error");
            }
        });
    });
});

And then in PHP use this to properly grab the countries values:

implode(', '.$countries);

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.