简体   繁体   中英

php: echo is not echo'ing

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");

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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