Solved]Radio button help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Solved]Radio button help

Post by nhan »

i have followed the code in authetication 101 php tutorial, and it is working fine, but i have to add a radio button, these radio buttons will indicate a different of page to be redirected, below is my code, i dont know how to pass the variable of the radio button.... thanks for the help!

any tutorial site would help...

Code: Select all

<?PHP
$db = mysql_connect('xx', 'xx', 'xx') or die("Couldn't connect to the database."); 
mysql_select_db('mydatabase') or die("Couldn't select the database"); 
$result = mysql_query("SELECT count(userId) FROM users WHERE userPass='$_POST[userPass]' AND userName='$_POST[userName]'") or die("Couldn't query the user-database."); 
$num = mysql_result($result, 0); 
if (!$num) { 
echo"
<style type='text/css'>
<!--
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>

<table width='367' height='218' border='0' cellpadding='0' cellspacing='0'>
  <tr>
    <td background='login.gif'><table width='367' border='0' cellpadding='0' cellspacing='0'>
      <tr>
        <td><div align='center'>
		<form action='$_SERVER[PHP_SELF]' method='post'>
          <input type='text'  name='userName' value='$_POST[userName]'>
        </div></td>
      </tr>
      <tr>
        <td><div align='center' class='style1'>Username</div></td>
      </tr>
      <tr>
        <td><div align='center'>
          <input type='password' name='userPass'>
        </div></td>
      </tr>
      <tr>
        <td><div align='center' class='style1'>Password</div></td>
      </tr>
    </table>
      <table width='367' border='0' cellspacing='0' cellpadding='0'>
        <tr>
          <td width='71' rowspan='4'>&nbsp;</td>
          <td width='126'>&nbsp;</td>
          <td width='97' rowspan='4'><div align='center'>
            <input type='submit' name='Submit' value='Login'>
          </div></td>
          <td width='73' rowspan='4'>&nbsp;</td>
        </tr>
        <tr>
          <td class='style1'><input name='radiobutton' type='radio' value='tin'>
          Time In </td>
        </tr>
        <tr>
          <td class='style1'><input name='radiobutton' type='radio' value='tout'>
          Time Out </td>
        </tr>
        <tr>
          <td class='style1'><input name='radiobutton' type='radio' value='ot'>
          Overtime</td>
        </tr>
      </table></td>
	  </form>
  </tr>
</table>

";
} else { 

session_start(); 
$id = session_id();

$_SESSION['userName'] = $_POST['userName']; 
$_SESSION['pass'] = $_POST['pass']; 

exit;
} 
?>
Last edited by nhan on Thu Jun 16, 2005 8:07 am, edited 1 time in total.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

Code: Select all

$_POST['radiobutton']
?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

the radio button is passed by its name and then its value so for eg.

Code: Select all

&lt;input type=&quote;radio&quote; name=&quote;groupA&quote; value=&quote;area1&quote;&gt;Go To Area 1
&lt;input type=&quote;radio&quote; name=&quote;groupA&quote; value=&quote;area2&quote;&gt;Go To Area 2
&lt;input type=&quote;radio&quote; name=&quote;groupA&quote; value=&quote;area3&quote;&gt;Go To Area 3
then to retrieve and redirect

Code: Select all

if (isset($_POST['groupA']))
{
   if ($_POST['groupA'] == "area1")
   {
      header ("Location: area1.php");
   }
   elseif ($_POST['groupA'] == "area2")
   {
      header ("Location: area2.php");
   }
   elseif ($_POST['groupA'] == "area3")
   {
      header ("Location: area3.php");
   }
   else
   {
      // no valid matches
      print "I dont know where to go";
   }
}
else
{
   // no radio button was submitted
   print "i dont know where to go";
}
hope that helps
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

but how do i use the $post[radiobutton] to select a different page, should i use this one??

Code: Select all

switch($_POST&#1111;'radio']){
        case 'tin':
           $timecheck = 'in.php';
           break;
        case 'tout':
           $timecheck = 'out.php';
           break;
        case 'ot':
           $timecheck = 'ot.php';
           break;
        default:
           $timecheck = 'login.php';
     }

header(&quote;Location:$timecheck.php?userName=$_POST&#1111;userName]&sid=$id&quote;);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ya sure why not
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

it does not work.... :cry:
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

that's because you aren't inserting it correctly in the header.
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

so far, i have tried this one...

Code: Select all

if ($_POST&#1111;'radiobutton'] == $tin){
header(&quote;Location:tin.htm&quote;); 
} 
elseif ($_POST&#1111;'radiobutton'] == $tout){
header(&quote;Location:tout.htm&quote;); 
} 
elseif ($_POST&#1111;'radiobutton'] == $ot){
header(&quote;Location:ot.htm&quote;); 
}
and it seems to work fine.... thanks so much for all the help...
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

I'm guessing
$ot = "ot" ?
etc
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

last question, if the data entered was incomplete, for example the radio button was not enabled, what code should i use? so that the page would come back to the login page???

thanks!
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

look at my post
Post Reply