I’m trying to post some very simple data to a php file using jquery and then get the json response but I seem to be running into a road block somewhere. Here is my jquery:
<script>
$(function() {
$('.resend-verify').click( function() {
var userid = $(this).attr('rel');
$.ajax({
type: "POST",
url: "resend_verification.php",
contentType: "application/json; charset=utf-8",
data: "userid=" + userid,
dataType: "json",
success: function(data) {
console.log(data.response);
if(data.response == 'error') {
$('div.alert').addClass('error');
}
$('div.alert').html(data.comment);
}
});
});
});
</script>
and here is the php page it posts to
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") { // Check For Post
require '../../config.php'; // Site Settings
require '../../connectors/mysqlConnector.php';
include $DIR['php'] . 'functions.php'; // Common Functions
//var_dump(json_decode($_POST));
$UserID = $_POST['userid'];
$userSQL = "SELECT p.user_firstname,p.user_lastname,a.user_email,a.user_salt FROM web_profiles p INNER JOIN web_accounts a ON p.user_id = a.user_id WHERE p.user_id ='" . $UserID . "'";
$userQuery = mysql_query($userSQL);
//var_dump($userSQL);
$user = mysql_fetch_object($userQuery);
if (!$user->user_email) {
$response = array('response' => 'error', 'comment' => 'User not found');
} else {
// Send User Verification Email
$sendmail = new sendMail();
$message = createUserAuthEmail($user->user_firstname, $user->user_lastname, $user->user_salt, $Site['register_email_body']);
$content['body'] = '<br /><br />' . $message . '<br /><br />DO NOT REPLY TO THIS EMAIL! IT IS ONLY AN AUTOMATED NOTIFICATION EMAIL!';
$sendmail->set(to, $user->user_email);
$sendmail->set(subject, 'Action Required to Activate Membership');
$sendmail->set(from, 'no-reply@domain.com');
$sendmail->set(html, true);
$sendmail->getParams($content);
$sendmail->parseBody();
$sendmail->setHeaders();
if ($sendmail->send()) {
$response = array('response' => 'success', 'comment' => 'email sent');
} else {
$response = array('response' => 'error', 'comment' => 'Error sending email');
}
}
echo json_encode($response);
}
?>
The problem im having is that if I use contentType: “application/json; charset=utf-8” the $_POST is always empty. And when I remove contentType: “application/json; charset=utf-8” the $_POST is populated but I cant get a json response. What am I doing wrong??
You should read through the jquery documentation once more, especially the part that covers the
dataanddataTypeparameters.datamust be a key/value object, i.e.:…and for dataType, allowed values are xml, html, text, json, and jsonp. If your PHP script sends a suitable
Content-typeheader (e.g.header('Content-type: text/json');, then you can simply leave this parameter at the default (‘Intelligent guess’). jQuery will infer the response type from its content type header. You should send the header anyway because otherwise, the server will probably assume you’re sending HTML and add a HTML content type header itself, which jQuery then chokes on.It’s probably also a good idea to set the internal encoding and output encoding in your PHP script, so that it understands the request correctly and sends a well-formed UTF-8 response.
For further debugging, you might want to:
$_POSTarray and the response you’re sending to a text file on the server