Forum Moderators: open

Message Too Old, No Replies

dropdown list problem?

         

goud

10:26 am on Dec 22, 2009 (gmt 0)

10+ Year Member



Hi to all.
I am new for web design.
I am getting categories in drop down menu well but when select cats i cannot getting data from the drop down list.

this is my sql tables.


CREATE TABLE `mandal_page` (
`id` int(4) unsigned NOT NULL auto_increment,
`name` varchar(100) default NULL,
`time` varchar(60) default NULL,
`offphone` int(10) default NULL,
`category` varchar(50) NOT NULL default 'Select',
`offname` varchar(150) default NULL,
`count` varchar(4) NOT NULL default '1',
`public` char(3) NOT NULL default 'no',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
CREATE TABLE `mandal_category` (
`id` int(4) unsigned NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

how can i get data from the chosen list.

sorry for my english. Help me any one to me.
THANK YOU IN ADVANCE

rocknbil

8:07 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard goud, not sure what you're doing here, or what language you are using, but this might be better in the PHP forum (if you're doing it in php.)

First thing I would do is change this

`category` varchar(50) NOT NULL default 'Select',

to this

`category` int(6) NOT NULL default 0,

Reason being, if you store category as textual values, and decide to change the title of one of your category names, you'd have to change all instances in the other table as well. By just storing the category ID, this becomes automatic. Second, use numeric data types wherever you can, it makes things run much faster.

With that in mind, basically you start like this:

$select = "select * from mandal_category order by name asc";

Which gives you an array of two items.

$selectList = '
<select name="category" id="category">
<option value="">select</option>';
$result = mysql_query($select);
while ($row=mysql_fetchrow_array($result)) {

$selectList .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';

}

$selectList .= '</select>';

You now use $selectList in your form. On submit, you should now have the category id, not the name, in post/get/request.

echo $_POST['category']; // a number, the id, like 5 or 7

So from there,

// a little error check, make sure it's a number
if (! ($_POST['category'] > 0)) {
die("Oops, category is invalid");
exit;
}

$query = "select * from mandal_page where category=" . $_POST['category'] . " order by name asc";

Should give you all the items in the selected category. Note I DID NOT quote ' ' the value in the query, which doesn't hurt anything. Quoting a numeric query is not necessary. If $_POST[category'] is null, it will error, which is good - tells you something is wrong in your programming.

goud

4:36 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



Hi rocknbil.
Thank you for reply for my post. I am getting data in drop down list well. but when I select category I can not getting data from that category. I am posting here my practicing site link here:

<snip>

once again thank you rocknbil.

[edited by: bill at 11:12 am (utc) on Dec. 27, 2009]
[edit reason] No links to your example sites please [/edit]

trigoon

4:48 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



Hi Goud,

Its hard to say whats wrong without being able to see the PHP code your using to handle the submit.

goud

5:43 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



Ok I am posting my php code Here

<?php

mysql_connect('localhost','user','pass');

mysql_select_db('db');

?>

<? if($error!=""){ ?>

<br>
<table width="75%" border="1" cellpadding="5" cellspacing="0" bordercolor="#000099" bgcolor="#E1FFFF">
<tr>
<td align="center" class="text"><strong><?=$error?></strong></td>
</tr>
</table>
<br>
<? } ?>

<FORM method="GET" ENCTYPE="multipart/form-data" name="uploadform" id="uploadform">

<br> <h1 align="left">View mandals</h1>
<hr color="#666666"> <br>

<table width="95%" height="40" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000099" bgcolor="#FFFFFF">

<tr bgcolor="#17A8FF">
<td bgcolor="#E1FFFF" class="text1">&nbsp;&nbsp;&nbsp;&nbsp;<b>mandal Categories</b>

<select name='category' id='category' onChange="submit()">
<option value="">Select A Mandal Here</option>
<?


$result=mysql_query("select * from mandal_category");
while($myrow = mysql_fetch_array($result)){

print "<option value='".$myrow['id']."'>".$myrow['name']."</option>";
}
?>
</select> </td>
</tr>
</table>
</FORM>
<?php

if($id==""){

$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";

}
else{
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";

}

$results=mysql_query($query1) or die(mysql_error());

echo "<table align='center' bgcolor='#hhhhfff' class='reference' width='60%' border='1' cellpadding='0' cellspacing='0' >
<tr bgcolor='#ffffff'>
<th>id</th>
<th>Office Name</th>
<th>offphone No.</th>
<th>Updated On</th>
<th>Updated By</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['id'] . "</td>";
echo "<td align='center'>" . $row['offname'] . "</td>";
echo "<td align='center'>" . $row['offphone'] . "</td>";
echo "<td align='center'>" . $row['time'] . "</td>\n";
echo "<td align='center' >" . $row['name'] . "</td>\n";
echo "</tr>";
}


echo "</table>";

?>

If you suggest any easy method I follow your code.

rocknbil

9:55 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First,
if($id==""){

$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";

}
else{
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";

}

The if and else are identical!

Second, mentioned above:

$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";

I told you, if you quote q numeric field, and there's no value, it will do this:

$query1 ="SELECT * FROM mandal_page WHERE id = '' && public = 'yes'";

And there will be no field with value '', so no results. Had you done this,

$query1 ="SELECT * FROM mandal_page WHERE id = $category && public = 'yes'";

You would get a mysql error. Why? $category has not been set anywhere.

I also don't see where $id is set, but since the if/else does the same thing, let's forge on.

Try this. Note also you had results as the resources but result as the fetch_array.


if ($isset($_POST['category']) and ($_POST['category'] > 0)) {
$category=$_POST['category'];
$query1 ="SELECT * FROM mandal_page WHERE id = $category and public = 'yes'";
$result=mysql_query($query1) or die(mysql_error());
echo '<table align="center" bgcolor="#hhhhfff" class="reference"
width="60%" border="1" cellpadding="0" cellspacing="0">
<tr bgcolor="#ffffff">
<th>id</th>
<th>Office Name</th>
<th>offphone No.</th>
<th>Updated On</th>
<th>Updated By</th>
</tr>
';
while($row = mysql_fetch_array($result)) {
echo '
<tr>
<td align="center">' . $row['id'] . '</td>
<td align="center">' . $row['offname'] . '</td>
<td align="center">' . $row['offphone'] . '</td>
<td align="center">' . $row['time'] . '</td>
<td align="center">' . $row['name'] . '</td>
</tr>
';
}
echo "</table>";
} // end if
else { echo '<p>Oops no category selected.</p>'; }

goud

7:22 pm on Dec 25, 2009 (gmt 0)

10+ Year Member



Hi rocknbil.

I tried that code not working at.

where can i use java text code. In this script.

if u have a time recode my script. sorry for troubling.

I have searched for how can retrieve data from drop down list. I didn't find any solution for my script its easy but i can not understanding calling query correctly.

rocknbil

6:06 pm on Dec 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, my apologies - I have an error there. This

if ($isset($_POST['category'])

should be this

if (isset($_POST['category'])

"isset" is not a variable.

After that, what errors are you getting? It may need debugging, untested . . .

What would you like to do with Javascript?

goud

6:11 pm on Dec 27, 2009 (gmt 0)

10+ Year Member



I am getting

Oops no category selected.

actually the list showing categories.

rocknbil

11:43 pm on Dec 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, because,

if (isset($_POST['category']) and ($_POST['category'] > 0)) {
......

Are you using GET method in your form? Malformed html in the form not posting "category?"

Are your category select list values still text values? See post #2, if you did not change the categories to a numeric value, text will always evaluate to 0. So if this is the case, change it to

if (isset($_POST['category']) and ($_POST['category'] != '')) {

goud

11:05 am on Dec 31, 2009 (gmt 0)

10+ Year Member



rocknbil
oops no category selected solved but not display any data

rocknbil

6:00 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. Verify that you are indeed using $result in both places, not $result1, etc.

$result=mysql_query($query1) or die(mysql_error());

... and ...

while($row = mysql_fetch_array($result)) {

2. Echo the select statement.

$query1 ="SELECT * FROM mandal_page WHERE id = $category and public = 'yes'";
echo "$query1\n";

(or, if it's still a text category, "... where id='$category' ...)

3. Copy it, go into whatever you use for a direct query of your database, either command line, phpMyAdmin->SQL link, whatever. Paste the select statement in. Do you get results?

goud

3:15 pm on Jan 1, 2010 (gmt 0)

10+ Year Member



Ok. I am used $query1 and text category correctly.

But method="GET" where that get value "$name" I am used
that mean
$name = addslashes(strip_tags($_GET['name'])); where can i used this correctly.
I think this is main problem.
for example :
$query1 ="SELECT * FROM mandal_page WHERE category = '".$name."' and public = 'yes'";

that $name didn't getting from drop down list
rock are you under standing my English. sorry for my poor english.

goud

6:27 am on Jan 9, 2010 (gmt 0)

10+ Year Member



ROCKNBILL thank you for cooperation for here.

My problem solved. your guidance used for me.

once again thank u very much.