Joshua Joshua - 5 months ago 14
Ajax Question

Echo in php Post block is not printing out values posted by JQuery

I have a test that runs for a certain number of trials, and once finished, it posts the data to the PHP script. The data is successfully added to the database, but it does not echo out the results. I want the test results to be displayed for the user.

PHP

<?php
require_once 'php-includes/header.php';
include ('php-includes/phpValidate.php');
if ($_POST) {
include ('php-includes/antHelper.php');

$testData = removeDelimiters($_POST['RT'], $_POST['condition'], $_POST['response'], $_POST['ansStr'], $_POST['congruency'], $_POST['cueStr']);
$testID = addTest($_POST['toa'], $_POST['nTrial']);

if (isset($testID)) {
for ($i = 0; $i < count($testData["RT"]); $i++) {
addResponse((int)$testData["RT"][$i], (int)$testData["condition"][$i], $testData["response"][$i], $testData["answer"][$i], $testData["cue"][$i], $testData["congruence"][$i], $testID);
}
$results = getCueScores($testID);
$results = getCongruencyScores($testID, $results);
$alerting = $results["noCue"] - $results["doubleCue"];
$orienting = $results["centerCue"] - $results["spatialCue"];
$executive = $results ["incongruent"] - $results["congruent"];
echo 'Your alerting result is' . $alerting;
echo 'Your orienting result is' . $orienting;
echo 'Your executive result is' . $executive;
header('Location: index.php');
}
}
?>


JS

$(".btn[name=submitANT").click(function() {
$.post("antApp.php",
{response : $("input[name=rspStr]").val(), RT : $("input[name=RTStr]").val(), nTrial : $("input[name=nTrial]").val(), toa : $("input[name=toa]").val(),
condition : $("input[name=condition]").val(), ansStr : $("input[name=ansStr]").val(), cueStr : $("input[name=cueStr]").val(),
congruency : $("input[name=congruency]").val()},
function(data) {
//$("#antDebrief").show();
//$("#antDebrief").css("visibility", "visible");
});
});

Answer

You PHP sends header('Location: index.php'); right after the echos.

This is like a window.location.href="XYZ";.
It redirects the page.
So maybe there is nothing wrong but you just don't have the time to see the echos.

Try commenting this line :

header('Location: index.php');

EDIT based on comment

1- Your "calling" page has to be different than the "called" page. A page that calls itself is never a good thing.

2- in your called page (antApp.php), the header is present : require_once 'php-includes/header.php'; and this is probably the body and footer : include ('php-includes/phpValidate.php');