Page 1 of 1

How to add shipping value to the total?

Posted: Mon Apr 30, 2007 1:58 pm
by danedner
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey,

I have a pretty simple question but I'm having a hard time finding a workabout/solution on the net. I'm new at this so I don't know all the tricks and turns. What I'm trying to accomplish is:

Take the value of the desired shipping selected and onchange, add that value (either 7$ or 15$)  to the total. Thanks

Here is the code

Code: Select all

<?php 
define("SUB_TOTAL","Sub-Total: ");
define("_GST","Tax(6%): ");
define("_TOTAL","Total: "); 
 
$totalCost = 50;
$gstTax = 0.06;
?>

<table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr> 
      <td height="27"> <div align="right"><font face="verdana" size="1" color="black"><?php echo SUB_TOTAL; ?> 
        $<?php echo number_format($totalCost, 2, ".", ","); ?> </font> </div></td>
    </tr>
    <tr> 
      <td height="18"><div align="right"><font face="verdana" size="1" color="black"><?php echo _GST; ?> 
        $ 
        <?php 
			$sum=$totalCost * $gstTax; 
			echo number_format($sum, 2, ".", ",");?>
        </font></div></td>
    </tr>
    <tr> 
      <td><div align="right"><font face="verdana" size="1" color="black"> <br>
        <select name="shipping">
          <option value="7">Shipping Local 7$</option>
          <option value="15">Shipping Surrounding 15$</option>
        </select>         
        <br>
          <br>
          </font> </div>
        <div align="right"><font face="verdana" size="1" color="black"><b><?php echo _TOTAL; ?>$ 
          <?php 
			$total=$sum + $totalCost; 
			echo number_format($total, 2, ".", ",");?>
          </b></font></div></td>
    </tr>
  </table>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Apr 30, 2007 2:21 pm
by volka
Hi,
here's an example for you to test

Code: Select all

<html>
  <head><title>form/select test</title></head>
  <body>
    <pre>GET parameters sent with this request: <?php var_export($_GET); ?></pre>
<?php
if (isset($_GET['shipping'])) {
  echo 'shipping: ', $_GET['shipping'], "<hr />\n";
}
?>
    <form method="get" action="<?php echo basename(__FILE__); ?>">
      <div>
        <select name="shipping">
          <option value="7">Shipping Local 7$</option>
          <option value="15">Shipping Surrounding 15$</option> 
        </select>
        <input type="submit" />
      </div>
    </form>
  </body>
</html>

Thanks

Posted: Mon Apr 30, 2007 3:10 pm
by danedner
Great!

It looks like it could do the job.

However, there 2 things.

1) upon submiting, the option value ("7" Shipping Local) always reset to default.
2) how can it be done on the fly (upon selecting and not having to click the submit button).

Thank you so much !

Posted: Mon Apr 30, 2007 4:52 pm
by volka
php runs server-side. The client has to perform a new http request to pass data to a php script. E.g. you can use the onchange event handler to submit the form.
You can add the attribute selected="selected" of an option element to make it the default selection.

Code: Select all

<html>
  <head><title>form/select test</title></head>
  <body>
    <pre>GET parameters sent with this request: <?php var_export($_GET); ?></pre>
<?php
if (isset($_GET['shipping'])) {
  echo 'shipping: ', $_GET['shipping'], "<hr />\n";
}
?>
    <form method="get" action="<?php echo basename(__FILE__); ?>">
      <div>
        <select name="shipping" onchange="this.form.submit()">
          <option value="7">Shipping Local 7$</option>
          <option value="15" selected="selected" >Shipping Surrounding 15$</option>
        </select>
        <input type="submit" />
      </div>
    </form>
  </body>
</html>

Re: Thanks

Posted: Mon Apr 30, 2007 4:57 pm
by RobertGonzalez
danedner wrote:2) how can it be done on the fly (upon selecting and not having to click the submit button).
Use Javascript, not PHP to do it. As volka said, what you are asking should be done on the client. Though there is nothing wrong with pushing it to the server, and you should probably put that in just in case the user has Javascript disabled.