davej Posted November 21, 2013 Share Posted November 21, 2013 I have not used Php in a long time. When do you need to use echo? Link to comment Share on other sites More sharing options...
jimfog Posted November 21, 2013 Author Share Posted November 21, 2013 before investigating the session start issue-which by the way hadien is right in mentioning- I want to comment the below code a little: <script> function doOnLoad(){ document.write('hi'); <?php echo 'john'; ?> } doOnLoad(); </script> And what is the result of the above? Hi is printed on the browser, while john is NOT printed. Can someone comment that. Is the above wrong? Putting PHP inside a js function. Because if the above is impossible than the session code presented in post21 is useless. Link to comment Share on other sites More sharing options...
Hadien Posted November 21, 2013 Share Posted November 21, 2013 I haven't used PHP in like a couple years either, but actually you should almost never need to use echo; only time I would use echo is if I'm accumulating all text to go on the page into a single variable so that i can continually mess with the data before actually sending any HTML headers. I can also run $Tidy->clean($html) before actually sending anything. most other times i would use a shorthand echo like <span> Welcome, <?= $username;?></span> but I don't count that as a true echo. jimfrog, the php code is valid, but you're echoing inside js code so the browser thinks its javascript code and not plaintext/html. it sees this: <script> function doOnLoad(){ document.write('hi'); john } doOnLoad(); </script> it thinks you're referring to a variable 'john', but not doing anything with it. and it sees the variable as undefined (its still valid code, so no error). the code in post 21 works because the jQuery that written there is written INSIDE the php curly brackets. if the isset($_SESSION[]) is false, php will skip all that jQuery code and jump to the closing curly bracket after in PHP, then continue on as usual. and as such the jQuery code wouldn't be added to the page. Link to comment Share on other sites More sharing options...
jimfog Posted November 21, 2013 Author Share Posted November 21, 2013 (edited) forget for now the code on post 21. How am I going to make the function above print also the PHP code? I am confused. Let me think a while and I will post again if needed. STILL CANNOT MAKE THE CODE WORK IN POST 21. give me a moment I am on to something. Edited November 21, 2013 by jimfog Link to comment Share on other sites More sharing options...
jimfog Posted November 21, 2013 Author Share Posted November 21, 2013 YES...THE JQUERY CODE WORKS AS INTENDED. The mistake was the code was placed above the script tags that call the jquery library into the script-it was a naive mistake of mine. Now to see that the code works also with the session variable. Link to comment Share on other sites More sharing options...
davej Posted November 21, 2013 Share Posted November 21, 2013 I don't know. I would have tried something like... <script>function doOnLoad(){<?php echo 'var validated = true'; ?> if (validated){alert('Yo ho, It be well validated!');}else{alert('Argh, it not be validated?');}}window.onload = doOnLoad; </script> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now