Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Checkbox to operate

Status
Not open for further replies.

smiffy47

Programmer
Apr 6, 2004
95
GB
To avoid struggling to "re-invent the wheel" I'd be grateful for guidance as to how to make the modest checkbox operate.
The text will be asking the punter to check the checkbox to confirm that they agree to abide by certin rules.
I simply want the submit button to open "page_a.htm" if the box is checked, and "page_b.htm" if is not.
Alternatively the Submit Button could simply remain inoperable until the box is checked.

Life...It's difficult not to take it personally.
 
You either need to use Javascript to dynamically change the location that the form that contains the checkbox submits to, or you would have to use a server-side language to redirect to the proper location once the Form has been submitted.

The easiest way would be to use Javascript, however this is highly un recommended because if the user doesn't have Javascript enabled the checkbox will fail miserably.

but if you can be sure that all your users will have Javascript enabled you can do something like:

Code:
<form id="myform" action="[red]page_a.htm[/red]" method="POST"> [green]//set default location for form in case chekcbox is never checked[/green]
<input name="mycheckbox" type=checkbox OnClick="[blue]if(document.forms[1].mycheckbox.checked){document.getElementById('myform').action='page_b.htm';} 
else{ document.getElementById('myform').action='page_a.htm';}[/blue]">
[green]check if chekcbx has been checked or unchecked and change location of form accordingly[/green]
  <input name="Submit" type=submit value="Send">
</form>

If you would like to go the server-side way tell us what server-side language is available to you and we can come up with a solution.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
If PHP is available then something like this should suffice in a PHP script say redirector.php

Code:
[green]//Have your form submit to redirector.php[/green]
<form action="redirector.php" method="POST">
<input type=checkbox name=mycheckbox>
<input type=submit name="send" value="Send">
</form>

and in redirector.php add:

Code:
<?PHP
if(isset($_POST['send'])){ [green]//check if the form has been submitted[/green]
  if(isset($_POST['mycheckbox']){ [green]// Check if checkbox has been submitted in which case it was checked. And redirect to appropriate page.
header("Location:page_b.htm");
     }
     else{
      [green]//checkbox was not checked, Direct to page_a.htm[/green]
      header("Location:page_a.htm");
         }
}
?>

Its a little more code than the JS solution, but it guarantees it will work in all browsers.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I find that your solution crashes with the message

"Parse error: parse error, unexpected '{' in C:\wamp\ on line 11"

Line 11 is your line:

if(isset($_POST['mycheckbox']){ [green]

Can you see why? I don't have the knowledge of PHP to debug this sort of thing.

Also, I assume the html code should have quotation marks around the elements after the equals signs?

I do appreciate your help.


Life...It's difficult not to take it personally.
 
Its missing a Parenthesis there, sorry:

Code:
if(isset($_POST['mycheckbox'])[red])[/red]{ 
[code]

Ohh and take out the "[green]" text it should not be there at all.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks.
Now I'm getting error message:

"Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\ in C:\wamp\ on line 12"

Line 12 reads: -

header("Location:registration.htm");

The whole redirector page reads:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?PHP
if(isset($_POST['send'])){ //check if the form has been submitted
if(isset($_POST['mycheckbox'])){ // Check if checkbox has been submitted in which case it was checked. And redirect to appropriate page.
header("Location:registration.htm");
}
else{
//checkbox was not checked, Direct to page_a.htm
header("Location:accept.htm");
}
}
?>

</body>
</html>

Where am I going wrong, please?
Steve

Life...It's difficult not to take it personally.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top