Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-18-2012, 07:53 PM   PM User | #1
lanx2cool
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
lanx2cool is an unknown quantity at this point
Confirmation box help :(

hello guys im confuse, what is the error in my code

i want to create a dialog box that if the user click true, the user will be directed to an another page
but if false it will stay on the current page

here is my code:

Code:
<script type="text/javascript">
function show_confirm()
{
	
	var s= confirm("Are you sure?")
	if (s==true)
		{
			<? header("Location: sad52/enrollment.php"); ?>
			}
	else
		{
			<? header("Location: sad52/studentpage.php"); ?>
			}
	
	}
</script>
<br /><input type="button" value="Enroll" onClick="show_confirm()" />
</form>
lanx2cool is offline   Reply With Quote
Old 12-18-2012, 07:57 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You'll have to understand that your PHP code will already be finished when Javascript is running. So if you put PHP code into your Javascript conditional, it won't do anything at run time because it will already have been evaluated and executed at page creation time.

You can do the redirection with Javascript only, though
Code:
function show_confirm()
{
	
	var s= confirm("Are you sure?")
	if (s==true)
		{
			window.location.href = "sad52/enrollment.php";
		}
	
}
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
lanx2cool (12-18-2012)
Old 12-18-2012, 08:03 PM   PM User | #3
lanx2cool
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
lanx2cool is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
You'll have to understand that your PHP code will already be finished when Javascript is running. So if you put PHP code into your Javascript conditional, it won't do anything at run time because it will already have been evaluated and executed at page creation time.

You can do the redirection with Javascript only, though
Code:
function show_confirm()
{
	
	var s= confirm("Are you sure?")
	if (s==true)
		{
			window.location.href = "sad52/enrollment.php";
		}
	
}
Thank you it fixed my problem, and im sorry im just a newbie
lanx2cool is offline   Reply With Quote
Old 12-18-2012, 08:51 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You also need to get rid of the confirm() call - it only continues to exist so it can be used for debugging scripts - in most browsers it will display an extra checkbox to either turn off all subsequent alert/confirm/prompt dialogs when the second such dialog is displayed. In at least one browser it always displays an extra checkbox allowing JavaScript to be turned off completely for the current web page every time it displays.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:20 PM.


Advertisement
Log in to turn off these ads.