...

View Full Version : My First Script - Any Tips?



srule_
07-28-2007, 04:22 PM
Hey, i Just made my first script ever! (YA!!!)

It is a very basic script that will give people a quote depending on what options they select from a form.

Anyway,It works fine but i would just like someone to look over it and tell me if there is a way i can write cleaner, point out any mistakes i make, and any improvements i should consider applying to my future scripts. THANKS!



<?php
//Only process if Send button was pressed
if (array_key_exists('send', $_POST)) {

//Assign Value according to numer of pages requires
if ($_POST['pages'] == 'five') {
$multi_pages ='10.00';
}
elseif ($_POST['pages'] == 'ten'){
$multi_pages = '20.00';
}
elseif ($_POST['pages'] == 'fifteen'){
$multi_pages = '30.00';
}
elseif ($_POST['pages'] == 'over_fifteen'){
$multi_pages = '40.00';
}
else {
$multi_pages = '0';
}

//Assign Value according to numer of images to be supplied
if ($_POST['images'] == 'no') {
$multi_images = '10.00';
}
elseif ($_POST['images'] == 'few'){
$multi_images = '$20.00';
}
elseif ($_POST['images'] == 'moderate'){
$multi_images = '30.00';
}
elseif ($_POST['images'] == 'lots'){
$multi_images = '40.00';
}
elseif ($_POST['images'] == 'all'){
$multi_images = '50.00';
}
else {
$multi_images = '0';
}

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Quotes</title>
</head>

<body>

<form id="quotes" method="post" action="">

<p>How many pages will your site require?</p>
<input type="radio" name="pages" id="pages-five" value="five"> 5
<input type="radio" name="pages" id="pages-ten" value="ten"> 6-10
<input type="radio" name="pages" id="pages-fifteen" value="fifteen"> 11-15
<input type="radio" name="pages" id="pages-over16" value="over_fifteen"> 16+
<p>

<p>Will you provide us with any images to be used in this project?</p>
<input type="radio" name="images" id="images-no" value="no"> No
<input type="radio" name="images" id="images-few" value="few"> A Few
<input type="radio" name="images" id="images-moderate" value="moderate"> Moderate
<input type="radio" name="images" id="images-lots" value="lots"> Lots
<input type="radio" name="images" id="images-all" value="all"> All
<p>

<input name="send" id="send" type="submit" value="Get Quote" />
</p>

</form>
<p> Your Quote:
<?php
if (isset($_POST['pages']) && isset($_POST['images'])) {
echo '$';
echo $multi_pages + $multi_images;
}
else {
echo 'Please fill in all fields';
}
?></p>

thesavior
07-28-2007, 05:32 PM
I personally would use a switch for this, it would clean it up quite alot.

http://us3.php.net/switch

JordanW
07-28-2007, 05:59 PM
A switch would be great for this... and it looks very clean... the only problem there is that I don't think you can use variables within a switch...which means that you won't be able to add up the cost of the pages and the cost of the images.

Just for your own personal reference... this is how you would use the switch for your case... but still make reference to the URL thesavior posted =]


if (array_key_exists('send', $_POST)) {

switch ($_POST['pages']) {
case five:
echo '$10.00';
break;
case ten:
echo '$20.00';
break;
case fifteen:
echo '$30.00';
break;
case over_fifteen:
echo '$40.00';
break;
default:
echo '$0.00';
break;
}
}


~Jordan

NancyJ
07-28-2007, 07:31 PM
You could eliminate the need for switches altogether if you used more useful form values.



<?php
//Only process if Send button was pressed
if (array_key_exists('send', $_POST)) {

//Assign Value according to number of pages requires
$multi_pages = $_POST['pages'] ? number_format($_POST['pages']*2,2) : 0;

//Assign Value according to numer of images to be supplied
$multi_images = $_POST['images'] ? number_format($_POST['images']*2,10) : 0;

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Quotes</title>
</head>

<body>

<form id="quotes" method="post" action="">

<p>How many pages will your site require?</p>
<input type="radio" name="pages" id="pages-five" value="5"> 5
<input type="radio" name="pages" id="pages-ten" value="10"> 6-10
<input type="radio" name="pages" id="pages-fifteen" value="15"> 11-15
<input type="radio" name="pages" id="pages-over16" value="20"> 16+
<p>

<p>Will you provide us with any images to be used in this project?</p>
<input type="radio" name="images" id="images-no" value="1"> No
<input type="radio" name="images" id="images-few" value="2"> A Few
<input type="radio" name="images" id="images-moderate" value="3"> Moderate
<input type="radio" name="images" id="images-lots" value="4"> Lots
<input type="radio" name="images" id="images-all" value="5"> All
<p>

<input name="send" id="send" type="submit" value="Get Quote" />
</p>

</form>
<p> Your Quote:
<?php
if (isset($_POST['pages']) && isset($_POST['images'])) {
echo '$'.($multi_pages + $multi_images);
}
else {
echo 'Please fill in all fields';
}
?></p>


If you want to validate that form values are only the values you want to allow, replace


//Assign Value according to number of pages requires
$multi_pages = $_POST['pages'] ? number_format($_POST['pages']*2,2) : 0;

//Assign Value according to numer of images to be supplied
$multi_images = $_POST['images'] ? number_format($_POST['images']*2,10) : 0;

with


//Assign Value according to number of pages requires
$allowed = array(5,10,15,20);
$multi_pages = in_array($_POST['pages'],$allowed) ? number_format($_POST['pages']*2,2) : 0;

//Assign Value according to numer of images to be supplied
$allowed = array(1,2,3,4,5);
$multi_images = in_array($_POST['images'],$allowed) ? number_format($_POST['images']*2,10) : 0;

srule_
07-28-2007, 08:43 PM
You could eliminate the need for switches altogether if you used more useful form values.

thank you nancy this solution u provided highlights of my main frustration with php in which i never know if there is a good function i could use to simplify my code.

likon
07-29-2007, 07:35 AM
your first script definitely more complex than mine.
while mine was
<?php
echo "hello world";
?>
lol
actually I never created this script...but.....???!!!!

srule_
07-29-2007, 01:44 PM
that was prob my 1st too lol~~!

this was the 1st i didn't copy from a book tho, kinda sad we had to copy something like that at one point! haha



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum