Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/239856-embedded-javascript-not-working/
Share on other sites

first off this is a php help forum there is a javascript help in a another section cause thats a java problem not php  :confused:

 

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!

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.