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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘