Page 1 of 1

Drop-down Get return from default[SOLVE]

Posted: Mon Jul 09, 2012 10:30 pm
by jayson.ph
Hi All,

i just ask why? when i select record from the drop-down-list it will return to a default. see image below:

Image

as you notice i select category and it success to change from the address bar. but i wonder why in the circle portion it return to a default netbook. here my code.

Code: Select all

var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='insert_request.php?cat=' + val ;

Code: Select all

		$query = "SELECT DISTINCT cat_name,cat_it FROM tbl_category";
			$x = mysql_query($query);
		
			echo "<p>Category:<select name ='cat' onchange=\"reload(this.form)\">";
			while($v = mysql_fetch_array($x))
		{
			if($v['cat_name'])
			{
			echo "<option value = $v[cat_name]> $v[cat_name]</option>";
			}
	
		}
			echo "</select></p>";

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 1:01 am
by requinix
Because you don't do anything with $_GET["cat"] to automatically (re)select the chosen option.

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 1:18 am
by jayson.ph
i trying to put this line.

Code: Select all

if(intval($_GET['cat'])!=0)
but Undefined index. please need help here..

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 7:52 pm
by requinix
Don't assume the cat exists. Use isset to check if it is, and if so then you can select the option.

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 8:14 pm
by jayson.ph
Okay, Thank but for now i use this if(isset($_GET['cat'])) to avoid Undefined index but my problem is same like last-day. please

Code: Select all

	  $query = "SELECT DISTINCT cat_name,cat_it FROM tbl_category";
                        $x = mysql_query($query);
						
						echo "<form name = 'f1' method = 'post' action ='jdjd.php'>";
                        echo "<p>Category:<select name ='cat' onchange=\"reload(this.form)\"><option value = ''>Select Category</option>";
                        while($v = mysql_fetch_array($x))
                {
                    if(isset($_GET['cat']))
					{	
                        echo "<option value = $v[cat_it]> $v[cat_name]</option>";
                    }
					else
					{
						echo "<option value = $v[cat_it]> $v[cat_name]</option>";
					}
                }
                        echo "</select></p>";
						echo "</form>";
	?>

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 9:36 pm
by requinix
Then my answer is the same as before.

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 9:58 pm
by jayson.ph
please tel me what should i do this.? or can i have your code.

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 10:33 pm
by requinix
As you're looping through the options, compare each value against $_GET["cat"] (if it's present), and if they match then mark the option as selected.

Re: Drop-down Get return from default

Posted: Tue Jul 10, 2012 11:43 pm
by jayson.ph
okay thanks this and earlier i put selected in option part. but it not work. but my problem is im not good on how to wright a correct syntax.

Thanks again. and i hope now, tomorrow, and for the next day i find a good solution for my problem.

Thanks.

Re: Drop-down Get return from default

Posted: Wed Jul 11, 2012 12:44 am
by jayson.ph
Solve and here,

Code: Select all

<?php
	@$cat=$_GET['cat']; 
		if(strlen($cat) > 0 and !is_numeric($cat))
			{
				echo "Query Error";
				exit;
			}

		$quer2 = mysql_query("SELECT DISTINCT cat_name,cat_it FROM tbl_category order by cat_name"); 
	
		if(isset($cat) and strlen($cat) > 0)
		{
			$quer = mysql_query("SELECT DISTINCT product_name,cat_it FROM tbl_product where cat_it=$cat order by product_name"); 
		}
		else
		{
			$quer=mysql_query("SELECT DISTINCT product_name,cat_it FROM tbl_product order by product_name"); 
		} 

			echo "<form method = post name = formname action='##'>";
			echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
			
			while($noticia2 = mysql_fetch_array($quer2)) 
				{ 
			if($noticia2['cat_it']==@$cat)
				{
				echo "<option selected value='$noticia2[cat_it]'>$noticia2[cat_name]</option>"."<BR>";
				}
			else{echo  "<option value='$noticia2[cat_it]'>$noticia2[cat_name]</option>";}
				}
			echo "</select>";
			echo "<select name='subcat'><option value=''>Select one</option>";
			while($noticia = mysql_fetch_array($quer)) { 
			echo  "<option value='$noticia[product_name]'>$noticia[product_name]</option>";
				}
			echo "</select>";
			echo "<input type=submit value=Submit>";
			echo "</form>";
	 	?>