mdmartiny Posted March 27, 2012 Share Posted March 27, 2012 I hope that this is in the right forum. I am in the process of writing a block of code that will allow the use of chained select boxes. I am pretty sure that it is a AJAX issue. When I select from the first select box. The second select box does not fill with anything. It gives me an empty select box. Here is the code that I am using. Anyone have any suggestions on what to do? <?php $host = 'localhost'; $username = 'root'; $dbname = 'classified'; $dbpassword = ''; $connection = mysql_connect($host, $username, $dbpassword); $db = mysql_select_db($dbname, $connection); if (!$connection) { die('Could not make a connection to MySql Database' . mysql_error()); } if (!$db) { die('Could not connect to DB $dbname' . mysql_error()); } function showCategory() { $select_sql = "SELECT * FROM category ORDER BY cat_id"; $cat_select = mysql_query($select_sql); $category = "<option value='0'>Select a Category......</option>"; while ($row = mysql_fetch_array($cat_select)) { $category .= "<option value='" . $row['cat_id'] . "'>" . $row['category'] . "</option>\n "; } return $category; } function subCategory() { $sub_sql = "Select * From sub_cat where cat_id='" . $_POST['id'] . "'"; $sub_select = mysql_query($sub_sql); $sub = "<option value='0'>Select a Sub Category</option>"; while ($row = mysql_fetch_array($sub_select)) { $sub .= "<option value='" . $row['sub_cat_id'] . "'>" . $row['sub_cat'] . "</option>\n "; } return $sub; echo $_POST['id']; } ?> <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("select#sub_category").attr("disabled","disabled"); $("select#category").change(function(){ $("select#sub_category").attr("disabled","disabled"); $("select#sub_category").html("<option>wait...</option>"); var id = $("select#category option:selected").attr('value'); $.post("<?php echo $_SERVER['PHP_SELF']; ?>", {id:id}, function(data){ $("select#sub_category").removeAttr("disabled"); $("select#sub_category").html(data); }); }); }); </script> </head> <body> <br/><form action="" method="post" name="cars"> <select id="category" name="category"> <?php $select_sql = "SELECT * FROM category ORDER BY cat_id"; $cat_select = mysql_query($select_sql); $category = "<option value='0'>Category</option>"; while ($row = mysql_fetch_array($cat_select)) { $category .= "<option value='" . $row['cat_id'] . "'>" . $row['category'] . "</option>\n "; } echo $category; ?> </select> <select id="sub_category" name="sub_category"> <?php $sub_sql = "Select * From sub_cat where cat_id='" . $_POST['id'] . "'"; $sub_select = mysql_query($sub_sql); $sub = "<option value='0'>Sub Category</option>"; while ($row = mysql_fetch_array($sub_select)) { $sub .= "<option value='" . $row['sub_cat_id'] . "'>" . $row['sub_cat'] . "</option>\n "; } echo $sub; echo $sub_sql; ?> </select> <input type="submit" name="fire" value="GO"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/259830-submitting-an-id-through-post/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.