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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Redirection of Javascript failing

    Hi,

    I am new to JavaScript and i though i had the hang of it until now, i apologise if this is an easy fix. Below is my js function:

    Code:
     function redirect() {
       var answer = confirm("Are you sure")
       if (answer){
         window.location = "www.google.com";
       }else{
         window.location = "www.facebook.com";
       }
     }
    I also have a form that calls js a function called Validate() which then calls the redirect() function if the statements are true - this all works apart from the redirection of pages.

    Code:
    function Validate() {
        
        var value = readCookie('TestCookie');
    
        if ( value == 1 && document.validate.drugid.selectedIndex == 5 )
        {
    		redirect();   
       
       }

    Below is my form:

    Code:
    <form name="validate" action="<?php echo $pfile;?>" onsubmit="return Validate()" method="post">
    <input type="hidden" name="patientid" id="drugnames" value="<?php echo $patientid;?>" />
    
    <?php
      
    	//blah blah blah
    ?>
    
    <br><br><input name="submit" id="formbutton"  type="submit" value="Prescribe"/>
    </form>

    The problem is my window.location are not redirecting at all, they just clear most of the contents of my page. Does anyone have any ideas how to solve this? I think it may have something to do with the "type=submit" on my form,it may have to be "button" but when i change this, i cant submit the page. Any help would be greatly appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    function redirect() {
       var answer = confirm("Are you sure")
       if (answer){
         window.location = "http://www.google.com";
       }else{
         window.location = "http://www.facebook.com";
       }
     }

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It's the type="submit", but only indirectly.

    The real problem is your Validate() function.

    You *DO* correctly code return Validate() in the <form> tag, but then you never do a return false from that function!

    When you don't return any particular value from Validate(), you have ONLY a 1 in 4 billion chance that the form will *not* submit, or in other words near certainty that it will.

    So you need:
    Code:
    function Validate() {
        
        var value = readCookie('TestCookie');
    
        if ( value == 1 && document.validate.drugid.selectedIndex == 5 )
        {
    		redirect();   
                    return false; 
       }
       ... and then maybe you need return false here as well...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Max31 (03-13-2013)

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey man, thanks for taking the time to reply, but unfortunately i had tried that before with no success

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Sorry...forgot to say: That <form> submittal is, effectively, overriding your change of location. So you indeed and up submitting the <form> and, since it looks like the action= is this same page, the server then serves up a new copy of the page.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah i see now, but surely if i want to redirect dependant on user inputs, should this be dismissed completely? I don't think this would work if i take it out all together though

  • #7
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile

    I'm sorry i didnt see you earlier post about the return false statement! It worked thank you so much!


  •  

    Tags for this Thread

    Posting Permissions

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