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
jayson.ph
Forum Contributor
Posts: 165 Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:
Post
by jayson.ph » Mon Jul 09, 2012 10:30 pm
Hi All,
i just ask why? when i select record from the drop-down-list it will return to a default. see image below:
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Jul 10, 2012 1:01 am
Because you don't do anything with $_GET["cat"] to automatically (re)select the chosen option.
jayson.ph
Forum Contributor
Posts: 165 Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:
Post
by jayson.ph » Tue Jul 10, 2012 1:18 am
i trying to put this line.
but Undefined index. please need help here..
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Jul 10, 2012 7:52 pm
Don't assume the cat exists. Use
isset to check if it is, and if so
then you can select the option.
jayson.ph
Forum Contributor
Posts: 165 Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:
Post
by jayson.ph » Tue Jul 10, 2012 8:14 pm
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>";
?>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Jul 10, 2012 9:36 pm
Then my answer is the same as before.
jayson.ph
Forum Contributor
Posts: 165 Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:
Post
by jayson.ph » Tue Jul 10, 2012 9:58 pm
please tel me what should i do this.? or can i have your code.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Jul 10, 2012 10:33 pm
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.
jayson.ph
Forum Contributor
Posts: 165 Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:
Post
by jayson.ph » Tue Jul 10, 2012 11:43 pm
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.
jayson.ph
Forum Contributor
Posts: 165 Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:
Post
by jayson.ph » Wed Jul 11, 2012 12:44 am
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>";
?>