简体   繁体   English

连接到echoed语句的PHP变量没有被echoed

[英]PHP variable concatenated to echoed statement is not being echoed

I have a html-js-php page that uses pure javascript ajax. 我有一个使用纯javascript ajax的html-js-php页面。

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. html页面有一个包含所有内容的容器,然后ajax将带入问题和按钮。

The unexpected result is a php variable is not echoed with the rest of the string. 意外的结果是php变量未与字符串的其余部分回显。 The php code is in the second code block, and the code proving this result is in the third code block. php代码在第二个代码块中,证明该结果的代码在第三个代码块中。

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. 第一组问题是通过正文onload加载的,它会调用对加载问题的php文件的ajax调用。 Skipping to the portion of this php code where the issue is... (Only displaying the Previous button) 跳到此php代码中问题所在的部分...(仅显示上一个按钮)

/*$_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. 问题是$ Viewing_Student无法访问html。 Everything else reachs the html, except that one variable. 除一个变量外,其他所有内容均到达html。 Which makes it even more confusing because the $_POST["ID"] is concatenated and displayed correctly, but not this variable. 由于$ _POST [“ ID”]已串联并正确显示,但不是此变量,这使它更加混乱。

As mentioned in a comment, the variable holds a value (boolean value false), this is checked by a var_dump. 如注释中所述,变量包含一个值(布尔值false),该值由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) 我知道通过查看chrome,firefox和microsoft edge开发人员工具不会显示该变量:(此处的代码是从chrome开发人员工具复制的)

<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: 该变量应该已经显示在saveSlide()参数的末尾,如下所示:

"this.value, false)"

Everything with the echo statement appears to be fine, it even echos out. 带有echo语句的所有内容似乎都很好,甚至可以回显。 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? 为了清楚地陈述我的问题...为什么这个变量($ Viewing_Student)没有与其余的字符串/变量一起回显? How can I fix this? 我怎样才能解决这个问题?

Programs/Languages/Versions I am using: 我正在使用的程序/语言/版本:

HTML 5 HTML 5

JS (also pure javascript AJAX) JS(也是纯JavaScript AJAX)

PHP 7 PHP 7

Sublime 3 崇高3

Apache 2.4.17 (all in 1 WAMP download) Apache 2.4.17(全部1 WAMP下载)

You can't dump a PHP boolean into Javascript (or even plain text) like that: 您不能像这样将PHP布尔值转储到Javascript(甚至纯文本)中:

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

Note how the false became a zero-length string. 注意false是如何变成零长度的字符串。 That'd produce a JS syntax error in your code. 那会在您的代码中产生JS语法错误。

Basically, NEVER output "text" from PHP directly into a Javascript context. 基本上, 从不将 PHP的“文本”直接输出到Javascript上下文中。 It's exactly situations like this that can produce JS syntax errors and kill your entire script block. 正是这种情况会产生JS语法错误并杀死您的整个脚本块。 Always use json_encode() : 始终使用json_encode()

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

It's a tiny thing that you have to know though, the boolean "false" will be echoed as "" <- empty string, you can check it from the manual : 这是您必须知道的一件小事,布尔值“ false”将被回显为“” <-空字符串,您可以从手册中进行检查:

A boolean TRUE value is converted to the string "1". 布尔TRUE值将转换为字符串“ 1”。 Boolean FALSE is converted to "" (the empty string). 布尔值FALSE转换为“”(空字符串)。 This allows conversion back and forth between boolean and string values. 这允许在布尔值和字符串值之间来回转换。

The problem is that a boolean is not a string and PHP simply does not display false values. 问题是布尔值不是字符串,PHP根本不显示错误值。 So I advice you replace this line: 因此,我建议您替换此行:

var_dump($Viewing_Student);//displays "false" as a boolean value

by this one: 通过这个:

echo $Viewing_Student?'true':'false';

Sounds like you want strings, not booleans 听起来像您想要的是字符串,而不是布尔值

if($_POST["ID"]==$_SESSION["uid"]){
$Viewing_Student='false';
}else{
$Viewing_Student='true';
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM