I have an issue with sending json data with ajax to a php file and then use it to upload to the server.
If I'm making echo of the variable $email_info->Name
the returned data value is empty.
I've tried json_decode but it doesn't do it.
This is the code I have,
Jquery:
$( document ).on('click', '#send_touch', function(){
new_email = [];
new_email.push({
Name: $('#name').val(),
Phone: $('#phone').val(),
Email: $('#email').val(),
Interested_in: $('#interested_in').val(),
User_id: $('#email_user_id').val()
});
new_email = JSON.stringify({Email: new_email}, null, "\t");
$.ajax({
url: "core.php",
type: "post",
data: { NewMail: new_email
},
success: function(data){
alert(data)
},
error: function(){
}
});
});
PHP:
if ($_POST['NewMail']) {
$timeStamp = time();
$new_email = json_decode($_POST['NewMail'], true);
$email_info = $new_email->Email[0];
// New Email
mysql_query("INSERT INTO " . $dbPrefix . "touches (date, user_id, name, phone, email, interested_in, seen, status) VALUES ('".safeSQL($timeStamp)."', '".safeSQL($email_info->User_id)."', '".safeSQL($email_info->Name)."','".safeSQL($email_info->Phone)."', '".safeSQL($email_info->Email)."', '".safeSQL($email_info->Interested_in)."', '0', '1')") or die("Query failed: " . mysql_error());
echo $email_info->Name;
}
If I make an echo on the PHP side for $_POST['NewMail']
I get returned this:
{
\"Email\": [
{
\"Name\": \"John Doe\",
\"Phone\": \"1234567\",
\"Email\": \"john@doe.com\",
\"Interested_in\": \"Text here..\",
\"User_id\": \"1\"
}
]
}
How can I fix this?