session array to save value of radio button

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
jamhussain
Forum Newbie
Posts: 17
Joined: Sat Apr 07, 2012 2:04 am

session array to save value of radio button

Post by jamhussain »

Code: Select all

<form method="post" action="point.php">
<table bgcolor="#FFCC00" width="40" border="2" cellspacing="10">
<tr><td colspan="20"><input type="radio" name="type" value="1000" />1000 point </tr>
<tr><td colspan="20"><input type="radio" name="type" value="500" />500 point </tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" id="submit" />
</table>
 </form> 

<?php
  
session_start();  
if (isset($_POST['Submit'])) {  
$_SESSION['a'][0] = $_POST['type'];
if($_SESSION['a'][0]== 1000)
{ $b==1000;}
 else
{ $b==500;}
 echo"the no is ".$b;
 
 }
      
?>

please check it that it is right. I want to save the value of radio button in a variable so that I may use it in further calculation.
Last edited by califdon on Wed Apr 18, 2012 3:35 pm, edited 1 time in total.
Reason: Moderator added syntax=php tags to make code readable. Note to poster, please always do this.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: session array to save value of radio button

Post by califdon »

You cannot use session_start() after you have sent anything to the browser, so you must do that BEFORE any HTML or even any blank lines.
jamhussain
Forum Newbie
Posts: 17
Joined: Sat Apr 07, 2012 2:04 am

Re: session array to save value of radio button

Post by jamhussain »

Please help me that how can improve this code.
session_start() is necessary for communication with session.
How can I save the value of radion button in an variable b so that I may use it in future calucaltion.
thanks.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: session array to save value of radio button

Post by x_mutatis_mutandis_x »

Swap your php block with the html block (see below)

Code: Select all


<?php
   
session_start();  
 if (isset($_POST['Submit'])) {  
 $_SESSION['a'][0] = $_POST['type'];
 if($_SESSION['a'][0]== 1000)
 { $b==1000;}
  else
 { $b==500;}
  echo"the no is ".$b;
  
  }
       
?>



 
<form method="post" action="point.php">
 <table bgcolor="#FFCC00" width="40" border="2" cellspacing="10">
 <tr><td colspan="20"><input type="radio" name="type" value="1000" />1000 point </tr>
 <tr><td colspan="20"><input type="radio" name="type" value="500" />500 point </tr>
 <tr><td colspan="2" align="center"><input type="submit" name="submit" id="submit" />
 </table>
  </form> 

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: session array to save value of radio button

Post by social_experiment »

When you equate use a single '=' the double equal sign is used for comparison.

Code: Select all

<?php
   
session_start();  
 if (isset($_POST['Submit'])) {  
 $_SESSION['a'][0] = $_POST['type'];
 // comparing here, use of == is correct
 if($_SESSION['a'][0]== 1000)
 // should be =, not ==
 { $b = 1000;}
  else
 { $b = 500;}
  echo"the no is ".$b;
  
  }       

?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply