Dependable dropdownlist php / mysql / ajax

Hi all,

I’m trying to code a dependable dropdownlist with php/mysql/ajax.

Goal is to select one thing in the first dropdown and depending on that option (and without reloading the page) enabling the second and add the options to the second, dependant on the first.

This is the index :

[code]<?php
require_once($_SERVER[‘DOCUMENT_ROOT’].’/dbconnect.php’);

?>

<?php $id_test=$_POST['selector']; $N = count($id_test); for($i=0; $i < $N; $i++) { $result = mysqli_query($conn, "SELECT * FROM cloud where id='$id_test[$i]'"); while($row = mysqli_fetch_array($result)) { $id_bird = $row['id']; ?>
Country : --Select Country-- <?php $sql=mysqli_query($conn, "SELECT category.id AS cat_id, category.name, category.type, cloud.id FROM cloud,category,link_category_cloud WHERE link_category_cloud.cloud_id = cloud.id AND link_category_cloud.category_id = category.id AND cloud.id='$id_test[$i]'"); while($row=mysqli_fetch_array($sql)) { $id_test=$row['cat_id']; $data=$row['name']; echo ''.$data.''; } ?>

City : --Select City--
<?php } } ?>[/code]

And this is the edit_form.php part :

[code]<?php
require_once($_SERVER[‘DOCUMENT_ROOT’].’/dbconnect.php’);
$cat_id = $_GET[‘cat_id’];
$cloud_id = $_GET[‘id’];
if($_POST[‘id_test’])
{
$id_test=$_POST[‘id_test’];
$data=$_POST[‘data’];
$sql=mysqli_query($conn, “SELECT * FROM link_category_cloud WHERE category_id={$cat_id}”);

$data=$row[‘cloud_id’];
$id_test=$_POST[‘id_test’];
echo ‘’.$id_test.’ ‘.$data.’’;

//}
}

?>[/code]

Now, it does not work. I’m not only tring to pass through ID to ‘edit_form.php’, but also $data and $id_test[i]. Any idea how I can do this?

Thank you very much, all help is so appreciated!!

Well, I did not test your code, but, when you POST from a JQuery script, it does NOT post the variables
inside the form itself. You have to add them to your AJAX routine.

You do put in the value for “id=” etc. But, you do this when the CLASS “COUNTRY” changes.
Not sure why you would want this to happen whenever a LABEL changes since it will never change.
I suspect, you meant to point that class to the SELECT (or dropdown) class instead. Usually, you would
just put in an ID name and use that instead of the class. Either way should work if you point to the
drop-down.

Try again and if it does not work, let us know… Good luck!

One other thing is a simple way to DEBUG this type of code is to just VIEW-SOURCE of the output
page where it does not work and look at what is placed into the various HTML code. (This will show
what is in place AFTER the PHP code does it’s jobs.) Might help…

Sponsor our Newsletter | Privacy Policy | Terms of Service