Need help with PHP code for drop down menu

Discussion in 'PHP' started by Cyrus255, May 20, 2010.

  1. #1
    I'm a bit new to php so if can someone fix my code or tell me how can I create a FORM that when an option is picked it changes a numerical value outside of the form? The base value is $100 and then changes based on size. Part of the problem is I don't know how to make a php code to describe if a certain option is picked to set a $value for that option.
    
    <form name="Form1">
    <select name="Sizes" size="1">
      <option value="<?php $size = small ?>" selected >Small</option>
      <option value="<?php $size = medium ?>" selected >Medium</option>
      <option value="<?php $size = large ?>" selected >Large</option>
    </select>
    </form>
    
    Code (markup):
    I want to write some sort of code like:
    
    if $size = small, then $sizevalue = 200  
    if $size = medium, then $sizevalue = 300
    if $size = large, then $sizevalue = 400
    
    Code (markup):
    then to set another form like the one above that also affects it, but using currency.
    
    if $currency = US Dollar, then $totalcost = $($sizevalue x 1.00)
    if $currency = Euro, then $totalcost = € ($sizevalue x 0.80)
    if $currency = British Pound, then $totalcost = £ ($sizevalue x 0.60)
    
    Code (markup):
    Can someone write this code for me? It should be simple enough. The usefulness of it is that I could create a chart allowing users to pick currency or size of the product and the number (cost of the product) displayed would change based on that input. Of course it doesn't have to be in PHP, any possible code would be great.
     
    Last edited: May 20, 2010
    Cyrus255, May 20, 2010 IP
  2. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #2
    I have created a code but it doesn't seem to work.

    
    
    <form name="theForm"  method="post" action="echo_post.php">
    <?php { echo "<select name=\"size\">"; 
    
    $size_array = array( 
    
        array ('01', 'Small'), 
    
        array ('02', 'Medium'), 
    
        array ('03', 'Large')
    ); 
    
        foreach($size_array as $subarray) { 
    
            list($num, $text) = $subarray; 
    
         
    
            if($num==$size){ 
    
                echo "<option value=\"$num\" selected>$text</option>"; 
    
            } else { 
    
                echo "<option value=\"$num\">$text</option>"; 
    
            } 
    
        } 
    
    echo "</select>"; 
    
    }
    ?>
    </form>
    
    <?php if ( $num == 02 ) { echo "Small Size";
    } else {
    	echo "Pick a Different Size";
    }
    
    ?>
    
    Code (markup):
    That code doesn't seem to work. The problem is I don't know how to write in php a "if this form option is selected" command?
     
    Cyrus255, May 20, 2010 IP
  3. abstractworld

    abstractworld Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well you will have to use javascript for this...

    <script>
    function update()
    {
    document.getElementById('txtbox').value = document.getElementById('sizeOp').value;
    }
    </script>
    <form name="Form1">
    <select name="Sizes" size="1" onChange="update()" id='sizeOp'>
    <option value="100" selected >Small</option>
    <option value="200">Medium</option>
    <option value="300">Large</option>
    </select>
    </form>
    <input type="text" id="txtbox" />


    I hope there isnt any syntax error...
    thanx
     
    abstractworld, May 21, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <form method="post">
    <strong>Select size (required):</strong>
    <select name="size">
      <option value="small">Small</option>
      <option value="medium">Medium</option>
      <option value="large">Large</option>
    </select>
    <h3>Or select size AND currency:</h3>
    <strong>Select Currency (optional):</strong>
    <select name="currency">
    <option value="">NONE</option>
    <option value="dollar">US Dollar</option>
    <option value="euro">Euro</option>
    <option value="pounds">British Pound</option>
    </select>
    <br/>
    <input type="submit" name="submit" value="Submit">
    </form>
    
    <?php
    //check form is submitted...
    if (isset($_POST['submit'])){
    $output = null;
    
    //give the sizes a value...
    switch ($_POST['size']) {
        case "small":
            $size_value = 200;
            break;
        case "medium":
            $size_value = 300;
            break;
        case "large":
            $size_value = 400;
            break;
    }
    
    //if size and currency are selected...
    if (!empty($_POST['currency']) && isset($_POST['size'])){
    switch ($_POST['currency']) {
        case "dollar":
            $total_cost = "$".$size_value * 1.00;
            break;
        case "euro":
             $total_cost = "€".$size_value * 0.80;
            break;
        case "pounds":
             $total_cost = "£".$size_value * 0.60;
            break;
    }
    $output = $total_cost;
    
    //if size is only selected return the size value..
    } elseif (isset($_POST['size']) && empty($_POST['currency'])){
    $output = $size_value;
    }
    
    echo $output;
    }
    
    ?>
    PHP:
     
    danx10, May 23, 2010 IP
  5. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #5

    This will definitely work!
    Try this and say your results!
     
    roopajyothi, May 25, 2010 IP