CyberGuru Posted June 20, 2011 Share Posted June 20, 2011 I am trying to set it up so that when someone who is not logged in tries to post in my shoutbox they get a javascript alert saying that they must be logged in to post. The session is passing and is allowing logged in users to post, and unlogged in users cannot post, but the javascript alert is not triggering. Any help would be greatly appreciated. here is the code: <?php session_start(); if(isset($_SESSION['Username'])) $poster = $_SESSION['Username']; else $poster = ""; if ($poster == "") { echo "<script>alert('You must be logged in to post')</script>"; //this is not working } else { //everything after this point seems to be working flawlessly $query = "INSERT INTO shoutbox (poster,message,time) VALUES ('".$poster."','".$message."','".$msg_time."')"; mysql_query($query); } KP: Added code tags. Quote Link to comment https://forums.phpfreaks.com/topic/239856-embedded-javascript-not-working/ Share on other sites More sharing options...
YoungNate_Black_coder Posted June 20, 2011 Share Posted June 20, 2011 first off this is a php help forum there is a javascript help in a another section cause thats a java problem not php but im not the best with jquery but a lil peace of advice jquery controls divs so u should be using ajax to talk to your php script bacuse java scripts dnt pass through php i dnt think they do my site does what u are trying to do but its a ajax xhtml request .... so my advice to you is ask around in the jquery / ajax / java section cause from the look of your code php is doing its job! Quote Link to comment https://forums.phpfreaks.com/topic/239856-embedded-javascript-not-working/#findComment-1232088 Share on other sites More sharing options...
Philip Posted June 20, 2011 Share Posted June 20, 2011 Where is $_SESSION['Username'] being set at? Does that script have session_start() at the top? Also, do note that your query is vulnerable to MySQL injections - you should consider sanitizing your input Quote Link to comment https://forums.phpfreaks.com/topic/239856-embedded-javascript-not-working/#findComment-1232090 Share on other sites More sharing options...
CyberGuru Posted June 20, 2011 Author Share Posted June 20, 2011 Where is $_SESSION['Username'] being set at? Does that script have session_start() at the top? Also, do note that your query is vulnerable to MySQL injections - you should consider sanitizing your input $_SESSION['Username'] is being set on the login page and has session_start(). The part of the code using the session is working correctly as its value is being successfully assigned to the variable $poster. The only thing thats not working is the echo'd javascript. No alert box pops up. If I knew what was wrong with it, I could better determine if it was a php or javascript issue. Quote Link to comment https://forums.phpfreaks.com/topic/239856-embedded-javascript-not-working/#findComment-1232095 Share on other sites More sharing options...
Philip Posted June 20, 2011 Share Posted June 20, 2011 Well a few things: Turn on error reporting, with E_ALL Add type="text/javascript" to your script tag Output (using var_dump/print_r $poster and/or $_SESSION to be sure Quote Link to comment https://forums.phpfreaks.com/topic/239856-embedded-javascript-not-working/#findComment-1232096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.