douzhangbao2187 2016-08-26 15:12
浏览 55
已采纳

连接到echoed语句的PHP变量未被回显

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-->

            </div>
            <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-->
        </div>
    </div>
</body>

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.*/
if($_POST["ID"]==$_SESSION["uid"]){
    $Viewing_Student=false;
}else{
    $Viewing_Student=true;
}
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:

HTML 5

JS (also pure javascript AJAX)

PHP 7

Sublime 3

Apache 2.4.17 (all in 1 WAMP download)

  • 写回答

4条回答 默认 最新

  • drn5375 2016-08-26 15:17
    关注

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

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

    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';
    xtruex
    php > echo 'x' . json_encode(false) . 'x';
    xfalsex
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效