Raised This Month: $51 Target: $400
 12% 

JavaScript help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Rirre
Veteran Member
Join Date: Nov 2006
Old 12-17-2010 , 10:46   JavaScript help
Reply With Quote #1

In need of help with JavaScript.
I'm trying to make a checkbox with a submit button which you have to check to redirect/continue to the 'newpage.php' file.

You don't get redirected to 'newpage.php' when you check the checkbox and submit. It just refresh the current page you are on.
PHP Code:
<html>
<
head>
<
script language=javascript>
function 
validate(chk){
  if (
chk.checked == 1)
    
window.location='newpage.php';
  else
    
alert('You have not agreed!')
}
</script>
</head>

<body>
    <form>
        <input type='checkbox' name='chk1' /> Agree<br />
        <input type='image' src='images/continue.jpg' align='right' onclick='return validate(chk1);' />
    </form>
</body>
</html> 
Appreciate all help.

Last edited by Rirre; 12-17-2010 at 10:57.
Rirre is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 12-17-2010 , 10:54   Re: JavaScript help
Reply With Quote #2

Quote:
Originally Posted by Rirre View Post
You don't get redirected to 'newpage2.php' when you check the checkbox and submit. It just refresh the page.
Quote:
Originally Posted by Rirre View Post
PHP Code:
    window.location='newpage.php'

Last edited by Seta00; 12-17-2010 at 10:56.
Seta00 is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 12-17-2010 , 14:18   Re: JavaScript help
Reply With Quote #3

The problem with your code is that by clicking the image your are submitting the form and the action of your code to change the page gets overriden.
Code:
<html>
<head>
<script language=javascript>
function validate(chk){

  if (chk.checked == 1)
    window.location='newpage.php';    
  else
    alert("You have to agree");
}
</script>
</head>

<body>
    <form onsubmit="validate(chk1);return false">
        <input type='checkbox' name='chk1' /> Agree<br />
        <input type='image' src='images/continue.jpg' align='right' />
    </form>
</body>
</html>
__________________

Last edited by joaquimandrade; 12-17-2010 at 14:20.
joaquimandrade is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 12-17-2010 , 14:20   Re: JavaScript help
Reply With Quote #4

Dead people don't talk.
Seta00 is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 12-17-2010 , 15:44   Re: JavaScript help
Reply With Quote #5

Quote:
Originally Posted by Seta00 View Post
Dead people don't talk.
__________________
joaquimandrade is offline
Rirre
Veteran Member
Join Date: Nov 2006
Old 12-17-2010 , 21:43   Re: JavaScript help
Reply With Quote #6

Quote:
Originally Posted by joaquimandrade View Post
The problem with your code is that by clicking the image your are submitting the form and the action of your code to change the page gets overriden.
Code:
<html>
<head>
<script language=javascript>
function validate(chk){

  if (chk.checked == 1)
    window.location='newpage.php';    
  else
    alert("You have to agree");
}
</script>
</head>

<body>
    <form onsubmit="validate(chk1);return false">
        <input type='checkbox' name='chk1' /> Agree<br />
        <input type='image' src='images/continue.jpg' align='right' />
    </form>
</body>
</html>
Thank you
Rirre is offline
Reply



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 -4. The time now is 01:24.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode