Greetings all,

I have multiple list options linked, like Country, city, town, when e.g france is selected it gives all the cities in france in the following option list. and when say paris is selected it gives you all the towns in paris and third category.

I have found an example online using a postgre DB and php + javascript.


I am using a Mysql DB and the rest is the same. Code the has no errors but doesnt select anything from the DB!!!

The table design:

Table: category
id int8 primary key not null
category varchar unique not null
description varchar

Table: sub_category
id int8 primary key not null
sub_category varchar unique not null
category varchar foreign key category.category
description varchar

Table: article

id int8 primary key not null
category varchar foreign key category.category
sub_category varchar foreign key sub_category.sub_category
title varchar
short_description varchar
description varchar

full code:

<html>
<head>
<title>ZB Knowledge Base: Add Article</title>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test");

$value = "";
if ( isset( $_POST[ 'category' ] ) ) {
$value = $_POST[ 'category' ];
}

?>
. <script language="JavaScript">
function resubmit()
{
document.myform.action="add_article.php";
document.myform.submit();
}
function process()
{
document.myform.action="process.php";
document.myform.submit();
}
</script>
<b>Add New KB Article</b><br /><br />

<form name="myform" method="post" action="process.php">
<input type="hidden" name="action" value="add_article" />

Category: <select name="category" onchange="resubmit()">
<?php
$result = mysql_query( $connection, "SELECT * FROM category" );

for ( $count = 0; $count < $num_rows = mysql_num_rows($result); $count++ )
{

$category = mysql_result ( $query_result, $count, 1 );
if ( $count == 0 )
{
$init_cat = $category;
}
if ( $category == $value )
{
print( "<option selected>$category</option>\n" );
$selected = $category;
}
else
{
print( "<option>$category</option>\n" );
}
}
?>
</select>

Sub Category: <select name="subcat">
<?php
if ( $value == "" )
{
$result = mysql_query( $connection,
"SELECT * FROM sub_category WHERE category = '$init_cat'" );

for ( $count = 0; $count < $num_rows = mysql_num_rows( $query_result ); $count++ )
{
$subcat = pg_result( $query_result, $count, 3 );
print( "<option>$subcat</option>\n" );
}
} else {
$result = mysql_query( $connection,
"SELECT * FROM sub_category WHERE category = '$selected'" );

for ( $count = 0; $count < pg_numrows( $query_result ); $count++ )
{
$subcat = pg_result( $query_result, $count, 3 );
print( "<option>$subcat</option>\n" );
}
}
?>
</select>
<br /><br />
<!-- OTHER FORM ITEMS HERE -->
<input type="button" name="post_data" value="Add It!" onclick="process()" />
</form>
</body>
</html>


Any suggestions much appreciated.

i use ajax to accomplish this. by using ajax the page will not have to reload and you will only have to submit the form one time to add_article.php. if you need an example let me know and i will post the url.

i use ajax to accomplish this. by using ajax the page will not have to reload and you will only have to submit the form one time to add_article.php. if you need an example let me know and i will post the url.

If you can post the example please, i would be grateful

there are a lot of errors i saw in your code. do you want me to fix them?

Can you fix it?

This article has been dead for over six months. Start a new discussion instead.