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 03-13-2013, 09:05 PM   PM User | #1
Max31
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Max31 is an unknown quantity at this point
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.
Max31 is offline   Reply With Quote
Old 03-13-2013, 09:59 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 03-13-2013, 10:07 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Max31 (03-13-2013)
Old 03-13-2013, 10:07 PM   PM User | #4
Max31
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Max31 is an unknown quantity at this point
Hey man, thanks for taking the time to reply, but unfortunately i had tried that before with no success
Max31 is offline   Reply With Quote
Old 03-13-2013, 10:10 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 03-13-2013, 10:19 PM   PM User | #6
Max31
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Max31 is an unknown quantity at this point
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
Max31 is offline   Reply With Quote
Old 03-13-2013, 10:28 PM   PM User | #7
Max31
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Max31 is an unknown quantity at this point
Smile

I'm sorry i didnt see you earlier post about the return false statement! It worked thank you so much!
Max31 is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript html

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 05:33 AM.


Advertisement
Log in to turn off these ads.