PDA

View Full Version : PHP Newbie, please check my code. Simple Radio button help guide



apeek22
05 Apr 2011, 09:15 AM
Hey All,

New php, just would like some guidance to make sure this would be the best way to go about this.
I am building a simple radio button help guide for choosing a product.
Option 1 leads to a more specific option etc etc. Eventually leading to the available products.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Select A Bike Carrier Guide</title>
</head>
<style type="text/css">
div#bikecarriercontainer{background-color:#cccccc;min-height:200px;width:800px;}
img{width:50%;height:50%;}
</style>
<body>
<div id="bikecarriercontainer" >
<form action="bikehelpstep2.php" method="Post" name="bikehelpstep1">
<table>

<tr>
<td>Step 1: Select Your Bike Carrier Style:</td>
<td><input type="radio" name="style" value="hitch" />Hitch Mounted Bike Rack</td>
<td><input type="radio" name="style" value="trunk" />Trunk Mounted Bike Rack</td>
</tr>

<tr>
<td><input type="submit" name="firststep" value="Next"/></td>
</tr>

</table>
</form>

</div>


</body>

</html>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Select A Bike Carrier Guide</title>
</head>
<style type="text/css">
div#bikecarriercontainer{background-color:#cccccc;min-height:200px;width:800px;}
img{width:50%;height:50%;}
</style>
<body>
<div id="bikecarriercontainer" >

<?php

$hitch_status = 'unchecked';
$trunk_status = 'unchecked';

if(isset($_POST['firststep'])) {
$selected_radio = $_POST['style'];

if ($selected_radio == 'hitch') {
$hitch_status = 'checked';

echo '<form action="bikehelpstep3hitch.php" method="Post" name="bikehelpstep2">
<table>

<tr>
<td><b>Option 1</b>: Hitch Mounted Bike Carrier</td>
</tr>
<tr>
<td>Step 2: How many bicycles do you need to carry?</td>
<td><input type="radio" name="quantity" value="two" <?php print $Hitch-Mounted_status; ?> 2</td>
<td><input type="radio" name="quantity" value="three" <?php print $Hitch-Mounted_status; ?> 3</td>
<td><input type="radio" name="quantity" value="four" <?php print $Trunk-Mounted_status; ?> 4</td>
</tr>
<tr>
<td><input type="submit" name="secondstep" value="Next"/></td>
</tr>
</table>
</form> ';

}
else if ($selected_radio == 'trunk') {
$trunk_status = 'checked';

echo '<form action="bikehelpstep3trunk.php" method="Post" name="bikehelpstep2">
<table>

<tr>
<td><b>Option 1</b>: Trunk Mounted Bike Carrier</td>
</tr>
<tr>
<td>Available Models</td>
</tr>
<td><a href="http://www.shipstoday.net/products/Trunk-Bike-Rack-3-Bicycle-Capacity.html">Trunk Bike Rack 3 Bicycle Capacity $33.99</a></td>
<td><a href="http://www.shipstoday.net/product_images/r/012/3biketrunkrack_1__65866_std.jpg">
<img src="http://www.shipstoday.net/product_images/r/012/3biketrunkrack_1__65866_std.jpg" /><a/></td>
</tr>
<tr>
<td><input type="submit" name="secondstep" value="Next"/></td>
</tr>
<tr>
<td><a href="bikehelpstep1.php">Start Over</a>
</tr>
</table>
</form> ';
}
}
?>
</div>


</body>

</html>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Select A Bike Carrier Guide</title>
</head>
<style type="text/css">
div#bikecarriercontainer{background-color:#cccccc;min-height:200px;width:800px;}
img{width:50%;height:50%;}
</style>
<body>
<div id="bikecarriercontainer" >
<?php

$two_status = 'unchecked';
$three_status = 'unchecked';
$four_status = 'unchecked';

if(isset($_POST['secondstep'])) {
$selected_radio = $_POST['quantity'];

if ($selected_radio = 'two') {
$two_status = 'checked';

echo '
<table>

<tr>
<td>Step 2: How many bicycles do you need to carry?</td>
<td><input type="radio" name="quantity" value="one" <?php print $Hitch-Mounted_status; ?> 1</td>
<td><input type="radio" name="quantity" value="two" <?php print $Hitch-Mounted_status; ?> 2</td>
<td><input type="radio" name="quantity" value="three" <?php print $Hitch-Mounted_status; ?> 3</td>
<td><input type="radio" name="quantity" value="four" <?php print $Trunk-Mounted_status; ?> 4</td>
</tr>

</table>';

}
else if ($selected_radio = 'trunk') {
$trunk_status = 'checked';
}
}
?>
</div>


</body>

</html>

plahpoy
05 Apr 2011, 10:38 AM
I would keep my form processing on the form page before moving to the next one. This will allow you to keep error processing relevant to the form your user is currently working on. Use sessions to store your form data and make it available across multiple pages. I would use if($_POST) and would not check for the specific 'submit' button variable being passed. You do not need to specify 'unchecked' in a form, only checked or selected for the one being checked or selected. Make a function to check for these values:
<?php
function isChecked($var,$value){
if($var == $value){
echo ' checked ';
}
}
<input type="checkbox" <?php isChecked($_SESSION['quantity'],2);?>

I would also not use 'quantity' for every forms input names, make them more descriptive such as 'quantityOfBikes' etc, this will make using the end result data a lot more understandable.

www.php.net/function
www.php.net/session
www.php.net/forms

when in doubt RTFM :)