• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Display variable based on drop down selection

I have the following in an order form...

Shipping Method
<select name='shipping' id='shipping'>
<option value="Ground">Ground</option>
<option value="2nd Day">2nd Day</option>
<option value="Overnight">Overnight</option>
<option value="Priority Overnight">Priority Overnight</option>
</select>

Depending on the option selected I need to call a php variable already setup to adjust the subtotal. This is on the fly before submitting the order form. Can this be done and how?
0
pixelscape
Asked:
pixelscape
  • 4
1 Solution
 
leakim971PluritechnicianCommented:
A php variable? A session variable?

Build you dropdown with the value inside instead the text

<select name='shipping' id='shipping'>
<option value="10">Ground</option>
<option value="15">2nd Day</option>
<option value="20">Overnight</option>
<option value="30">Priority Overnight</option>
</select>

Open in new window


0
 
leakim971PluritechnicianCommented:
you can't access server side variables on the client side (browser)
- You can use ajax to retrieve value in the database
- You can use ajax to retrieve value of session variables

you can put value(s) in hidden field to use them later on the client side :

echo '<input type="hidden" name="shipping_charges_amount0" value="10" />';
echo '<input type="hidden" name="shipping_charges_amount1" value="15" />';
echo '<input type="hidden" name="shipping_charges_amount2" value="20" />';
echo '<input type="hidden" name="shipping_charges_amount3" value="30" />';

Open in new window


or :

echo '<input type="hidden" name="shipping_charges_amounts"  id="shipping_charges_amounts" value="10;15;20;30" />';

Open in new window


0
 
pixelscapeAuthor Commented:
Session variable...

if I select Overnight with value of 20 how do I echo/print that without submitting the form?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leakim971PluritechnicianCommented:
using ajax

simle javascript :

// selectedindex is the selected index in the dropdown (zero based)
function getShippingCharges(selectedIndex) {
	if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open("GET", "get_shipping_charges_amounts.php?si="+selectedIndex, false);
	xmlhttp.send();
	return xmlhttp.responseText
}

Open in new window


With jQuery :
function getShippingCharges(selectedIndex) {
       return $.ajax({"url":"get_shipping_charges_amounts.php?si="+selectedIndex, "async":false}).responseText;
}

Open in new window


you dropdown :

<select name='shipping' id='shipping' onchange="getShippingCharges(this.selectedIndex)">

Open in new window


server side (you've set four session variables, one for each options : freight1, freight3,freight3,freight4 :

<?php 
   session_start(); ?> 
   echo $_SESSION["freight" . $_GET["si"] ]; //$_GET["si"] return the index of the selected index so you can return the right value
?php>

Open in new window



But you should use the hidden field method or better embed each value in the dropdown itselft instead the text...
/!\ AND DO A NEW CHECKING ON THE SERVER SIDE !!! /!\

0
 
Tyler LaczkoCommented:
PHP is server size
Javascript is client side.

You can just use java script to do this.
add a function call to your dropdown
onchange='callfunc()'

function callfunc()
{
//getvalues
//calc values
//document.write value out
}

alot simpler than ajax.
0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now