0

在下面的表格中,如何根据我的 URL 参数的值自动选择各种选项?

例如:http://example.com/?type=%28shirt%2C+tshirt%2C+t-shirt%29&length=25&width=17&expand=yes

        <form name="shirty" method="get">
            <select name="type" />
            <option value="(shirt, tshirt, t-shirt)">T-Shirt</option>
            <option value="(hoodie, sweatshirt)">Sweatshirt</option>
           </select>

          <select name="length" />
            <option value="select">Select a length</option>
            <option value="14">14 Inches</option>
            <option value="15">15 Inches</option>
            <option value="16">16 Inches</option>
            <option value="17">17 Inches</option>
            <option value="18">18 Inches</option>
            <option value="19">19 Inches</option>
            <option value="20">20 Inches</option>

           </select>


            <select name="width" />
            <option value="select">Select a Width</option>
            <option value="14">14 Inches</option>                
            <option value="15">15 Inches</option>
            <option value="16">16 Inches</option>
            <option value="17">17 Inches</option>
            <option value="18">18 Inches</option>
            <option value="19">19 Inches</option>
            <option value="20">20 Inches</option>
           </select>

          <input type="checkbox" name="expand" value="yes"" checked><small>include ± 1 inch?</small>

          <input type="submit" name="Submit" value="Search" />

        </form>
4

2 回答 2

0

知道了。

        <form name="shirty" method="get">
            <select name="type" />
             <?php //create the select options
                $options = "
                    <option value=\"(shirt, tshirt, t-shirt)\">T-Shirt</option>
                    <option value=\"(hoodie, sweatshirt)\">Sweatshirt</option>";
                    $saved = $type;
                $saved = (!empty($saved))? $saved: false;
                if ($saved)
                    //if there is a saved data set the option to selected
                    $options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
                //echo out the options
                echo $options; ?>
                ?>
           </select>


          <select name="length" />
            <?php //create the select options
                $options = "
                    <option value=\"14\">14 Inches</option>
                    <option value=\"15\">15 Inches</option>
                    <option value=\"16\">16 Inches</option>
                    <option value=\"17\">17 Inches</option>
                    <option value=\"18\">18 Inches</option>
                    <option value=\"19\">19 Inches</option>
                    <option value=\"20\">20 Inches</option>";

                    $saved = $length;
                $saved = (!empty($saved))? $saved: false;
                if ($saved)
                    //if there is a saved data set the option to selected
                    $options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
                //echo out the options
                echo $options; ?>
                ?>
           </select>


            <select name="width" />
            <?php //create the select options
                $options = "
                    <option value=\"14\">14 Inches</option>
                    <option value=\"15\">15 Inches</option>
                    <option value=\"16\">16 Inches</option>
                    <option value=\"17\">17 Inches</option>
                    <option value=\"18\">18 Inches</option>
                    <option value=\"19\">19 Inches</option>
                    <option value=\"20\">20 Inches</option>";
                    $saved = $width;
                $saved = (!empty($saved))? $saved: false;
                if ($saved)
                    //if there is a saved data set the option to selected
                    $options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
                //echo out the options
                echo $options; ?>
                ?>
           </select>

          <input type="checkbox" name="expand" value="yes"" checked><small>include ± 1 inch?</small>

          <input type="submit" name="Submit" value="Search" />

          </form>
于 2012-04-23T05:27:23.467 回答
0
<?php   
  //This example creates a select element and marks selected option using selected_id in URL 

        $attributesAssocArray = array(
            "id"=>"someId","class"=>"someClass"
        );

        $optionsArray = array(
            0=>"choose...",1=>"one",2=>"two",3=>"three"
        );

        $selectedId = (!empty($_GET['selected_id'])&&(is_numeric($_GET['selected_id'])))?  intval($_GET['selected_id']):-1;

        echo generate_select($attributesAssocArray,$optionsArray,$selectedId);

        function generate_select($attributesAssocArray=array(),$optionsArray=array(),$selected=-1)
        {
            $attributes = generate_attributes($attributesAssocArray);
            $html = '<select '.$attributes.'>';
            foreach ($optionsArray as  $value => $name)
            {
                $isSelected = $value==$selected ? " selected" : "";
                $html.= '<option value="'.$value.'"'.$isSelected.'>'.$name.'</option>';
            }
            $html.='</select>';
            return $html;
        }

        function generate_attributes($attributesAssocArray)
        {
            $attributes='';
            foreach ($attributesAssocArray as $name => $value)
            {
                $attributes.= $name.'="'.$value.'" ';
            }
            return $attributes;
        }
?>
于 2014-03-15T22:12:59.897 回答