Display variable based on drop down selection

Posted on 2011-05-12
Last Modified: 2012-05-11
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>

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?
Question by:pixelscape
    LVL 81

    Expert Comment

    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>

    Open in new window

    LVL 81

    Expert Comment

    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


    Author Comment

    Session variable...

    if I select Overnight with value of 20 how do I echo/print that without submitting the form?
    LVL 81

    Accepted Solution

    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");"GET", "get_shipping_charges_amounts.php?si="+selectedIndex, false);
    	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 :

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

    Open in new window

    But you should use the hidden field method or better embed each value in the dropdown itselft instead the text...

    LVL 10

    Expert Comment

    by:Tyler Laczko
    PHP is server size
    Javascript is client side.

    You can just use java script to do this.
    add a function call to your dropdown

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

    alot simpler than ajax.
    LVL 81

    Expert Comment

    Thanks for the points!

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now