Jim, I have a quirky error (I believe it’s in jQuery)
First my php file ajax.php :
[php]<?php
header(‘Content-type: application/json’);
require_once ‘lib/includes/utilities.inc.php’;
/* set content type so javascript understands this should be parsed as json
- and not html, this gives us automatic translation into arrays/object on
- the client side */
/* get ajax action */
$action = isset($_GET[‘action’]) ? $_GET[‘action’] : NULL;
/* validate ajax action /
switch ($action) {
case ‘save’:
save($pdo);
break;
case ‘validateUnique’:
/ valid ajax action, run it! /
$action();
break;
default:
/ invalid ajax action, show error! */
error(‘Invalid action’, 400);
break;
}
function save($pdo) {
if (empty($_POST[‘username’]) || empty($_POST[‘password’]) ) {
error(‘Missing param(s)’, 400);
}
else
{
/* Hash the Password with password_hash (PHP 5.5 or greater) /
/ PHP 5.3, 5.4 - https://github.com/ircmaxell/password_compat/blob/master/lib/password.php */
$password_hash = password_hash($_POST[‘password’], PASSWORD_BCRYPT, array(“cost” => 15));
try {
/* Set the query with the user's profile using prepared statements */
$query = 'INSERT INTO users ( username, password ) VALUES ( :username, :password )';
/* Prepare the statement using PDO prepare method */
$stmt = $pdo->prepare($query);
/* Execute statement along with the prepared values */
$result = $stmt->execute(array(':username' => $_POST['username'], ':password' => $password_hash));
/* If a result is return back then return "success" back */
if ($result) {
output('Saved ' . $_POST['username'] . ' / ' . $_POST['password'] );
} else {
error('Not Saved!');
}
} catch (PDOException $e) { // Report the Error!
echo "DataBase Error: The user could not be added.<br>" . $e->getMessage();
} catch (Exception $e) {
echo "General Error: The user could not be added.<br>" . $e->getMessage();
}
}
}
/* Displays error message /
function error($message, $code = 500) {
/ Set response code so javascript understands this is not a 200 OK
see: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes */
//http_response_code($code);
output($message, [], ‘error’);
}
/* Output data in correct format */
function output($message = ‘’, $data = [], $status = ‘success’) {
echo json_encode([
‘status’ => $status,
‘data’ => $data,
‘message’ => $message
]);
}[/php]
the strange things is when I comment out the http_response_code($code) it works and when I don’t i get url 400 Bad Request in the JavaScript console.
Here’s the js file:
[php]$(function() {
$("#register").submit(function(event) {
event.preventDefault(); // Prevent submit button from firing:
console.log($(this).serialize());
$.post(“ajax.php?action=save”, $(this).serialize())
.done(function() {
$("#register input[type=‘text’], #register input[type=‘password’]").each(function() {
$(this).val(’’);
});
}).always(function(response) {
console.log(response);
$(".result").text(response.message).html();
});
});
}); // END OF DOCUMENT READY FUNCTION:
[/php]
I’ve done google searches, but to no avail.