CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Redirection of Javascript failing (http://www.codingforums.com/showthread.php?t=289766)

Max31 03-13-2013 09:05 PM

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.

Philip M 03-13-2013 09:59 PM

Code:

function redirect() {
  var answer = confirm("Are you sure")
  if (answer){
    window.location = "http://www.google.com";
  }else{
    window.location = "http://www.facebook.com";
  }
 }


Old Pedant 03-13-2013 10:07 PM

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...


Max31 03-13-2013 10:07 PM

Hey man, thanks for taking the time to reply, but unfortunately i had tried that before with no success :(

Old Pedant 03-13-2013 10:10 PM

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.

Max31 03-13-2013 10:19 PM

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 03-13-2013 10:28 PM

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


All times are GMT +1. The time now is 07:36 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.