Jump to content

Linked dynamic select list issue


thursgun

Recommended Posts

Hi,

 

I really really really need some help with this annoying issue (I've searched  in the web a lot because im too lazy at making post, but this is pissing me off  :-[ )

 

 

The following code show 3 "linked" select lists. "Proyecto", "Fase" and "Etapa". So I choose an item from proyecto, and the select box "Fase" shows the items associated to the item selected. Same with "Fase" and "Etapa".

 

When I select an item from Proyecto, the page reloads, and the selected item is being displayed on the select box of Proyecto. However, when I do the same with Fase, the item is not displayed and the message "-- Elija una fase --" appears instead. (this doesn't mean that the Etapa select doesn't work, because it does).

 

P.S: Im sorry if the code is not clear enough, but feel free to ask anything about it.

 

Heres the code:

(some words in spanish, but nothing serious)

<SCRIPT language=JavaScript>
function reload(form)
{
	var val_proyecto=form.proyecto.options[form.proyecto.options.selectedIndex].value;
	//var val_fase=form.fase.options[form.fase.options.selectedIndex].value;

	self.location='xxx.php?proyecto=' + val_proyecto;// + '& fase=' + val_fase ;
}

function reload2(form, val_proyecto)
{
	var val_fase=form.fase.options[form.fase.options.selectedIndex].value;
	var val_etapa=form.etapa.options[form.etapa.options.selectedIndex].value;
	self.location='xxx.php?proyecto=' + val_proyecto + '& fase=' + val_fase ;
}
</script>

<?php
$conn = pg_connect("host=localhost port=5432 password='' user=postgres dbname= DB ");
if (pg_ErrorMessage($conn))
{ 
	echo "<p><b>Ocurrio un error conectando a la base de datos: .</b></p>"; 
	exit;
}

$query_proyecto=pg_query("SELECT * FROM proyecto");

@$proyecto=$_GET['proyecto'];
@$fase=$_GET['fase'];

if(isset($proyecto) and strlen($proyecto) > 0)
{
	$query_fase=pg_query("SELECT * FROM fase WHERE id_proyecto=".$proyecto);
	if(isset($fase) and strlen($fase) > 0)
	{
		$query_etapa=pg_query("SELECT * FROM etapa WHERE id_fase=".$fase. " AND id_proyecto=".$proyecto);
	}
}


echo "<form method=post name=f1 action='dd-check.php'>";
	//lista de proyectos
	echo "<select name='proyecto' onchange=\"reload(this.form)\"><option value=''>-- Elija un proyecto --</option>";
		while($select_proyecto = pg_fetch_array($query_proyecto))
		{
			if($select_proyecto['id_proyecto']==@$proyecto)
			{
				echo "<option selected value='".$select_proyecto['id_proyecto']."'>".$select_proyecto['nombre_proyecto']."</option>";
			}
			else
			{
				echo "<option value='".$select_proyecto['id_proyecto']."'>".$select_proyecto['nombre_proyecto']."</option>";
			}
		}
	echo "</select>";

	//lista de fases
	echo "<select name='fase' onchange=\"reload2(this.form,".$proyecto.")\"><option value=''>-- Elija una fase --</option>";
		while($select_fase = pg_fetch_array($query_fase)) 
		{
			if($select_fase['id_fase']==@$fase)
			{
				echo "<option value='".$select_fase['id_fase']."'>".$select_fase['nombre_fase']."</option>";
			}
			else
			{
				echo "<option value='".$select_fase['id_fase']."'>".$select_fase['nombre_fase']."</option>";
			}
		}
	echo "</select>";

	//lista de etapas
	echo "<select name='etapa'><option value=''>-- Elija una etapa --</option>";
		while($select_etapa = pg_fetch_array($query_etapa)) 
		{
			echo "<option value='".$select_etapa['id_etapa']."'>".$select_etapa['nombre_etapa']."</option>";
		}
	echo "</select>";

	echo "<input type=submit value=Submit>";
echo "</form>";
?>

Link to comment
https://forums.phpfreaks.com/topic/160861-linked-dynamic-select-list-issue/
Share on other sites

Solved

 

Ok, if anyone, someday somewhere needs this info, the problem was that I missed the word "selected" here :

 

(no code tag so I can highlight the word  ;D)

 

 

//lista de fases

      echo "<select name='fase' onchange=\"reload2(this.form,".$proyecto.")\"><option value=''>-- Elija una fase --</option>";

        while($select_fase = pg_fetch_array($query_fase))

        {

            if($select_fase['id_fase']==@$fase)

            {

              echo "<option selected value='".$select_fase['id_fase']."'>".$select_fase['nombre_fase']."</option>";

            }

            else

            {

              echo "<option value='".$select_fase['id_fase']."'>".$select_fase['nombre_fase']."</option>";

            }

        }

      echo "</select>";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.