Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

drop-down auto fill

Codes here !

Moderators: macek, egami, gesf

drop-down auto fill

Postby ranonymus » Tue May 28, 2013 7:26 am

hello all,
im new here and i have a little problem with my project...
i want to do a form with 4 select boxes.
when i choose an opcion in selectbox1 it will fill the selectbox2 values from database and it continues until selectbox4...
im almost there but for some reason the 3rd selectbox doesnt appear

my code is the following:

<body id="body">
<?php
$Cod_ano = $Cod_curso = $Cod_disc = $Cod_modulo = null; //declare vars

$conn = mysql_connect("localhost", "root", "");
$db = mysql_select_db('cisave',$conn);

if(isset($_GET["cod_ano"]) && is_numeric($_GET["cod_ano"]))
{
$Cod_ano = $_GET["cod_ano"];
}

if(isset($_GET["cod_curso"]) && is_numeric($_GET["cod_curso"]))
{
$Cod_curso = $_GET["cod_curso"];
}

if(isset($_GET["cod_disciplinas"]) && is_numeric($_GET["cod_disciplinas"]))
{
$Cod_disc = $_GET["cod_diciplinas"];
}

if(isset($_GET["cod_modulo"]) && is_numeric($_GET["cod_modulo"]))
{
$Cod_modulo = $_GET["cod_modulo"];
}
?>

<script language="JavaScript">
function autoSubmit()
{
var formObject = document.forms['theForm'];
formObject.submit();
}
</script>

<form id ="theForm" name="theForm" method="GET">


<select id ="my_form" name="cod_ano" onChange="autoSubmit();">
<option value="cod_ano">Selecione o Ano</option>
<?php
$sql = "SELECT * FROM Ano";
$countries = mysql_query($sql,$conn);
while($row = mysql_fetch_array($countries))
{
echo ("<option value=\"$row[Cod_ano]\" " . ($Cod_ano == $row["Cod_ano"]? " selected" : "") . ">$row[Nome_ano]</option>");
}
?>
</select>
<?php
if($Cod_ano!= null && is_numeric($Cod_ano))
{
?>
<br />
<br />



<select id ="my_form" name="cod_curso" onChange="autoSubmit();">
<option value="cod_curso">Selecione o Curso</option>
<?php
$sql = "SELECT * FROM cursos WHERE cod_ano = $Cod_ano";
$Cod_curso = mysql_query($sql,$conn);
while($row = mysql_fetch_array($Cod_curso))
{
echo ("<option value=\"$row[Cod_curso]\" " . ($Cod_curso == $row["Cod_curso"]? " selected" : "") . ">$row[Nome_curso]</option>");
}
?>
</select>
<br />
<br />
<?php
if($Cod_curso!= null && is_numeric($Cod_curso))
{
?>

<?php
$sql = "SELECT * FROM disciplinas WHERE cod_curso = $Cod_curso And cod_ano = $Cod_ano";
$Cod_disc = mysql_query($sql,$conn);
while($row = mysql_fetch_array($Cod_disc))
{
echo ("<option value=\"$row[Cod_disciplinas]\" " . ($Cod_disc == $row["Cod_disciplinas"]? " selected" : "") . ">$row[Nome_disciplina]</option>");
}
?>
</select> <br />
<br />
<?php
if($Cod_disc!= null && is_numeric($Cod_disc))
{
?>


<select id ="my_form" name="Cod_modulo" onChange="autoSubmit();">
<option value="Cod_modulo">Selecione o Modulo</option>

<?php
$sql = "SELECT * FROM Modulos WHERE cod_disciplina = $Cod_disc";
$Cod_modulo = mysql_query($sql,$conn);
while($row = mysql_fetch_array($Cod_modulo))
{
echo ("<option value=\"$row[Cod_modulo]\" " . ($Cod_modulo == $row["Cod_modulo"]? " selected" : "") . ">$row[Nome_modulo]</option>");
}
?>
</select>

<?php
} } }
?>



</form></body>


thx for any help
ranonymus
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue May 28, 2013 7:21 am

Re: drop-down auto fill

Postby johnj » Tue May 28, 2013 9:32 pm

please debug it. check if $Code_curso, $Cod_ano, $Cod_disc, and $Cod_modulo has a value. Then, check whether the sql within each "if" is picking the right records. if the third select does not have a value means that $Cod_disc does not have even one record for the while loop to execute...please check.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.