jacko_162 Posted April 7, 2010 Share Posted April 7, 2010 i want to display a <div> with a class upon a user entering my site on the header.php file, but i want it to only show the notice once and user can close it, i already have a jquery notification which allows me to have a nicely styled <div> which the user can close, but it shows every time the header.php file is loaded, can i change the way the div is shown using php and perhaps the database to show only untill the user closes it and it wont load again untill i reset it? Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/ Share on other sites More sharing options...
aeroswat Posted April 7, 2010 Share Posted April 7, 2010 Yes. Use session variables of course. Something like: on close div code $_SESSION['divclosed']=1; then in the display of the div use php tags <?php if($_SESSION['divclosed']) { show div } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/#findComment-1038406 Share on other sites More sharing options...
jacko_162 Posted April 7, 2010 Author Share Posted April 7, 2010 sounds interesting, how can i initiate the session, i already have sessions on the login script can i edit the php on that to create a new one? here is my login exec php file: <?php //Start session session_start(); //Include database connection details require_once('Includes/config.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $login = clean($_POST['login']); $password = clean($_POST['password']); //Input Validations if($login == '') { $errmsg_arr[] = 'Login ID missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'Password missing'; $errflag = true; } //If there are input validations, redirect back to the login form if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: login-failed.php"); exit(); } //Create query $qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'"; $result=mysql_query($qry); //Check whether the query was successful or not if($result) { if(mysql_num_rows($result) == 1) { //Login Successful session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_MEMBER_ID'] = $member['member_id']; $_SESSION['SESS_FIRST_NAME'] = $member['firstname']; $_SESSION['SESS_LAST_NAME'] = $member['lastname']; session_write_close(); header("location: login-success.php"); exit(); }else { //Login failed header("location: login-failed.php"); exit(); } }else { die("Query failed"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/#findComment-1038415 Share on other sites More sharing options...
jacko_162 Posted April 7, 2010 Author Share Posted April 7, 2010 oops what i ment to say is that this message is not just for logged in users, i need anyone who visits site to see the message and being able to click close and not show it again. can i initiate a temporary session to show the message? Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/#findComment-1038418 Share on other sites More sharing options...
aeroswat Posted April 7, 2010 Share Posted April 7, 2010 oops what i ment to say is that this message is not just for logged in users, i need anyone who visits site to see the message and being able to click close and not show it again. can i initiate a temporary session to show the message? You should be able to just start the session with session_start and then set the variable. No matter if they are logged in or not. Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/#findComment-1038428 Share on other sites More sharing options...
jacko_162 Posted April 7, 2010 Author Share Posted April 7, 2010 ok i managed to get the following to work if i edit manually; <?php session_start(); $_SESSION['divclosed']=1; ?> then i echo: <?php if($_SESSION['divclosed']) { echo "<table width='100%' border='0' cellspacing='0' cellpadding='4' id='header_notification' style='opacity: 0.9;'><tr><td><div align='center'><img src='img/exclamation_octagon_fram.png'>"; echo "Please Note, this site is still in BETA version and changes can occur, please bear with us while we rectify any problems."; echo "</div></td></tr></table><br>"; } ?> this then allows me to show/hide the message if i change "1" to "0" how can i edit the above coding to allow members to see the message, then click an image to close it.? Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/#findComment-1038518 Share on other sites More sharing options...
ignace Posted April 7, 2010 Share Posted April 7, 2010 At best PHP can "close" the div after it received the first request (on which the div will be closed on any subsequent requests) Off course this is in the science that the user may not have read nor have actually closed it. You just assume that the user has read it before he clicks on any link on your website. A more effective way would be to use cookies (as both PHP and JS can set them) and set the cookie whenever the user actually clicks close then whenever he returns JS checks if any such cookie has been set and if it is then keeps the div closed, opened otherwise. PHP at that point can read the cookie and update the user table (if a login system is present) stating that the message was read and should not be displayed on any subsequent logins. Quote Link to comment https://forums.phpfreaks.com/topic/197870-how-to-display-a-status/#findComment-1038561 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.