Tyler Tyler - 11 months ago 49
HTML Question

PHP variable concatenated to echoed statement is not being echoed

I have a html-js-php page that uses pure javascript ajax.

The page is a questionnaire that uses a load & save as you go setup. When you hit the next button, it saves the currently displayed questions, and loads the next set of questions. There is also a previous button and at the end a submit button.

The html page has a container for everything, and then ajax would bring in the questions and buttons.

The unexpected result is a php variable is not echoed with the rest of the string. The php code is in the second code block, and the code proving this result is in the third code block.

Here is the simplified container:

<body onload="loadSlide(<?php echo $_GET["ID"]; ?>, 'Next', hdn_Section_Counter.value)">
<div id="div_Questionnaire" class="Main_Content" >
<p class="Main_Content_Title"> Plannning For Success - Questionnaire </p>
<div class="Sub_Content" id="div_Questions">
<p class="Sub_Content_Title"> Questions </p>
<div id="div_Question_Holder"><!--Holds the questions-->

<input id="hdn_Section_Counter" type="hidden" value="0" Subject="1" Category="1" Section="1"/>
<!--Keeps track of current slide under 2 counting forums - section count (values 1-22), and Subject-Category-Section-->

The first set of questions is loaded via the body onload, which calls a ajax call to the php file that loads the questions. Skipping to the portion of this php code where the issue is... (Only displaying the Previous button)

/*$_POST["ID"] was sent over through ajax in the send() method (using POST)
$_SESSION["uid"] is created on login, there is a session_start() (to resume
the session) at the beginning of the php file.*/
var_dump($Viewing_Student);//displays "false" as a boolean value
echo"<input class='Nav_Button' id='btn_Previous' type='button' value='Previous' onclick=\"saveSlide('/my-site/Tools/Planning_For_Success/save_slide.php', ".$_POST["ID"].", this.value, ".$Viewing_Student.")\"/>";

The issue is $Viewing_Student is not getting to the html. Everything else reachs the html, except that one variable. Which makes it even more confusing because the $_POST["ID"] is concatenated and displayed correctly, but not this variable.

As mentioned in a comment, the variable holds a value (boolean value false), this is checked by a var_dump.

I know the variable is not being displayed by looking at the chrome, firefox and microsoft edge developer tools: (the code here was copied from chrome developer tools)

<input class="Nav_Button" id="btn_Previous" type="button" value="Previous" onclick="saveSlide('/my-site/Tools/Planning_For_Success/save_slide.php', 1, this.value, )">

The variable should have been displayed at the end of the saveSlide() arguments as followed:

"this.value, false)"

Everything with the echo statement appears to be fine, it even echos out. Except this one variable is always missing.

To clearly state my question... Why is this variable ($Viewing_Student) not being echoed with the rest of the string/variables? How can I fix this?

Programs/Languages/Versions I am using:


JS (also pure javascript AJAX)


Sublime 3

Apache 2.4.17 (all in 1 WAMP download)

Answer Source

You can't dump a PHP boolean into Javascript (or even plain text) like that:

php > echo 'x' . true . 'x';
php > echo 'x' . false . 'x';

Note how the false became a zero-length string. That'd produce a JS syntax error in your code.

Basically, NEVER output "text" from PHP directly into a Javascript context. It's exactly situations like this that can produce JS syntax errors and kill your entire script block. Always use json_encode():

php > echo 'x' . json_encode(true) . 'x';
php > echo 'x' . json_encode(false) . 'x';