tbremer
tbremer

Reputation: 693

Ajax Post 500 server error

I am trying to write some data to a MySQL Table however the .post call is returning with a 500 server error. Any help in the right direction would be great.

I think it's something to do with the _POST variables not sending right.

Here is the code:

JS:

function write_table(response) {
  var data = {
    'user_id' : response.id,
    'user_email' : response.email,
    'user_first' : response.first_name,
    'user_last' : response.last_name
  };

  console.log(data);

  $.ajax({
    'url': './includes/php/login_facebook.php',
    'data': data,
    'type': 'POST',
    'beforeSend': function(xhr, settings) {
      console.log('ABOUT TO SEND');
    },
    'success': function(result, status_code, xhr) {
      console.log('SUCCESS!');
    },
    'complete': function(xhr, text_status) {
      console.log('Done.');
    },
    'error': function(xhr, text_status, error_thrown) {
      console.log('ERROR!', text_status, error_thrown);
    }
  });
}

PHP:

    <?php

ini_set('display_errors',1);
error_reporting(E_ALL);

$host = 'localhost';
$un = 'root';
$pw = 'root';
$db = 'bikelouis';

$user_id = $_POST['user_id'];
$user_email = $_POST['user_email'];
$user_first = $_POST['user_first'];
$user_last = $_POST['user_last'];

$conn = mysql_connect($host, $un, $pw) or die(mysql_error());

if ($conn) {
    echo '<script> alert("connected!");</script>';
    mysql_select_db($db) or die(mysql_error());
    $sql = "INSERT INTO users (user_id, user_email, user_first, user_last) VALUES ($user_id, $user_email, $user_first, $user_last)";

} else {
    echo 'Connection failed.';
}
?>

I am using facebook connect, that is where 'response' is coming from. That works perfectly.

Upvotes: 0

Views: 4388

Answers (1)

Joshua Burns
Joshua Burns

Reputation: 8572

jQuery Side

$.post() is simply a wrapper for $.ajax() so if you want a little more control and visibility into what's going on, I'd highly suggest using $.ajax() instead.

The data argument for $.post() should be an object of key/value pairs and not a list. That said, I'm not sure what throwing the data object into the user_info list accomplishes, and this may be the root of your problem.

Try this and let me know how it works out for you:

function write_table(response) {
  var data = { // This is the format $.post() expects.
    'user_email' : response.email,
    'user_id' : response.id,
    'user_first' : response.first_name,
    'user_last' : response.last_name
  };
  console.log(data);

  $.post('./includes/php/login_facebook.php', data, function(result, status, xhr) {
    console.log(status, result);
  });
}

The same request, performed through $.ajax():

function write_table(response) {
  var data = {
    'user_email' : response.email,
    'user_id' : response.id,
    'user_first' : response.first_name,
    'user_last' : response.last_name
  };

  $.ajax({
    'url': './includes/php/login_facebook.php',
    'data': data,
    'type': 'POST',
    'beforeSend': function(xhr, settings) {
      console.log('ABOUT TO SEND');
    },
    'success': function(result, status_code, xhr) {
      console.log('SUCCESS!', 
    },
    'complete': function(xhr, text_status) {
      console.log('Done.');
    },
    'error': function(xhr, text_status, error_thrown) {
      console.log('ERROR!', text_status, error_thrown);
    }
  });
}

PHP Side

First off, I'd highly recommend opening PHP with <?php rather than <?, as the later is not enabled on all set ups.

Secondly, instead of receiving an Internal Server Error, actually displaying the errors in the browser is so much cleaner. At the beginning of any PHP script you wish to display potential errors on, include the following:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

As for the 500 Internal Server Error you're receiving, it's most likely because you're missing a $ in front of $_POST on line 8.

Instead of:
$user_id = _POST['user_id'];
It should read:
$user_id = $_POST['user_id'];

Inserting

  • All variables should be encapsulated in ticks / apostrophes.
  • It's also a great idea to escape the values, to prevent SQL injection attacks:

Try this:

$sql = "
  INSERT INTO users
  (user_id, user_email, user_first, user_last) VALUES (
    '" . mysql_real_escape_string($user_id) . "',
    '" . mysql_real_escape_string($user_email) . "',
    '" . mysql_real_escape_string($user_first) . "',
    '" . mysql_real_escape_string($user_last) . "'
  )
";

Upvotes: 3

Related Questions