开发者

Show 2nd Dropdown based on First Drop Down PHP JS

开发者 https://www.devze.com 2023-04-13 04:50 出处:网络
PHP/HTML <select id=\"n\" name=\"userListingCateory\"> <optiondisabled=\"disabled\">Category...</option>

PHP/HTML

            <select id="n" name="userListingCateory">
      <option  disabled="disabled">Category...</option>
              <?php while($row = $sth2->fetch(PDO::FETCH_ASSOC))
              {echo "<option value=". $row['catID'] . ">" .$row['catName']."</option>";}
            unset($sth2);
            ?>
            </select> 
            <select id="n" name="userListingSubCateory">
      <option  disabled="disabled">Sub-Category...</option>
              <?php while($row = $sth3->fetch(PDO::FETCH_ASSOC))
              {echo "<option value=". $row['scatID'] . ">" .$row['scatName']."</option>";}
            unset($sth3);
            ?>

This gives me correctly t开发者_如何学编程wo dropdowns with values from database. http://i.stack.imgur.com/s9rWB.png http://i.stack.imgur.com/4psBC.png (admin please show) How do I get this (using JS or PHP) to make the 'Sub-Category' disabled until a 'Category' is selected, then based on the value chosen in Category, will enable the 'sub-category' dropdown to come up with the values pertaining to the category initially chosen??

The two database tables for category and subcategory are: Category: catID (Pk) catName

Subcategory: scatID (PK) scatName

(admin please show images)

Anyone?


First of all, you are mixing two independant states and two critical parts of an application.

When a script is generated it is sent to the browser and then there is a downtime until the response comes back. But you seem to understand that.

The other critical part is to rip out the data logic from your script from the view part. No need to have any complex MVC framework to do that, just at least get your data for both parts in the top of your script will make it simpler.

Here is an approximate way to do it, adapt it to your technology or framework:

<?php

//Read the data
$data = $data2 = array();
$datares = mysql_query('SELECT * FROM categories');
$data2res = mysql_query('SELECT * FROM subcategories');
while($datarow = mysql_fetch_assoc($datares)){
    $data[$datarow['id']] = $datarow;
}
while($data2row = mysql_fetch_assoc($data2res)){
    $data2[$data2row['parentid']][$data2row['id']] = $data2row;
}

//If this is a postback
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if(isset($_POST['userListingCateory']) && in_array($_POST['userListingCateory'], array_keys($data))){
        $subcategorydata = $data2[$_POST['userListingCategory']];
    }
}

/* put all the rest of the code you think you need here */

?>
<select name="userListingCategory">
<?php foreach($data as $dataitem){ ?>
    <option value="<?php echo $dataitem['id']; ?>"<?php if($_POST['userListingCategory'] == $dataitem['id']){ echo ' selected="SELECTED"'; } ?> ><?php echo $dataitem['label']; ?></option>
<?php } ?>
</select>

<?php if(isset($subcategorydata)){ ?>
<select name="userListingSubCateory">
<?php foreach($subcategorydata as $dataitem){ ?>
    <option value="<?php echo $dataitem['id']; ?>"<?php if($_POST['userListingSubCateory'] == $dataitem['id']){ echo ' selected="SELECTED"'; } ?> ><?php echo $dataitem['label']; ?></option>
<?php } ?>
</select>
<?php } ?>

I hope this will get you started...

Good luck

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号