0

i tried here a lot of options to send data from jQuery to php but without success, i'm trying to send an array of objects like here:

$.ajax({
            type: "POST",
            contentType: 'application/json',
            url: "sendCartDetails.php",
            data: {
                'details': prod
            }
    });

where prod has this structure:

prod: [{name: 'Some Name'}, {name: 'Other Name'}]

and my php file:

$data = json_decode($_POST['details']);

$to = "email@email.com";
$subject = "New Order";
$headers = "From: Some ";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $data . "</td></tr>"
$message .= "</table>";
$message .= "</body></html>";

mail($to,$subject,$message,$headers);

and data here it's undefined also had the 500 (Internal Server Error) when trying to send it. How to get the data correctly here? Thanks a lot

ConstantinEx
  • 160
  • 3
  • 16

3 Answers3

0

try prod:[{"name": "Some Name"}, {"name": "Other Name"}]

Wasif Khan
  • 956
  • 7
  • 25
0

You can not send emails from localhost(your local system), unless you are using 3rd party email solution This Line : mail($to,$subject,$message,$headers); will work on server but on local machine, it will result a 500 server error

Gaurav Garg
  • 137
  • 1
  • 9
  • Sending that not from localhost, this is an real server – ConstantinEx Jul 06 '17 at 11:07
  • Haha, ok Then First of all, use real json , [{name: 'Some Name'}, {name: 'Other Name'}] is not json, and if you have a javascript array, You have to convert your array in json , please check this - https://stackoverflow.com/questions/2295496/convert-array-to-json – Gaurav Garg Jul 06 '17 at 11:11
0

I believe that solution for your problem lies on server side, not on client. Also you should send data to server properly like (something like this):

data: {
    'details': JSON.Stringify(prod)
}

And check is data properly sent, you can do that in browser's development tools (on most of them is key F12) and look in Network tab.

After that, I suggest that you try to debug your PHP code, e.g.

die(var_dump($_POST['details']));

On start of your script. If this turns to undefined index notice, there is problem with sending/receiving data.