Drop-down Get return from default[SOLVE]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Drop-down Get return from default[SOLVE]

Post 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>";
Last edited by jayson.ph on Wed Jul 11, 2012 12:45 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Drop-down Get return from default

Post by requinix »

Because you don't do anything with $_GET["cat"] to automatically (re)select the chosen option.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: Drop-down Get return from default

Post by jayson.ph »

i trying to put this line.

Code: Select all

if(intval($_GET['cat'])!=0)
but Undefined index. please need help here..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Drop-down Get return from default

Post by requinix »

Don't assume the cat exists. Use isset to check if it is, and if so then you can select the option.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: Drop-down Get return from default

Post 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>";
	?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Drop-down Get return from default

Post by requinix »

Then my answer is the same as before.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: Drop-down Get return from default

Post by jayson.ph »

please tel me what should i do this.? or can i have your code.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Drop-down Get return from default

Post 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.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: Drop-down Get return from default

Post 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.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: Drop-down Get return from default

Post 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>";
	 	?>
Post Reply