简体   繁体   中英

Multiple Drop down Not Working in HTML

Hi Friends Good Morning
I have Created an Page like Shopping Cart in that i am using many Drop down field [example 8 drop downs in a single page].Using drop down i can add as much as quantity i needed and amount should be increased on the basis of Quantity changes using PHP
i have the following Problems while running it.

1.When i click one drop down the value changes to 0 automatically.

Second Problem has been Solved

2.SOLVED

2.While Clicking the Second Drop down box the page get refreshed and First value has been cleared.

Here is the Coding

<form action="" method="get" name="months">
<select id="selectId" onchange="this.form.submit()" name="month" >
 <option   value="0">0</option>
 <option value="1">1</option>
 <option  value="2">2</option>
 <option  value="3">3</option>
 <option  value="4">4</option>
 <option  value="5">5</option>
 <option  value="6">6</option>
 <option  value="7">7</option>
 <option  value="8">8</option>
 <option  value="9">9</option>
 <option  value="10">10</option>
 <option  value="11">11</option>
 <option  value="12">12</option>
 </select><br><br></form>
 <?php
$rs='5000';
$mul=$_REQUEST['month'];
$multiply= $rs*$mul;
?></td><td><?php echo "Rs.$multiply"; ?></td></tr> 

<form action="" method="get" name="months">
<select id="selectId" onchange="this.form.submit()" name="month1" >
 <option   value="0">0</option>
 <option value="1">1</option>
 <option  value="2">2</option>
 <option  value="3">3</option>
 <option  value="4">4</option>
 <option  value="5">5</option>
 <option  value="6">6</option>
 <option  value="7">7</option>
 <option  value="8">8</option>
 <option  value="9">9</option>
 <option  value="10">10</option>
 <option  value="11">11</option>
 <option  value="12">12</option>
 </select><br><br></form>
 <?php
$rs='1000';
$mul=$_REQUEST['month1'];
$multiply= $rs*$mul;
?></td><td><?php echo "Rs.$multiply"; ?></td></tr>

For More Information here is the Screen Shot --> 点击这里 <--

onchange="this.form.submit()" is causing the page to submit and refresh every time you change the selected item.

This means, you either use a session to remember the values, or you can remove "onchange="this.form.submit()" and put a submit button inside the form:

<input type="submit" value="Submit">

If you are trying not to refresh the page, you need to use ajax to call a web-service that will return JSON.

Try this one:

<html>
<head>
    <title>SO - Cart</title>
</head>
<body>
<form action="" method="get" name="months">
<select id="selectId" onchange="this.form.submit()" name="month" >
 <?php
for($i=0;$i<=12;$i++){
    if($i==$_REQUEST['month']) {
        echo '<option selected="selected"   value='.$i.'>'.$i.'</option>';
    }
    else{
        echo '<option   value="'.$i.'">'.$i.'</option>';
    }
}
?>
</select><br><br>
</form>
<?php
    $mul = 0;
    $rs='5000';
    $mul=$_GET['month'];
    $multiply= $rs*$mul;
?>
<?php echo "Rs ".$multiply; ?>
</body>
</html>

1) you are missing an if statement in your <option> to set selected="selected" for each of your lines do the following:

<option value="1" <?php if($_REQUEST['month'] == 1){echo 'selected="selected"';}?>>1</option>

probably best to do it in a loop.

2) I don't see a "second drop down box" however, in the example above - you have an event handler that submits the form (aka refreshes the page) after it is changed.

In stead of option of select use php code to create option, it will also reduce code in the page.

<?php
for($i=0;$i<=12;$i++){
    if($i==$_REQUEST['month']) {
        echo '<option selected="selected"   value="'.$i.'">"'.$i.'"</option>';
    }
    else{
        echo '<option   value="'.$i.'">"'.$i.'"</option>';
    }
}
?>

For each select box use can use above code. but need to update request parameter $_REQUEST['month'] in if condition.

Working Code Modified From above Duikboot's Code.

Just Inserted my PHP code above the Form.It works Fine with it.

<html>
<head>
<title>SO - Cart</title>
</head>
<body><?php
$mul = 0;
$rs='5000';
$mul=$_GET['month'];
$multiply1= $rs*$mul;
?>

<?php
$mul = 0;
$rs='2000';
$mul=$_GET['months'];
$multiply= $rs*$mul;
?>

<form action="" method="get" name="months">
<select id="selectId" onchange="this.form.submit()" name="month" >
 <?php
for($i=0;$i<=12;$i++){
if($i==$_REQUEST['month']) {
    echo '<option selected="selected"   value='.$i.'>'.$i.'</option>';
}
else{
    echo '<option   value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<?php echo "Rs ".$multiply1; ?><br><br>
<select id="selectId" onchange="this.form.submit()"  name="months" >
 <?php
for($i=0;$i<=12;$i++){
if($i==$_REQUEST['months']) {
    echo '<option selected="selected"   value='.$i.'>'.$i.'</option>';
}
else{
    echo '<option   value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<?php echo "Rs ".$multiply; ?>
<input type="submit" />
</form><br><br>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM