Jump to content

If/else statement in html form


simon157

Recommended Posts

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
Link to comment
Share on other sites

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

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

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

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) [snapback]375728[/snapback][/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
[code]
<?
if ($_post['checkbox'] == 1)
{
$action = "databaseresultsthumb.php";
}
else
{
$action = "databaseresults.php";
}
?>

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

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

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) [snapback]375752[/snapback][/div][div class=\'quotemain\'][!--quotec--]
try $_POST instead of $_post
[/quote]
Link to comment
Share on other sites

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:
[code]if (isset($_POST['checkbox']) && intval($_POST['checkbox'])==1)[/code]
Link to comment
Share on other sites

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) [snapback]375756[/snapback][/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:
[code]if (isset($_POST['checkbox']) && intval($_POST['checkbox'])==1)[/code]
[/quote]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.