Help - Search - Members - Calendar
Full Version: Trouble with $pricing on HTML Form
HTMLHelp Forums > Programming > Server-side Scripting
SeanWoods
I have every part of my form working properly except for one.

I am having trouble getting my cost to populate on this page: Confirmation page

There are 3 different costs depending on how soon they want their document edited by:
  • Standard - 3 Day/72 Hour Return - $5.00 Per Page
  • Expedite - 2 Day/48 Hour Return - $7.50 Per Page
  • Overnight - 1 Day/24 Hour Return - $9.00 Per Page
I am trying to have whatever selection is chosen from these options to have the price per page multiplied by the # of pages ($numCount).

I wrote the code where it will calculate the cost for the Standard $5 option (although it won't show 2 decimal places). This is the number of pages ($numCount) multiplied by 5, but how do I automatically have it chose the correct code based on which option they chose? - Standard $5, Expedite $7.50, Overnight $9
CODE
<?php

$number = $numCount;
echo "\$" . $number*5;
$formatted = number_format($number, 2);

?>


Code for this page:
CODE

<?php
$words = $_GET['words'];
?>

<?php @$format= $_POST['serviceOption'];
if( is_array($serviceOption)){
while (list ($key, $val) = each ($serviceOption)) {
echo "$val <br>";
}
}//else{echo "not array";}  
?></td>
                </tr>
                  <tr>
                    <td class="pagetext"><strong>Word Count/Pages</strong><br />
(300 Words=1 Page)</td>
                    <td class="pagetext">
<?php

$txtWordCount = $_POST['txtWordCount'];
$numCount = $_POST['numCount'];


echo " ". $txtWordCount . " Words / " . $numCount . " Pages";


?>
</td>
                  </tr>
                  <tr>
                    <td class="pagetext"><b>Cost</b> </td>
                    <td class="pagetext">
                    
<?php

$number = $numCount;
echo "\$" . $number*5;
$formatted = number_format($number, 2);

?>
                    </td>
                  </tr>
                  <tr>
                    <td class="pagetext"><b>Level Of Work</b> </td>
                    <td class="pagetext">
<?php @$format= $_POST['level'];
if( is_array($level)){
while (list ($key, $val) = each ($level)) {
echo "$val <br>";
}
}//else{echo "not array";}  
?>
                    </td>
                  </tr>
                  <tr>
                    <td class="pagetext"><b>Writing Format</b> </td>
                    <td class="pagetext">
                    
<?php @$format= $_POST['format'];
if( is_array($format)){
while (list ($key, $val) = each ($format)) {
echo "$val <br>";
}
}//else{echo "not array";}  
?></td>


Code for previous page where selections are made Previous page where selections are made:
CODE

<?

$serv = $_GET['serv']; // retrieves the data and creates a variable called $serv
$words = $_GET['words']; // as above

$servvalue = $serv; // creating a value to go in the drop down box..

if($serv == 1)
{
$serv = "Standard - 3 Day/72 Hour Return - $5.00 Per Page";
$opt1 = ' selected="selected"';
}


if($serv == 2)
{
$serv = "Expedite - 2 Day/48 Hour Return - $7.50 Per Page";
$opt2 = ' selected="selected"';
}


if($serv == 3)
{
$serv = "Overnight - 1 Day/24 Hour Return - $9.00 Per Page";
$opt3 = ' selected="selected"';
}

?>

<form action="confirmorder.php" method="post">
<select name='serviceOption[]'>
<option value="Standard - 3 Day/72 Hour Return - $5.00 Per Page"<?php echo $opt1?>>Standard - 3 Day/72 Hour Return - $5.00 Per Page</option>
<option value="Expedite - 2 Day/48 Hour Return - $7.50 Per Page"<?php echo $opt2?>>Expedite - 2 Day/48 Hour Return - $7.50 Per Page</option>
<option value="Overnight - 1 Day/24 Hour Return - $9.00 Per Page"<?php echo $opt3?>>Overnight - 1 Day/24 Hour Return - $9.00 Per Page</option>                    
       </select>              
<!-- finish -->                      </td>
                    </tr>
                    <tr>
                      <td class="pagetext" width="145"><b>Word Count/Pages</b><br/>
                      (300 Words=1 Page) </td>
                      <td class="pagetext">
<!-- Word Count Below -->

<input name="txtWordCount" type="text" size="12" maxlength="12" value="
<?php echo $words; ?>" /></a>Words</a>  
<input name="numCount" type="text" size="4" maxlength="4"
value="<?php echo ceil($words/300); ?>" /></a>Pages</a>


                      <br />  
                      <a href="/cost.html">Word Counter/Cost Estimator</a> </td>
                    </tr>
                    <tr>
                      <td class="pagetext"><b>Level of Work</b> </td>
                      <td>

<select name='level[]'>
<option value='High School'>High School</option>
<option value='College - Admission'>College - Admission</option>
<option value='College - Under Graduate'>College - Under Graduate</option>
<option value='College - Post Graduate'>College - Post Graduate</option>
<option value='Website'>Website</option>
</select>                      </td>
                    </tr>
                    <tr>
                      <td class="pagetext"><b>Writing Format</b> </td>

<td><select name='format[]'>
<option value='MLA'>MLA</option>
<option value='AP'>AP</option>
<option value='N/A'>N/A</option>
</select>                     </td>
                    </tr>
                    <tr>
                      <td class="pagetext"><input type="submit" value="Proceed With Order" name="btnOrder" /></td></form>
Brian Chandler
It's not easy to see exactly what your problem is...

QUOTE(SeanWoods @ Apr 8 2009, 12:11 PM) *

I have every part of my form working properly except for one.



Really? The sample page you showed doesn't seem to include _any_ of the expected values...?

QUOTE


I am having trouble getting my cost to populate on this page: Confirmation page

There are 3 different costs depending on how soon they want their document edited by:
  • Standard - 3 Day/72 Hour Return - $5.00 Per Page
  • Expedite - 2 Day/48 Hour Return - $7.50 Per Page
  • Overnight - 1 Day/24 Hour Return - $9.00 Per Page
I am trying to have whatever selection is chosen from these options to have the price per page multiplied by the # of pages ($numCount).



Right, so on the page on which the user chooses, there is a select box? And what is the name of the parameter passed to the next page? You get this value from the $_GET or $_POST array? Then you echo this value somewhere?

At which stage does this process fail?

QUOTE


I wrote the code where it will calculate the cost for the Standard $5 option (although it won't show 2 decimal places). This is the number of pages ($numCount) multiplied by 5, but how do I automatically have it chose the correct code based on which option they chose? - Standard $5, Expedite $7.50, Overnight $9
CODE
<?php

$number = $numCount;
echo "\$" . $number*5;
$formatted = number_format($number, 2);

?>




Code for this page:
CODE

<?php
$words = $_GET['words'];
?>

<?php @$format= $_POST['serviceOption'];
if( is_array($serviceOption)){
while (list ($key, $val) = each ($serviceOption)) {
echo "$val <br>";
}
}//else{echo "not array";}  
?></td>




I think this is unnecessarily convoluted.

Why would the customer choose more than one "service option"? Does this make sense? (Ah, you have your POST arguments called things with [] on the end so PHP will read all the values? I think this is a bad idea (ugly at least), but unnecessary.

But if $serviceOption is an array, and you want to print all the values in it use

CODE

foreach($serviceOption as $opt)
{ echo "$opt<br>";
}


(Note that you have each() wrong anyway, since it returns an array of _four_ values, 2 copies of 'key' and 2 of 'val'.)

And if it is _not_ an array, what will you do?

<snip>

QUOTE

CODE

echo " ". $txtWordCount . " Words / " . $numCount . " Pages";




You've missed the point of double-quoted strings. They make it easier to include the values of (simple) variables in strings. All you need is:

echo " $txtWordCount Words / $numCount Pages";

Above you concatenated the string "\$" with something: this is where you want to use single quotes (= no $ replacement), and you can just write '$'.


QUOTE

CODE

$number = $numCount;
echo "\$" . $number*5;
$formatted = number_format($number, 2);



This is very odd. You echo $number*5, which should give the right value, but no decimal places if it's a whole number. Then you assign the number_format() value to a variable, which you never seem to use for anything...? (I presume the number_format() function is right, but it's *your* job to keep looking at the PHP manual, not mine.

HTH a bit. Please try to identify where your problem is a bit more clearly.
SeanWoods
QUOTE(Brian Chandler @ Apr 8 2009, 12:16 AM) *

It's not easy to see exactly what your problem is...

QUOTE(SeanWoods @ Apr 8 2009, 12:11 PM) *

I have every part of my form working properly except for one.



Really? The sample page you showed doesn't seem to include _any_ of the expected values...?

QUOTE


I am having trouble getting my cost to populate on this page: Confirmation page

There are 3 different costs depending on how soon they want their document edited by:
  • Standard - 3 Day/72 Hour Return - $5.00 Per Page
  • Expedite - 2 Day/48 Hour Return - $7.50 Per Page
  • Overnight - 1 Day/24 Hour Return - $9.00 Per Page
I am trying to have whatever selection is chosen from these options to have the price per page multiplied by the # of pages ($numCount).



Right, so on the page on which the user chooses, there is a select box? And what is the name of the parameter passed to the next page? You get this value from the $_GET or $_POST array? Then you echo this value somewhere?

At which stage does this process fail?

QUOTE


I wrote the code where it will calculate the cost for the Standard $5 option (although it won't show 2 decimal places). This is the number of pages ($numCount) multiplied by 5, but how do I automatically have it chose the correct code based on which option they chose? - Standard $5, Expedite $7.50, Overnight $9
CODE
<?php

$number = $numCount;
echo "\$" . $number*5;
$formatted = number_format($number, 2);

?>




Code for this page:
CODE

<?php
$words = $_GET['words'];
?>

<?php @$format= $_POST['serviceOption'];
if( is_array($serviceOption)){
while (list ($key, $val) = each ($serviceOption)) {
echo "$val <br>";
}
}//else{echo "not array";}  
?></td>




I think this is unnecessarily convoluted.

Why would the customer choose more than one "service option"? Does this make sense? (Ah, you have your POST arguments called things with [] on the end so PHP will read all the values? I think this is a bad idea (ugly at least), but unnecessary.

But if $serviceOption is an array, and you want to print all the values in it use

CODE

foreach($serviceOption as $opt)
{ echo "$opt<br>";
}


(Note that you have each() wrong anyway, since it returns an array of _four_ values, 2 copies of 'key' and 2 of 'val'.)

And if it is _not_ an array, what will you do?

<snip>

QUOTE

CODE

echo " ". $txtWordCount . " Words / " . $numCount . " Pages";




You've missed the point of double-quoted strings. They make it easier to include the values of (simple) variables in strings. All you need is:

echo " $txtWordCount Words / $numCount Pages";

Above you concatenated the string "\$" with something: this is where you want to use single quotes (= no $ replacement), and you can just write '$'.


QUOTE

CODE

$number = $numCount;
echo "\$" . $number*5;
$formatted = number_format($number, 2);



This is very odd. You echo $number*5, which should give the right value, but no decimal places if it's a whole number. Then you assign the number_format() value to a variable, which you never seem to use for anything...? (I presume the number_format() function is right, but it's *your* job to keep looking at the PHP manual, not mine.

HTH a bit. Please try to identify where your problem is a bit more clearly.



Nice catches Brian.

Let me try to start over and explain exactly what I am trying to accomplish and the problem I am having. I sometimes have a difficulty fully explaining my problem.

The user starts on this page: Page 1
On this page, they paste their document and proceed to get the price and they then move on to the page where more selections are made. page 2

The user selects their options from the drop down list and proceeds to this page page 3 (note: this page will not populate values into form by clicking on link I just posted, one must have selected options on page 2 and then gone to this page).

On this page (page 3) it should list the price based on what selection was made on page 2 ("Standard - 3 Day/72 Hour Return - $5.00 Per Page ", etc.) multiplied by the number of pages which I am calling ($numCount).

Each one of the options from page 2 has a value which matches its description - option value="Standard - 3 Day/72 Hour Return - $5.00 Per Page"

I have made some headway into fixing some problems in the code.

On page 2, here is the code I have for the drop down list (still the same as before):
CODE
<form action="confirmorder.php" method="post">
<select name='serviceOption[]'>
<option value="Standard - 3 Day/72 Hour Return - $5.00 Per Page"<?php echo $opt1?>>Standard - 3 Day/72 Hour Return - $5.00 Per Page</option>
<option value="Expedite - 2 Day/48 Hour Return - $7.50 Per Page"<?php echo $opt2?>>Expedite - 2 Day/48 Hour Return - $7.50 Per Page</option>
<option value="Overnight - 1 Day/24 Hour Return - $9.00 Per Page"<?php echo $opt3?>>Overnight - 1 Day/24 Hour Return - $9.00 Per Page</option>                    
       </select>


On page 3, I have new code:
CODE

//above HTML
<?php
$words = $_GET['words'];

switch ($_POST['serviceOption']) {
  case 'Standard - 3 Day/72 Hour Return - $5.00 Per Page':
    $perPage = 5;
    break;
  case 'Expedite - 2 Day/48 Hour Return - $7.50 Per Page':
    $perPage = 7.5;
    break;
  case 'Overnight - 1 Day/24 Hour Return - $9.00 Per Page':
    $perPage = 9;
    break;
  default:
    echo 'ERROR: invalid service option';
}
?>

//code where I want the price to be displayed
<?php
$cost = $numCount*5;
echo number_format($cost, 2);

$cost = $numCount*7.5;
echo number_format($cost, 2);

$cost = $numCount*9;
echo number_format($cost, 2);


?>


I know for sure that the "//code where I want the price to be displayed" is wrong. I am not sure how to write this so it will only display the price for the selected serviceOption.

Let me know if this makes sense.
SeanWoods
Update:

I got the price to show, but only by changing the code on the drop down box from:
CODE
<select name='serviceOption[]'>

to
CODE
<select name='serviceOption'>


And on the last page, I changed the code where the price is shown to:
CODE
<?php
$cost = $numCount*$perPage;
echo "\$" . number_format($cost, 2);
?>


Is there a way to keep my serviceOption selections shown on the confirmation page while taking out the []'s? That seems to be what kept the price from working.

Brian,

You mentioned:

"(Ah, you have your POST arguments called things with [] on the end so PHP will read all the values? I think this is a bad idea (ugly at least), but unnecessary.

But if $serviceOption is an array, and you want to print all the values in it use"

CODE

foreach($serviceOption as $opt)
{ echo "$opt<br>";
}


I used that to replace the code where my serviceOption is shown on the summary page (page 3):
CODE
@$format= $_POST['serviceOption'];
if( is_array($serviceOption)){
while (list ($key, $val) = each ($serviceOption)) {
echo "$val <br>";
}


I'm still having difficulty getting the serviceOption and the price to show at the same time (darn [] brackets).
Brian Chandler
You are failing to distinguish two concepts:

(a) The set of possible values for "service option".

(b) The particular value for "service option" being chosen in the form.

I think it is also a bad idea to pass form parameter including a full text description like 'Standard - 3 Day/72 Hour Return - $5.00 Per Page'. What if you have to change the price or timing?! You will have to rewrite all the bits of your program that handle this string.

It is better to use some code to represent each option -- as mnemonic as possible, so you might use 'std' 'exp' 'ovn'. Then the parameter that gets passed from the form to the handling page might look like svcopt=std (as will appear in the URL if it's GET; no obvious reason to use POST here, since this page does not submit actual changes to a database). So the value (b) is 'std' for example.

Then you need to represent (a), for which you need an ('associative') array mapping the codes to the (current) descriptions:

$serviceOptionNames = array(
'std' => 'Standard - 3 Day/72 Hour Return - $5.00 Per Page',
...);

(It would be better still to have a $serviceOptionPrice array that maps 'std' to 5 (and so on). Actually, it would be even better still to use OOP: a class for the service option...)

Then because you need the same list of service option names on at least two pages, you put the definition of array $serviceOptionNames (and prices...) in an include file, either just for these bits, called svcopt.inc / svcopt.h / whatever, or for all of the 'checkout/estimate' stuff, with whatever name you fancy. It really _is_ a good idea to find out how to keep your include files somewhere outside the document tree.

Can you see now how to get rid of that peculiar stuff you have near the beginning:

CODE

$serv = $_GET['serv']; // retrieves the data and creates a variable called $serv
$words = $_GET['words']; // as above

$servvalue = $serv; // creating a value to go in the drop down box..

if($serv == 1)
{
$serv = "Standard - 3 Day/72 Hour Return - $5.00 Per Page";
$opt1 = ' selected="selected"';
}
...


The _value_ in the select box should be the code you use ('std' etc). The string you display to the user should be the corresponding name plucked from the $serviceOptionNames array. Above you start with $serv being the code (except that it's a meaningless number), then you copy this to $servvalue, then you reuse $serv to mean its description... urggh!)
HTH


Disclaimer: Just because I recommend doing something, doesn't mean I did it properly myself...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.