php: echo 不是 echoing

发布于 2024-12-14 10:42:07 字数 1053 浏览 3 评论 0原文

我有以下代码,以及 javascript 回显之前的内容和 javascript 包含之后的内容,但 javascript 不会回显:/

$currentPage = $_POST["current_page"];
$nextPage = 1 + $currentPage;
$count = $_POST["cum_count"];
$total = $_POST["cum_total"];
$progress = $_POST["cum_progress"];
echo $currentPage . $nextPage;

# number of questions less 1
$numQs[2]=6;
$numQs[3]=3;
$numQs[4]=5;
$numQs[5]=34;
$numQs[6]=17;
$numQs[7]=43;

$falses = array('false');
    for ($i=0; $i < $numQs[$nextPage]; $i++) {
        array_push($falses,', false');
    }

#  the js is how the survey keeps track of where it is
    echo "<script type='text/javascript'>\n
            var c_name = 'whbssurvey';\n
            var c_value = '$nextPage';\n
            document.cookie=c_name + '=' + c_value;\n
            // set survey info\n
            var count = $count;\n
            var total = $total;\n
            var progress = $progress;\n
            var qArray = [$falses];\n
        </script>";

    include("$nextPage.php");

PS 如果有人认为 cum_count 是肮脏的东西,它是累积的缩写。

I've got the following code, and the stuff before the javascript echos and the stuff after the javascript includes, but the javascript won't echo :/

$currentPage = $_POST["current_page"];
$nextPage = 1 + $currentPage;
$count = $_POST["cum_count"];
$total = $_POST["cum_total"];
$progress = $_POST["cum_progress"];
echo $currentPage . $nextPage;

# number of questions less 1
$numQs[2]=6;
$numQs[3]=3;
$numQs[4]=5;
$numQs[5]=34;
$numQs[6]=17;
$numQs[7]=43;

$falses = array('false');
    for ($i=0; $i < $numQs[$nextPage]; $i++) {
        array_push($falses,', false');
    }

#  the js is how the survey keeps track of where it is
    echo "<script type='text/javascript'>\n
            var c_name = 'whbssurvey';\n
            var c_value = '$nextPage';\n
            document.cookie=c_name + '=' + c_value;\n
            // set survey info\n
            var count = $count;\n
            var total = $total;\n
            var progress = $progress;\n
            var qArray = [$falses];\n
        </script>";

    include("$nextPage.php");

P.S. In case anyone is thinking cum_count is something dirty, it's short for cumulative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揪着可爱 2024-12-21 10:42:07

你确定它没有回声吗?它是在您进行包含之前完成的。如果该包含是一个完整的 html 页面,则 JS 将在开始 标记之前回显(这会导致页面无效)。

同样,为了转储这样的多行文本,您应该退出 PHP 模式,这样它只是自动回显的纯文本,或者使用 HEREDOC。由于您要在该输出中插入几个 PHP 变量,因此 HEREDOC 可能更可取。

Are you sure it's not echoing? It's being done BEFORE you do your include. If that include is a complete html page, the JS would be echoed BEFORE the opening <html> tag (which makes for an invalid page).

As well, for dumping out multiline text like that, you should either drop out of PHP mode so it's just plaintext that'll get echoed out automatically, or use a HEREDOC. Since you're inserting a couple PHP vars into that output, the HEREDOC would probably be preferable.

没有伤那来痛 2024-12-21 10:42:07

尝试在不使用 标签的情况下进行回显。它可能正在打印,但您的浏览器由于某种原因没有呈现它。如果您将所有代码都喷在页面上,那么它就起作用了。

Try echoing without the <script> and </script> tags. It's possible that it is being printed but your browser isn't rendering it for some reason. If you get all the code spewed on the page, it worked.

老娘不死你永远是小三 2024-12-21 10:42:07

尝试:

echo "<script type='text/javascript'>\n" .
        "var c_name = 'whbssurvey';\n" . 
        "var c_value = '$nextPage';\n" .
        "document.cookie=c_name + '=' + c_value;\n" .
        "// set survey info\n" .
        "var count = $count;\n" .
        "var total = $total;\n" .
        "var progress = $progress;\n" .
        "var qArray = [$falses];\n" .
    "</script>";

try:

echo "<script type='text/javascript'>\n" .
        "var c_name = 'whbssurvey';\n" . 
        "var c_value = '$nextPage';\n" .
        "document.cookie=c_name + '=' + c_value;\n" .
        "// set survey info\n" .
        "var count = $count;\n" .
        "var total = $total;\n" .
        "var progress = $progress;\n" .
        "var qArray = [$falses];\n" .
    "</script>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文