php: echo no hace eco

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

$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


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

preguntado el 08 de noviembre de 11 a las 16:11

Stupid question - but are you viewing the source when checking to see if the JS has been printed to the page? -

Nice title, nice variable name. -

Side note: you are inserting Digital XNUMXk line feeds on each JS line. -

There are no PHP errors and the output of this PHP is loaded into another page via AJAX (so I can't check the source). But I do check the DOM via firebug, and everything except this javascript appears where I expect (and the javascript doesn't appear at all). @ÁlvaroG.Vicario, I thought so, but I added the \ns just in case the linebreak was being ignored and they were being commented out. -

Please double-check. You can actually check the HTML source in Firebug's Net panel. -

3 Respuestas

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.

respondido 08 nov., 11:20

+1 for pointing out the possibly invalid HTML. As for the heredoc, PHP accepts raw line feeds inside strings, that's not really an issue. - Álvaro González

@Alvaro: true, but using the heredoc removes the necessity of all those \n caracteres - Marc B

They aren't necessary anyway. If you hit entrar inside a string you get a carriage return as well. - Álvaro González

Yep, exactly. heredocs just make it easier to generate nicely formatted html blocks (with variable interpolation) and let you use whatever quote styles you want, without having to sprinkle escapes everywhere. - Marc B

Hi @MarcB, the the result of this php is loaded into a div via AJAX and the page from the include no contiene <html>,<head>y <body>, just the inners of a page. The echo $currentPage . $nextPage; appears at the very beginning inside the container div, and then is followed by the contents of the include. I look at the DOM with firebug after the AJAX load, and I don't see the javascript anywhere :/ I expect it to appear immediately after the result of echo $currentPage . $nextPage;. ¡Gracias! - Jakob Jingleheimer

Try echoing without the <script> y </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.

respondido 08 nov., 11:20

Si elimino el script tags, it does show up on the page. However, with them, the script doesn't appear in DOM and it does not do anything (the cookie's value is not changed and the js variables are not set). - Jakob Jingleheimer

btw, without the script tags, the text appears in the DOM where I expect it to. - Jakob Jingleheimer


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

respondido 08 nov., 11:20

There's no difference between a multiline concatenation and a multiline string. All you've done is added some extra work for the parser to join all those little strings together. - Marc B

thanks @snkmchnb, I did try that before for troubleshooting in case there was some crazy thing happening with quoting etc, but it didn't help (and just added more work for the parser). - Jakob Jingleheimer

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.