PDA

View Full Version : Javascript Warning!


stoneygossard
03-22-2007, 11:14 PM
Hi,

I want to place a javascript pop up warning (not a popup message).This will allow the user to press OK or CANCEL whereas the popup message will only give the OK option.

Any suggestions, preferrably there is a built in javascript snippet in DW8?

Stone

davidj
03-23-2007, 12:12 AM
function ask(){

conf = confirm("are you sure you want to proceed");

if(conf){

alert ("you pressed ok"); // do stuff here

}else{

alert ("you pressed cancel"); // do stuff here

}

}

just call the function from a button using


onclick="ask()";

stoneygossard
03-23-2007, 06:02 PM
Thanks David,

I have used your code snippet (and tweaked it for my site) and it does as it should i.e. alerts the user to their actions. As I have no experience in the syntax of javascript, could you let me know the "//do stuff" code whereby clicking ok on this link will log a user out of their session (the logout function is already written and applied to the link using php so I presume there is no code needed here) and if they press cancel, then no action will be taken i.e. the log out link will not be activated.

Stone

davidj
03-24-2007, 02:25 PM
if the logout function is in a page called logout.php...


function ask(){

conf = confirm("are you sure you want to proceed");

if(conf){

document.location.href="logout.php";

}

}


i have removed the ELSE condition as you dont need to do anything if they press cancel

stoneygossard
03-24-2007, 07:03 PM
Thanks again David,

I still have a problem though. I am putting the "onClick" function on the button of a logIn form ok. Now when the button is clicked, the javascript kicks in and asks "are you sure you wish to proceed". That part is fine. If I press "OK" it will log me in as per usual which is fine. However, If I press "Cancel", it still logs me in anyway (provided my log in details are correct.

Is there anyway I can get the Javascript to stop the log In process if I press "Cancel" on the button?

Appreciate the help on this one mate.

Stoney

davidj
03-24-2007, 07:07 PM
could the button be a submit button

If it is a submit button the the form will submit no matter what you select

that script is for a logout event not a login

stoneygossard
03-24-2007, 07:31 PM
could the button be a submit button

If it is a submit button the the form will submit no matter what you select

that script is for a logout event not a login

Yeah, it's for a submit. Well OK so I know now that the form will submit no matter what I do.

I was trying it with a "log-out" link (text link using the DW log-out php function) and the same was happening. The user was alerted to their action but once again, no matter what was pressed, the user was logged out.

Unfortunately I only have this code on a local testing server, otherwise i would stick it up on a site for your perusal.

Would you have any idea where I'm going wrong?

Stoney

davidj
03-24-2007, 07:41 PM
ok from the begining

what are you after when you click the confirm button ?

1) you want message displayed

if ok >> do stuff >> then submit form

??

stoneygossard
03-24-2007, 07:54 PM
ok from the begining

what are you after when you click the confirm button ?

1) you want message displayed

if ok >> do stuff >> then submit form

??

David,

Sorry if I was unclear about this so here goes:

This is the scenario as it stands:

I have a person logged into a site.
There is a "log off" link on this page(behind which lies php code to log that persons' session off)
When they press the "log off" link, they are logged off and brought back to the home page of the site.

This is what I want to happen:

The person clicks the "log off" link.
Javascript function kicks in, and they are asked "are u sure you want to log off"
If they press "ok", then they are logged off.
If they press "cancel" then nothing happens, they remain on same page and are not logged off.

I keep trying to get this to work but no matter what they press i.e. OK or CANCEL, they are logged off and brought back to the homepage of site. So I guess the PHP log off function goes ahead no matter what the javascript function does (as it stands now anyway).

I know there is a simple way to make this work but I'm obviously not putting in a piece of code/omitting a piece of code.

Appreciate the help on this one.

Stoney

ps Going to Newcastle ( as i'm from Ireland) on a stag weekend in April!

davidj
03-24-2007, 08:03 PM
if your clicking a logoff link then you dont need a form so no submit is nessesary

is the logoff a button or a text link?

stoneygossard
03-24-2007, 08:06 PM
if your clicking a logoff link then you dont need a form so no submit is nessesary

is the logoff a button or a text link?

lol...David,

I should have never said anything about a form, I was just trying to apply the javascript to a different part of the site - let's totally forget about forms and submit buttons.

Yes, the log off is a text link.

Stone

davidj
03-24-2007, 08:09 PM
ok does the link have a <a href

if so you need to do this

<a href="#" onclick="ask()" >log off</a>

so the JS function directs the action not the link

stoneygossard
03-24-2007, 08:23 PM
David,

It's not a real link, it simply contains the following:

<a href="<?php echo $logoutAction ?>">Log Out</a>

So, Can I just place the onClick="ask()" on this link?
And also, you gave me 2 snippets of code so far, which should I use as this is getting a bit confusing now for me.

Stone

davidj
03-24-2007, 08:25 PM
is the logout script in the same page

stoneygossard
03-24-2007, 08:25 PM
is the logout script in the same page

Yes it is.

Stone

davidj
03-24-2007, 08:27 PM
post the logout script

(if this looks like a dreamweaver logout script im going to take a bat to you!)

stoneygossard
03-24-2007, 08:32 PM
post the logout script

(if this looks like a dreamweaver logout script im going to take a bat to you!)

Gulp! It is a DW logout script ( I just took the easy way out with this one, hadn't time to hand code!)

Do I still need to post it?

Stone

davidj
03-24-2007, 08:34 PM
post it

stab stab stab

sticking pins into stoney doll!

stoneygossard
03-24-2007, 08:37 PM
Start Code:

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$logoutGoTo = "logIn.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}

End code


Sorry bout the DW code! But isn't this the DW Club?????


Stone

davidj
03-24-2007, 09:08 PM
ok

the page where the link is is called thepage.php in this example
change where you see this //<<****** to match your page name.

javascript..
function ask(){

conf = confirm("are you sure you want to proceed");

if(conf){

document.location.href="thepage.php?logout=1"; //<<******

}

}

PHP logout script... (in the same page)
logout.php is the page with the message which may say ..You have logged out successfully
or you could just redirect to the login page


if ((isset($_GET[logout])){
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);

header("Location: logout.php");//<<*****
exit;
}
}

stoneygossard
03-24-2007, 09:11 PM
ok

the page where the link is is called thepage.php in this example
change where you see this //<<****** to match your page name.

javascript..
function ask(){

conf = confirm("are you sure you want to proceed");

if(conf){

document.location.href="thepage.php?logout=1"; //<<******

}

}

PHP logout script... (in the same page)
logout.php is the page with the message which may say ..You have logged out successfully
or you could just redirect to the login page


if ((isset($_GET[logout])){
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);

header("Location: logout.php");//<<*****
exit;
}
}


Yep, that makes sense ok. Sorry for draggin' the arse out of it mate.
Thanks a lot.
Stoney

davidj
03-24-2007, 09:13 PM
addition...


Sorry bout the DW code! But isn't this the DW Club?????



yes but....

let me explain

pick a card.. any card..

dreamweaver code.

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$logoutGoTo = "logIn.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}



dj code..


if ((isset($_GET[logout])){
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);

header("Location: logout.php");//<<*****
exit;
}
}


get it?

stoneygossard
03-24-2007, 09:19 PM
Hate to bust yer chops on this mate, I just used that code and it did ask me "do you want to proceed" but when i pressed cancel, it still logged me out. do I need to change some of the DW logout code?Maybe i missed that bit.
Stone

davidj
03-24-2007, 09:19 PM
or even better
more streamlined...


if ((isset($_GET[logout])){

session_destroy();

header("Location: logout.php");//<<*****
exit;
}
}

davidj
03-24-2007, 09:20 PM
i need to see your page with the link in it

davidj
03-24-2007, 09:22 PM
in the javascript change this

if(conf){

to


if(conf==true){

stoneygossard
03-24-2007, 09:32 PM
ok tried it all,

I think we are crossing our wires david honestly.Doesn't that piece of code say to "log out" if the user confirms the action.

What I want is if the user presses "Cancel" for the user NOT to be logged out (but they do get logged out no matter which button is pressed). Correct me if i'm wrong as my head is wrecked at this stage!

Stone

davidj
03-24-2007, 09:36 PM
let me see the link code

i need it all

stoneygossard
03-24-2007, 09:42 PM
OK man,

Here's the code:

Start Code:


<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$logoutGoTo = "logIn.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "logIn_error.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Site Name</title>
<link href="css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {color: #8DB585}
.style2 {color: #8EB686}
.style4 {color: #91B686}
.style5 {color: #95BB8E}
.style6 {color: #99BD8F}
-->
</style>
<script type="text/JavaScript">
function ask(){

conf = confirm("Are you sure you want to Log Out");

if(conf==true){

document.location.href="logIn.php?logout=1";

}

}
</script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" class="bgmain"><table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><table width="780" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" rowspan="2" class="loginbg"><img src="images/spacer.gif" alt="spacer" height="15"/><center>
</center></td>
<td colspan="4" style="font-size:9px; color:#afafaf; padding-top:3px;">&nbsp;</td>
<td><img src="images/spacer.gif" width="1" height="25" alt="1" /></td>
</tr>
<tr>
<td colspan="4" rowspan="2"><a href="index.php">Home</a> &gt;&gt; White Wines Page </td>
<td><img src="images/spacer.gif" width="1" height="15" alt="1" /></td>
</tr>
<tr>
<td colspan="3"><img src="images/top_logo copy.gif" alt="company logo" width="368" height="83" /></td>
<td><img src="images/spacer.gif" width="1" height="83" alt="1" /></td>
</tr>
<tr>
<td class="navt1"><a href="#"></a> </td>
<td class="navt2"><a href="index.php" target="_self">Home</a></td>
<td colspan="2" class="navt3"><a href="<?php echo $logoutAction ?>" onclick="ask()">Log Out</a></td>

End Code:

Stone

stoneygossard
03-24-2007, 09:44 PM
Gotta go out for a while. Appreciate the help.No panic on it David. I'll check it out in the morning. the wife is giving out to me for being on so long .hee hee.

Stoney

davidj
03-24-2007, 09:47 PM
you are still using the php logout script

<a href="<?php echo $logoutAction ?>" onclick="ask()">Log Out</a>

needs to be

<a href="#" onclick="ask()">Log Out</a>

http://www.dreamweaverclub.com/forum/showpost.php?p=122457&postcount=12

stoneygossard
03-25-2007, 08:16 PM
David,

Thanks a million honestly. I got it solved with your help.

Stoney