Jump to content


Photo

If/else statement in html form


  • Please log in to reply
6 replies to this topic

#1 simon157

simon157
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 May 2006 - 10:41 AM

Some help would be great is possible I have tried the following code to determine whether a checkbox has been checked or not whith the result of a different page loading of form submission. Here is a snippet from the code.

<form action = "<?php
$field1 = $_post['checkbox'];
if ( $field1 == 1 ) {
"databaseresultsthumb.php";
} else {
"databaseresults.php";
}
?>" method="get" name="indexform" id="indexform">

If someone can help me to get this working then that would be great as it's a real headache.

Many Thanks

Simon


#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 21 May 2006 - 10:50 AM

You need to echo the results, i've set it up like this for you as its more readable in my oppinion
<?
if ($_post['checkbox'] == 1)
{
$action = "databaseresultsthumb.php";
}
else
{
$action = "databaseresults.php";
}
?>

<form action = "<?php echo $action; ?>" method="get" name="indexform" id="indexform">

or yours would look like this
<form action = "<?php
$field1 = $_post['checkbox'];
if ( $field1 == 1 ) {
echo "databaseresultsthumb.php";
} else {
echo "databaseresults.php";
}
?>" method="get" name="indexform" id="indexform">


#3 simon157

simon157
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 May 2006 - 11:20 AM

Thanks so much for that. It's very nearly working. There are no errors but it just loads databaseresult.php even if the checkbox is checked. Any ideas? I have attached my code for the checkbox. Please see below..

<input name="checkbox" type="checkbox" id="checkbox" value="1">

I set the checked value to "1". Is this correct?

If you could help with this final problem then that would be great.

Thanks again

Simon

[!--quoteo(post=375728:date=May 21 2006, 05:50 AM:name=alpine)--][div class=\'quotetop\']QUOTE(alpine @ May 21 2006, 05:50 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You need to echo the results, i've set it up like this for you as its more readable in my oppinion
<?
if ($_post['checkbox'] == 1)
{
$action = "databaseresultsthumb.php";
}
else
{
$action = "databaseresults.php";
}
?>

<form action = "<?php echo $action; ?>" method="get" name="indexform" id="indexform">

or yours would look like this
<form action = "<?php
$field1 = $_post['checkbox'];
if ( $field1 == 1 ) {
echo "databaseresultsthumb.php";
} else {
echo "databaseresults.php";
}
?>" method="get" name="indexform" id="indexform">
[/quote]


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 May 2006 - 12:22 PM

try $_POST instead of $_post
Legend has it that reading the manual never killed anyone.
My site

#5 simon157

simon157
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 May 2006 - 12:39 PM

I tried that Andy but still does the same. I'm really not sure why ! I have attached the complete bit of code which might help solve the problem. It's a mess but I'll tidy it up after the problem is solved so my applogies for that.

<?
if ($_POST['checkbox'] ==1)
{
$action = "databaseintroresultsthumb.php";
}
else
{
$action = "databaseintroresults.php";
}
?>

<form action = "<?php echo $action; ?>" method="get" name="indexform" id="indexform">
<table width="90%" border="0" cellpadding="1" cellspacing="1" bgcolor="#FFFFCC">
<tr bgcolor="#485B3C">
<td colspan="22"><div align="center"><span class="style10">Search species index alphabetically - select a letter below , and press 'Go' to search databases and list results from all species databases. </span></div></td>
<td><div align="center"><span class="style10">
<input type="submit" name="Submit4" value="Go">
</span></div></td>
</tr>
<tr bgcolor="#485B3C">
<td><div align="center">A</div></td>
<td><div align="center">B</div></td>
<td><div align="center">C</div></td>
<td><div align="center">D</div></td>
<td><div align="center">E</div></td>
<td><div align="center">F</div></td>
<td><div align="center">G</div></td>
<td><div align="center">H</div></td>
<td><div align="center">I</div></td>
<td><div align="center">J</div></td>
<td><div align="center">K</div></td>
<td><div align="center">L</div></td>
<td><div align="center">M</div></td>
<td><div align="center">N</div></td>
<td><div align="center">O</div></td>
<td><div align="center">P</div></td>
<td><div align="center">R</div></td>
<td><div align="center">S</div></td>
<td><div align="center">T</div></td>
<td><div align="center">U</div></td>
<td><div align="center">V</div></td>
<td><div align="center">W</div></td>
<td><div align="center">Y</div></td>
</tr>
<tr bgcolor="#485B3C">
<td><div align="center">
<input name="speciesindex" type="radio" value="A">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="B">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="C">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="D">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="E">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="F">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="G">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="H">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="I">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="J">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="K">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="L">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="M">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="N">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="O">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="P">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="R">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="S">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="T">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="U">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="V">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="W">
</div></td>
<td><div align="center">
<input name="speciesindex" type="radio" value="Y">
</div></td>
</tr>
</table>
<input name="checkbox" type="checkbox" id="checkbox" value="1">
</form>

Thanks
Simon

[!--quoteo(post=375752:date=May 21 2006, 07:22 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 21 2006, 07:22 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
try $_POST instead of $_post
[/quote]


#6 eves

eves
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 21 May 2006 - 01:13 PM

Are you trying to dynamically change the path of the form when the user checks the checkbox? say, If the checkbox is checked, form will submit to page A, else submit to page B? If yes, use JavaScript (client-side)

if not, is the page submitted from another file? try these:
if (isset($_POST['checkbox']) && intval($_POST['checkbox'])==1)


#7 simon157

simon157
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 May 2006 - 01:37 PM

Hi Eve,

How you describe is exactly what I'm trying to do. I thought it would be fairly simple but it's been a real problem. Do you have any idea of a java script I could use?

Thanks

Simon


[!--quoteo(post=375756:date=May 21 2006, 08:13 AM:name=eves)--][div class=\'quotetop\']QUOTE(eves @ May 21 2006, 08:13 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Are you trying to dynamically change the path of the form when the user checks the checkbox? say, If the checkbox is checked, form will submit to page A, else submit to page B? If yes, use JavaScript (client-side)

if not, is the page submitted from another file? try these:
if (isset($_POST['checkbox']) && intval($_POST['checkbox'])==1)
[/quote]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users