James Alton
James Alton

Reputation: 11

Jquery AJAX not passing “POST” data on PHP

function getHash() {
  alert('Debug me gethash');
  return localStorage.getItem('hash');
}

function getUser() {
  return getHash().split('~')[0];
}

function setHash(data) {
  alert('Debug sethash');
  localStorage.setItem('hash', data);
}

function doAction(action, params, callback) {
  alert('Debug me 3');
  $.post('../core.php?action=' + action + '&hash=' + getHash(), params, callback);
}

function register() {
  alert('debug1');
  doAction('register', {
    username: $('#register_user').val(),
    ref: fromUrl('ref')
  }, function(data) {
    alert('Debug me 2');
    var json = JSON.parse(data);
    if (json.hasOwnProperty('error'))
      alert(json.error);
    else
      ready(json);
  });
}
$db=new PDO('mysql:dbname=name;host=localhost',
'',
'',
array(PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION));
 switch ($_GET['action']) {
  case'register': if(isset($_POST['username'], $_POST['ref'])) {
    $username=$_POST['username'];
    if(strlen($username) < 3) error('Username is too short!');
    if(strlen($username) > 10) error('Username is too long!'); //N
    if(!preg_match('/^[a-zA-Z0-9_]+$/', $username)) error('Username contains invalid characters!');
    $ret=$db->prepare("SELECT * FROM users WHERE username=?");
    $ret->execute(array($username));
    if($ret->fetch()) error('Username already exists!');
    $hash=md5(uniqid("", true));
    $db->prepare("INSERT INTO users(username,hash,ontime,referral) VALUES(?,?,?,?)")->execute(array($username, $hash, time(), (int)$_POST['ref']));
    die(json_encode(array('hash'=>$username.'~'.$hash)));
  }
  ;
}
 <h2 class="dialog_text">Register</h2>
<input type="text" class="form-control-modal" placeholder="Username" id="register_user">
<button type="button" class="btn btn-modalbuy" class="close" onclick="register()">Sign Up</button>
<div class='smalltext'>By accessing the site I verify that I am at least 18 years old and have read the Terms & Conditions.</div>
<br>
<hr>//PHPCOde $db = new PDO('mysql:dbname=name;host=localhost', '', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); switch ($_GET['action']) { case 'register': if(isset($_POST['username'],$_POST['ref'])) { $username = $_POST['username']; if(strlen($username)
<
3) error( 'Username is too short!'); if(strlen($username)>10) error('Username is too long!'); //N if(!preg_match('/^[a-zA-Z0-9_]+$/',$username)) error('Username contains invalid characters!'); $ret = $db->prepare("SELECT * FROM users WHERE username=?"); $ret->execute(array($username)); if($ret->fetch()) error('Username
  already exists!'); $hash = md5(uniqid("",true)); $db->prepare("INSERT INTO users(username,hash,ontime,referral) VALUES(?,?,?,?)")->execute(array($username,$hash,time(),(int)$_POST['ref'])); die(json_encode(array('hash'=>$username.'~'.$hash))); }; }

I get POST http://localhost/core.php?action=register&hash=null 500 (Internal Server Error) and XHR finished loading: POST "http://localhost/core.php?action=register&hash=null".

Not so sure if its my server or the code. But I cant seem to get it to work. Spent 4 hours today trying.

Upvotes: 0

Views: 52

Answers (1)

Nytrix
Nytrix

Reputation: 1139

A 500 Internal Server Error is shown if the code has a fatal error but error reporting is off.

Turning on error reporting through php file:

ini_set('display_errors', 1);

Turning on error reporting through htacces file:

php_flag display_errors 1

Upvotes: 1

Related Questions