| | |
Am not able to get a radio button selection to display in output
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2005
Posts: 15
Reputation:
Solved Threads: 0
I am having a little problem:
I am pretty "wet behind the ears" in the PHP world, so please, bare with me...I am tasked with writing a script that does two things:
(1) Allows a user to enter a temperature and based on a selection from a radio button, converts the temperature to either celcius or fahrenheit.
(2) Writes the output to another php page.
My problem lies when selecting either the celcius or fahrenheit radio button. How do I get either, based on selection, to write to the output? Do I need to write if/else statment or can I use case/switch?
Any and all advice is welcome. Remember, I am not at the advanced level yet, so please, bring yourself down a level or two (i.e. put yourself back at the beginning when you were first starting to develop... =) )
Here's my code for the output:
<?php
$mytempEntry=$_POST["tempEntry&qu ot;];
$convCelTemp=$_POST["convCel" ;];
$convFahTemp=$_POST["convFah" ;];
$convCelTemp=($mytempEntry - 32) *.55;
#echo $convCelTemp;
$convFahTemp=($mytempEntry * 1.8) + 32;
#echo $convFahTemp;
$celconvMsg=Celcius;
$fahconvMsg=Fahrenheit;
?>
<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<!-- Author: David Lindsey -->
<!-- ITEC2171 Mid-Term Page 2 -->
<!-- Last Revision: 3/7/05 -->
<title>Temperature Conversion 2</title>
<style>
body
{
font-family:tahoma,verdana,arial;
font-size:15pt;
}
h1
{
text-align:left;
}
</style>
</head>
<body>
<h1>Temperature Conversion</h1>
<p>The temperature you entered was <?php echo $_POST["tempEntry"]; ?>.</p>
<p>That is equivalent to <?php printf("%2.0f",$convCelTemp) ?> <?php echo $celconvMsg; ?>.</p>
<a href='convertTemp.php'?>Return to Temperature Page</a>
</body>
</html>
Thanks...
I am pretty "wet behind the ears" in the PHP world, so please, bare with me...I am tasked with writing a script that does two things:
(1) Allows a user to enter a temperature and based on a selection from a radio button, converts the temperature to either celcius or fahrenheit.
(2) Writes the output to another php page.
My problem lies when selecting either the celcius or fahrenheit radio button. How do I get either, based on selection, to write to the output? Do I need to write if/else statment or can I use case/switch?
Any and all advice is welcome. Remember, I am not at the advanced level yet, so please, bring yourself down a level or two (i.e. put yourself back at the beginning when you were first starting to develop... =) )
Here's my code for the output:
<?php
$mytempEntry=$_POST["tempEntry&qu ot;];
$convCelTemp=$_POST["convCel" ;];
$convFahTemp=$_POST["convFah" ;];
$convCelTemp=($mytempEntry - 32) *.55;
#echo $convCelTemp;
$convFahTemp=($mytempEntry * 1.8) + 32;
#echo $convFahTemp;
$celconvMsg=Celcius;
$fahconvMsg=Fahrenheit;
?>
<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<!-- Author: David Lindsey -->
<!-- ITEC2171 Mid-Term Page 2 -->
<!-- Last Revision: 3/7/05 -->
<title>Temperature Conversion 2</title>
<style>
body
{
font-family:tahoma,verdana,arial;
font-size:15pt;
}
h1
{
text-align:left;
}
</style>
</head>
<body>
<h1>Temperature Conversion</h1>
<p>The temperature you entered was <?php echo $_POST["tempEntry"]; ?>.</p>
<p>That is equivalent to <?php printf("%2.0f",$convCelTemp) ?> <?php echo $celconvMsg; ?>.</p>
<a href='convertTemp.php'?>Return to Temperature Page</a>
</body>
</html>
Thanks...
•
•
•
•
Originally Posted by venetian_jigsaw
(1) Allows a user to enter a temperature and based on a selection from a radio button, converts the temperature to either celcius or fahrenheit.
(2) Writes the output to another php page.
My problem lies when selecting either the celcius or fahrenheit radio button. How do I get either, based on selection, to write to the output? Do I need to write if/else statment or can I use case/switch?
<INPUT type="radio" name="temp_format" value="c"> Use Celcius
<INPUT type="radio" name="temp_format" value="f"> Use Farenheit
Then in your php page you would just
if ($temp_format == 'c') { // or use the "proper" $_POST["temp_format"]
// show the celcius version
} else {
// show the farenheit version
}
You're not asking us to do your homework are you?
•
•
Join Date: Mar 2005
Posts: 15
Reputation:
Solved Threads: 0
Thanks barnamos...ok, I am busted. The gig is up. Actually, I understand alot, but every once in a while my mind goes blank. I think we've all had that happen. An additional objective is that if the user enters a number and selects Convert to Celcius, then the message on the convertScript page will read: The temperature you entered was Fahrenheit. That is equivalent to x Celcius.
I am more inclined to use case/switch for this one. What is your take?
Thanks again...
Here's the form:
<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<title>Temperature Conversion 1</title>
<style>
body
{
font-family:tahoma,verdana,arial;
font-size:15pt;
}
h1
{
text-align:left;
}
</style>
</head>
<body>
<h1>Temperature Conversion</h1>
<form
name='myForm'
id='myForm'
method='post'
action='convertScript.php'
onsubmit=''
onreset="">
<p>Enter a temperature:
<input
type='text'
name='tempEntry'
id='tempEntry'
/><br>
</p>
<input
type='radio'
name='temp'
checked='checked'
value='Celcius' /> Convert to Celcius <br>
<input
type='radio'
name='temp'
value='Fahrenheit'/> Convert to Fahrenheit<br><br>
<input type='reset'
name='clear_form'
value='Reset Temperature'
/>
<input type='submit'
name='btnSubmit'
id='btnSubmit'
value='Convert'
/>
</form>
</body>
</html>
I am more inclined to use case/switch for this one. What is your take?
Thanks again...
Here's the form:
<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<title>Temperature Conversion 1</title>
<style>
body
{
font-family:tahoma,verdana,arial;
font-size:15pt;
}
h1
{
text-align:left;
}
</style>
</head>
<body>
<h1>Temperature Conversion</h1>
<form
name='myForm'
id='myForm'
method='post'
action='convertScript.php'
onsubmit=''
onreset="">
<p>Enter a temperature:
<input
type='text'
name='tempEntry'
id='tempEntry'
/><br>
</p>
<input
type='radio'
name='temp'
checked='checked'
value='Celcius' /> Convert to Celcius <br>
<input
type='radio'
name='temp'
value='Fahrenheit'/> Convert to Fahrenheit<br><br>
<input type='reset'
name='clear_form'
value='Reset Temperature'
/>
<input type='submit'
name='btnSubmit'
id='btnSubmit'
value='Convert'
/>
</form>
</body>
</html>
[HTML]<input
type='radio'
name='temp'
checked='checked'
value='Celcius' /> Convert to Celcius <br>
<input
type='radio'
name='temp'
value='Fahrenheit'/> Convert to Fahrenheit<br><br>[/HTML]
The above means that you need to look for the post variable 'temp' like so:
[PHP]$convTemp = $_POST['temp'];[/PHP]
Then whenever you reference $convTemp:
[PHP]if($convTemp == 'Fahrenheit') { do stuff }
if($convTemp == 'Celcius') {do stuff }
if(($convTemp != 'Fahrenheit') && ($convTemp !='Celcius')) { die('we have a big problem!'); }[/PHP]
you will see Fahrenheit or Celcius, whichever the user picked.
If I remember correctly I read somewhere (can't remember where) that if-then-else statements are faster than case-switch statements, but as you are only dealing with 2 options it probably doesn't matter which you use as long as it works.
type='radio'
name='temp'
checked='checked'
value='Celcius' /> Convert to Celcius <br>
<input
type='radio'
name='temp'
value='Fahrenheit'/> Convert to Fahrenheit<br><br>[/HTML]
The above means that you need to look for the post variable 'temp' like so:
[PHP]$convTemp = $_POST['temp'];[/PHP]
Then whenever you reference $convTemp:
[PHP]if($convTemp == 'Fahrenheit') { do stuff }
if($convTemp == 'Celcius') {do stuff }
if(($convTemp != 'Fahrenheit') && ($convTemp !='Celcius')) { die('we have a big problem!'); }[/PHP]
you will see Fahrenheit or Celcius, whichever the user picked.
If I remember correctly I read somewhere (can't remember where) that if-then-else statements are faster than case-switch statements, but as you are only dealing with 2 options it probably doesn't matter which you use as long as it works.
•
•
Join Date: Mar 2005
Posts: 15
Reputation:
Solved Threads: 0
Well, thanks to both of you, barnamos and Dance Instructor. Based on what you provided me with, I was able to figure out what I needed to do. I tested and it worked!!! Like I side, sometimes your brain goes in other directions. Actually, I sat down and wrote out my vars, etc, and then it came to me where each var needed to go in the if/else statements and then w/in my php tags.
Happy trails and again, many thanks!!!
venetian_jigsaw
Happy trails and again, many thanks!!!
venetian_jigsaw
![]() |
Similar Threads
- Issue with Radio button selection (PHP)
- Validating radio button selection for login redirect (JavaScript / DHTML / AJAX)
- how I chang radio button selection ? (Visual Basic 4 / 5 / 6)
Other Threads in the PHP Forum
- Previous Thread: trying to create an online contents menu
- Next Thread: Need help with PHP database search
Views: 5639 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email encode error file files folder form forms function functions google howtowriteathesis href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple multipletables mysql oop parse paypal pdf php problem provider query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table template tutorial update updates upload url validation validator variable video web xml youtube





