PHP Ajax jQuery Post - What have I done wrong?
This is my jQuery...
$.ajax({
type: 'POST',
url: 'http://adamscarter.co.uk/daily/facebook/savefbprofile.php',
data: {'fb_id': fb_id, 'fbusername': fbusername, 'location_id': location_id, 'gender': gender},
beforeSend: function() {
console.log('Before send: ' + fb_id, fbusername, location_id, gender);
},
success: function(data) {
console.log('saveFbProfile post ajax success');
console.log(data);
}
});
...and this is my PHP...
<?php
session_start();
include('dbcon.php');
$gender = substr($_POST['gender'], 0, 1);
$fb_id = $_POST['fb_id'];
$location_id = $_POST['location_id'];
$fbusername = $_POST['fbusername'];
//Set session vars
$_SESSION['gender'] = $gender;
$_SESSION['fb_id'] = $fb_id;
$_SESSION['location_id'] = $location_id;
$_SESSION['fbusername'] = $fbusername;
if (isset($_SESSION['userid'])) {
mysql_select_db('users', $GLOBALS['conInsert']);
$sql = "UPDATE user_fb_details SET gender = '". $gender. "', fb_id = '". $fb_id. "', location_id = '". $location_id. "', username = '". $fbusername. "' WHERE user_id = '". $_SESSION['userid']. "'";
mysql_qu开发者_StackOverflow中文版ery($sql, $GLOBALS['conInsert']);
}
?>
Is there a mistake in my code? When I log the variables they all have the correct value, but i just get a 'parseerror'.
Sounds like jQuery might be trying to parse the result of the POST as a JSON response and failing as it's not JSON. You might want to set the dataType to 'text'
, or return a different Content-Type
from the PHP.
And you'll definitely want to fix your dangerous SQL-injection holes with a bit of mysql_real_escape_string
or parameterised queries.
精彩评论