Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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";
    		}
    	
    }

  • Users who have thanked devnull69 for this post:

    lanx2cool (12-18-2012)

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,616
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •