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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple if else question 2

Status
Not open for further replies.
Jun 9, 2004
188
US
Hello I am passing two radio buttons via a post. One called HELP TEST.

On sumbit I want it to do two different things depenending on what is selected.

This is the first time I am using radio button and superglobal variables.

Should I be doing something like this:

Code:
<form action=\"temp.php\" method=\"post\" name=\"entry\">
	

	<input type=\"radio\" name=\"group1\" value\"help\" checked>HELP		
	<input type=\"radio\" name=\"group1\" value\"test\">TEST
	<input type=\"submit\" value=\"search\" >

</form>

//temp.php

if ($_POST['group1'] & HELP){
	
do this...
}

else { 
do something else...
}
 
Code:
if ($_POST['group1'] && $_POST['group1'] == 'help'){
    do this...
}
else { 
do something else...
}

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks for the quick response. It does not seems to be working though. It always defaults to test.



Code:
<form action=\"temp.php\" method=\"post\" name=\"entry\">
    
<input type=\"radio\" name=\"group1\" value\"help\" checked>HELP        

<input type=\"radio\" name=\"group1\" value\"test\">TEST
<input type=\"submit\" value=\"search\" >

</form>

if ($_POST['group1'] && $_POST['group1'] == 'help'){
    echo "Help was passed";
}
else { 
echo "test was passed";
}

Is there something wrong in my form?
 
The 'if' statement has to be executed after the form is submitted.

Code:
if (!isset($_POST['submit'])) {
$l = array();
$l[] = '<form action="temp.php" method="post" name="entry">';
$l[] = '<input type="radio" name="group1" value"help" checked>HELP';
$l[] = '<input type="radio" name="group1" value"test">TEST';
$l[] = '<input type="submit" value="search" name="submit">';
$l[] = '</form>';
echo implode("<br>\n",$l)."<br>\n";
}
else {
if ($_POST['group1'] && $_POST['group1'] == 'help')
    echo "Help was passed";
else echo "test was passed";
}

The first part will display the form if $_POST['submit'] is not set, the second part will do the test after the form is submitted.

Ken
 
Code:
if (!isset($submit))
{
  <form action=\"?omg=wtf\" method=\"post\" name=\"entry\">
    <input type=\"radio\" name=\"request\" value=\"help\" checked=\"checked\">HELP        
  <input type=\"radio\" name=\"request\" value=\"test\">TEST
  <input type=\"submit\" value=\"search\" >
</form>
}
else
{
  if ($_POST['request'] == "help"){ // if request == help
      echo "<b>OMG</b>, I need teh help!";
  } // end if (request == help)
  else { 
    echo "1337 power!!"; // if request is not help
  } // end else (if request != help)
} // end else (if submit is set)

I saw some fault in your html code:
You forgot to have the EQUALS operator in the value property of the radiobutton!
value="value", not value"value"

Olav Alexander Mjelde
Admin & Webmaster
 
sorry, forgot to echo out the form there, but I guess you know enough php to fix that..

my bad!

Code:
if (!isset($submit))
{
  echo "<form action=\"?omg=wtf\" method=\"post\" name=\"entry\">
    <input type=\"radio\" name=\"request\" value=\"help\" checked=\"checked\">HELP        
  <input type=\"radio\" name=\"request\" value=\"test\">TEST
  <input type=\"submit\" value=\"search\" >
</form>";
}
else // if user has submitted form
{
  if ($_POST['request'] == "help"){ // if request == help
      echo "<b>OMG</b>, I need teh help!";
  } // end if (request == help)
  else { 
    echo "1337 power!!"; // if request is not help
  } // end else (if request != help)
} // end else (if submit is set)

Olav Alexander Mjelde
Admin & Webmaster
 
So I would include this code in temp.php and not in form.html?

Seems overkill but hey what do I know. I will try. Thanks again for the effort.
 
Why include?

I dont know what you want to do here, but I set up the logical structure here.

If you wish to transfer it into multiple files, you are free to do so, but you can also include or require files, based on this logic. You might also want to meerly do a header redirect, with 301, permanently moved, based on the logic here.

Olav Alexander Mjelde
Admin & Webmaster
 
I'm a damn idiot. DaButcher found my error in the value property. Thanks for everyones help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top