mysqli - Dynamic populating the drop down list based on the selected value of first list in php -
i have small php page contains 2 drop down lists
i need populate second 1 according result selected in first drop down list. possible? in other words need use value selected first drop down list , use in db query used populate second drop down list (but should populated upon selection of first drop down list.
if possible hints please? (you can assume able populate first drop down list db)
connecting db
<?php include ("db_connect.php"); ?>
javascript:
function abc() { var val = document.getelementbyid('customer_type').value; self.location='add.php?customer_type=' + val ; }
php
<div class="heading"> <h1>add products</h1> </div> <div class="content"> <form action="" method="post" enctype="multipart/form-data"> <span class="text_line">select customer type:</span> <select name="customer_type" id="customer_type" onchange="abc()" style="line-height: 30px; height: 30px; width:18%;"> <option class="tfield"> customer type </option> <?php $result = mysqli_query($con, "select * cust_type"); while($row = mysqli_fetch_array($result)) { $cust_name = $row['customer_type']; echo "<option value='$cust_name'> $cust_name </option>"; } ?> </select><br /> <span class="text_line">select shoes type:</span> <select name="shoe_type" style="line-height: 30px; height: 30px; width:18%;"> <option class="tfield"> shoes type </option> <?php $result1 = mysqli_query($con, "select * shoes_type"); while($row1 = mysqli_fetch_array($result1)) { $shoesid = $row1['gender_id']; $shoes_cat = $row1['shoes_category']; echo "<option value= '$shoesid'> $shoes_cat </option>"; } ?> </select><br /> <span class="text_line">select image:</span> <input type="file" name="upload" style="line-height: 30px; height: 30px; width:18%; color:#fff;"/><br /> <span class="text_line">price:</span> <input type="text" name="price" class="tfield"/><br /> <span class="text_line">description:</span> <textarea name="descrip" cols="25" class="tfield"></textarea><br /> <div class="button1"> <input type="submit" name="submit" value="submit" class="sign" /> </div> </form> </div> <?php if(isset($_post['submit'])) { $cust_type = $_post['customer_type']; $prod_price = $_post['price']; $shoe_img = $_files["upload"]["name"]; $prod_descrip = $_post['descrip']; $shoeid = $_post['shoe_type']; if($cust_type == "" || $prod_price == "" || $shoe_img == "" || $prod_descrip == "") { ?> <!-- <div style="color:#fff; text-align:center; font-size:20px; font-family:arial, helvetica, sans-serif; margin-top:2%;">--> <?php echo "please fill fields";?> </div> <?php } else { $result2 = mysqli_query($con, "select * shoes_type shoes_id = '$shoeid'"); while($row2 = mysqli_fetch_array($result2)) { $shoesid = $row2['shoes_id']; $shoetype = $row2['shoes_category']; } $sql = mysqli_query($con, "insert product(shoes_id,customer_type,shoe_type,shoe_price,image,description) values('$shoesid','$cust_type','$shoetype','$prod_price','$shoe_img','$prod_descrip')"); ?> <!--<div style="color:#fff; text-align:center; font-size:20px; font-family:arial, helvetica, sans-serif; margin-top:5%;">--> <?php echo "data inserted !!!"; ?> <?php //echo "<meta http-equiv='refresh' content='1'; url='index.php?id=addproduct'>"; ?> </div> <?php }//else loop ends }//if loop ends ?>
i not experienced in ajax , jquery. please suggest solution in javascript , php
i'm not cleared whether having customer_type in shoes_type table. still try this,
connecting db
<?php include ("db_connect.php"); ?>
javascript:
function abc(){ var val = document.getelementbyid('customer_type').value; $.post("getseconddropdown.php",{ gender_id:val}, function( data ) { $("#shoe_type").html(data); }); }
your main php
<div class="heading"> <h1>add products</h1> </div> <div class="content"> <form action="" method="post" enctype="multipart/form-data"> <span class="text_line">select customer type:</span> <select name="customer_type" id="customer_type" onchange="abc()" style="line-height: 30px; height: 30px; width:18%;"> <option class="tfield"> customer type </option> <?php $result = mysqli_query($con, "select * cust_type"); while($row = mysqli_fetch_array($result)) { $cust_name = $row['customer_type']; $gender_id = $row['gender_id']; echo "<option value='$gender_id '> $cust_name </option>"; } ?> </select><br /> <span class="text_line">select shoes type:</span> <select name="shoe_type" id="shoe_type" style="line-height: 30px; height: 30px; width:18%;"> </select><br /> <span class="text_line">select image:</span> <input type="file" name="upload" style="line-height: 30px; height: 30px; width:18%; color:#fff;"/><br /> <span class="text_line">price:</span> <input type="text" name="price" class="tfield"/><br /> <span class="text_line">description:</span> <textarea name="descrip" cols="25" class="tfield"></textarea><br /> <div class="button1"> <input type="submit" name="submit" value="submit" class="sign" /> </div> </form> </div> <?php if(isset($_post['submit'])) { $cust_type = $_post['customer_type']; $prod_price = $_post['price']; $shoe_img = $_files["upload"]["name"]; $prod_descrip = $_post['descrip']; $shoeid = $_post['shoe_type']; if($cust_type == "" || $prod_price == "" || $shoe_img == "" || $prod_descrip == "") { ?> <!-- <div style="color:#fff; text-align:center; font-size:20px; font-family:arial, helvetica, sans-serif; margin-top:2%;">--> <?php echo "please fill fields";?> </div> <?php } else { $result2 = mysqli_query($con, "select * shoes_type shoes_id = '$shoeid'"); while($row2 = mysqli_fetch_array($result2)) { $shoesid = $row2['shoes_id']; $shoetype = $row2['shoes_category']; } $sql = mysqli_query($con, "insert product(shoes_id,customer_type,shoe_type,shoe_price,image,description) values('$shoesid','$cust_type','$shoetype','$prod_price','$shoe_img','$prod_descrip')"); ?> <!--<div style="color:#fff; text-align:center; font-size:20px; font-family:arial, helvetica, sans-serif; margin-top:5%;">--> <?php echo "data inserted !!!"; ?> <?php //echo "<meta http-equiv='refresh' content='1'; url='index.php?id=addproduct'>"; ?> </div> <?php }//else loop ends }//if loop ends ?>
and new php file fetch values in second select box.
getseconddropdown.php
<?php $gender_id =$_post['gender_id ']; $option=""; $result1 = mysqli_query($con, "select * shoes_type gender_id ='$gender_id'"); while($row1 = mysqli_fetch_array($result1)) { $shoesid = $row1['gender_id']; $shoes_cat = $row1['shoes_category']; $option.= "<option value= '$shoesid'> $shoes_cat </option>"; } echo $option; ?>
one more thing, you'll need add jquery library work.
Comments
Post a Comment