I'm kinda making a mess now with my code, usually I'd make a PHP-AJAX script and pass one variable back to the JS script with json_encode. This time I would like to pass a few variables with json_encode, unfortunately I don't know how to separate the value in JS.
My JS code:
$('#submit').click(function(){
// Storing the value into variables
var content_page = $('#content-page').val();
var content_url = 'action=saving&page='+content_page;
// Starting the AJAX post form
$.ajax({
type: 'POST',
url: 'handlers/content.handler.php',
data: content_url,
success: function(responseText)
{
$.parseJSON(responseText); // Making the return value of the PHP handler reedable
alert(responseText.pageid)
if(responseText.indexOf(1) > -1)
{
noty({ text: 'U heeft geen pagina gekozen om te bewerken!' });
}
else if(responseText.indexOf(2) > -1)
{
noty({ text: 'U heeft geen geldige pagina gekozen!' });
}
else if(reponseText.indexOf(100) > -1)
{
noty({ text: 'Pagina is opgehaald' });
}
}
});
return false;
});
My PHP code:
<?php
/* Dit bestand handeld gegevens af voor het bestand content in de map panel. Gegevens komen hier door heen via een JS bestand(content.handler.js...) en worden ook weer terug gestuurd. */
include_once '../includes/config.php';
// Controleren of server contact maakt en geen persoon.
if(isset($_POST['action']) && $_POST['action'] == 'saving')
{
$stringPage = trim($_POST['page']);
$error = array();
$bolean = false;
# Prepared statement.
$stmt = $mysqli->prepare('SELECT id,name,title,text FROM pages WHERE name = ?');
$stmt->bind_param('s', $stringPage);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($pageID, $pageName, $pageTitle, $pageText);
$stmt->fetch();
$intPage = $stmt->num_rows();
$stmt->close();
/* Controle */
if(empty($stringPage))
{
$error[] = 1;
$bolean = true;
}
if($intPage == 0)
{
$error[] = 2;
$bolean = true;
}
if($bolean == false)
{
$error[] = 100;
}
header('Content-Type: application/json');
$array = array($error, $pageID, $pageName, $pageTitle, $pageText);
echo json_encode($array, JSON_FORCE_OBJECT);
}
?>
So all the value is coming back just fine, so there's no problem with that. My problem is that I put everything that I wanted in an array(not the best solution) and don't know how to separate in JS. I suggest it would be something like response.pageid but that didn't work.