Using PHP drop down to filter MySQL table data?

I just sent you pm and you sent this reply :slight_smile: Thank you!

Yes, I’m wiping the value every time because EACH click is running a query - I don’t need all 4 together like in your last example!
when Click TestA1 gives a result, TestA2 gives another etc… every option is a filter.
This is actually a very simple drop menu value select -> run query thing
Yes you’re right i don’t need that $value part at all :smiley:
Yes i have database, but it’s not a part of this menu - i call on same values only, because those never change - they’re column’s names.

  • as i said - for my needs it works - the all part is the problem… is there a way i can make all to link to select * mytable - because i get that only when page loads for the first time…

and do you have any ideas how can i simply solve this about the 20 per page limits?

hi there, i am trying to do something similar. I want to filter database using multiple filters e.g. last name, first name and display results. i tried to change code but couldn’t work. Pls. advise what changes i make in the above code to filter by 2 or more drop down filters. Thanks for your help. Sorry, i am a newbie and still learning.

Hello,

I am trying to develop a code with 2 pull-down-menus (PDM). I want the user to select x data from the first pull-down menu, and depending on what is chosen, the choices will be reduced on the second pull down menu. All data is take from the database.
The table is composed of: item_id, brand, model, country_network, price, information. The first PDM is ‘network’ and the second is phone ‘brand’.

Here is the code as of right now. The first PDM works great, the second PDM DOESN’T WORK.

<?php

include_once “mysql_connect.php”;

/First pull down menu/
/query the table/
$phones = mysql_query(“SELECT DISTINCT country_network FROM phones”) or die(mysql_error());

/loops the network from phones table/
while($country_network = mysql_fetch_array($phones))
{
$item_id = $item[‘item_id’];
$network_name = $country_network[‘country_network’];

/assign a network inside the option tag/
$network_block .= ‘<OPTION value="’.$item_id.‘">’.$network_name.‘</OPTION>’;

}/loop ends/

/Second pull dounw menu/
/query the table/
$phones2 = mysql_query(“SELECT DISTINCT model FROM phones WHERE country_network LIKE ‘$network_block’”) or die(mysql_error());

/loops the network from phones table/
while($model = mysql_fetch_array($phones2))
{
$item_id2 = $item[‘item_id’];
$model_name2 = $model[‘model’];

/assign a network inside the option tag/
$network_block2 .= ‘<OPTION value="’.$item_id2.‘">’.$model_name2.‘</OPTION>’;

}/loop ends/

?>

<html>
<body>

<label for=“country_network”>Network/Country:</label>
<select name=“netwID”><?php echo $network_block; ?></select>

<label for=“model”>Model:</label>
<select name=“modelID”><?php echo $network_block2; ?></select>

</body>
</html>

Any advise will be greatly appreciated.
Thank you!

One term that you can search on to find advice on this is “chained drop down php”, that should get you some tutorials.

Essentially though there are 2 main ways of achieving this:

  1. pure JS - where you load up into JS arrays all the options. This is how many of the airline sites manage it. Pick a “From airport” and the “To airport” is populated dynamically, and almost instantaneously - its fast ('ray!), relies on JS (boo!) and does not require trips back to the server (('ray!). Great if you do not have too many options.

  2. html/PHP where on detecting a change in the state of drop list one, you wait (boo!) while the form is submitted (possibly using js, boo!) and then the page refreshes (boo!) showing the next chained options. Probably best if you have many thousands of options.

There is a 3. – which is to do 2 above - but use Ajax to bring back the correct data so the page does not refresh entirely. This is faster ('ray!) but is more complicated (boo!) and relies on JS (boo!) and that there is no latency on the line.

HTH

Hi

I was basically trying out the things that were mentioned in this post. I have a dropdown box in a table column. But I am not able to get the value that is selected in this dropdown box.

<form method=“post” action=“<?php echo $_SERVER[‘$PHP_SELF’];?>”>
<td><select name=“env”>
<option value=“all”>All</option>
<option value=“Windows”>Windows</option>
<option value=“Win 2008”>Win 2008</option>
<option value=“Win 7”>Win 7</option>
<option value=“Win Vista”>Win Vista</option>
<option value =“Linux”>Linux</option>
<option value = “Win 7/Linux”>Win 7/Linux</option>
</select></td>
</form>
</tr>

<?php

$environ=$_POST[“env”];
echo $environ;

?>

Here the $environ is not getting echoed. So can anyone tell me what is the mistake I made here?

Thanks

… and we’re back! Hello SPers …

You have no submit button.


<form etc ...>

// your form elements

<input type=submit />
</form>

Some personal preferences of mine:


$environ=$_POST["env"];

I always use single quotes on stringed array keys, you used single and double in your example code, that will end up in confusion. Pick one, stick to it.


$environ=$_POST['env'];

and…


<form method="post" action="<?php echo $_SERVER['$PHP_SELF'];?>">

I always use CAPS for form method declarations – jumps off the page at me, and if the form submits to itself, I leave the action blank.


<form method="POST" action="">

Hi thanks for your reply. I was actually trying to do this without a submit button, in such a way that selecting the filter option from the dropdown box will automatically refresh the page with the queries I want.

I found out from forums that the parameter: “onchange=“document.getElementById(“env”).submit();”>”, added with the <select> will do the trick.

But when I select an option from the drop down box, the page stays still. This is how the form and select statements look like, and this dropdown is in a table cell:

<td>
<form method=“post” id=“env” action=“<?php echo $_SERVER[PHP_SELF];?>” >
<select name=“name” onchange=“document.getElementById(“env”).submit();”>
<option value=“all”>All</option>
<option value=“Windows”>Windows</option>
<option value=“Win 2008”>Win 2008</option>
<option value=“Win 7”>Win 7</option>
<option value=“Win Vista”>Win Vista</option>
<option value =“Linux”>Linux</option>
<option value = “Win 7/Linux”>Win 7/Linux</option>
</select>
</form>
</td>

And for trial purposes, I want to see if the option that I selected is being echoed as:

<?php
if(isset($_POST[“name”]))
{
echo $_POST[“name”];
}
?>

But the page remains still once I select an option. Can you let me know what I am doing wrong here?

Also this page is an authenticated page, like it appears after user logs in with their username/pwd. Just letting you know this, so that you can have a better picture of this page. Please let me know if you have any questions.

Actually I think I made mistake in calling the javascript function. So I declared the function as:

function formSubmit()
{
document.getElementById(“report_filter”).submit();
}

and called this in the “onchange” event as:

<form method=“post” id=“report_filter” action=“<?php echo $_SERVER[PHP_SELF]?>” >
<select name=“try” onchange=“formSubmit();”>
<option value=“all”>All</option>
<option value=“Windows”>Windows</option>
<option value=“Win 2008”>Win 2008</option>
<option value=“Win 7”>Win 7</option>
<option value=“Win Vista”>Win Vista</option>
<option value =“Linux”>Linux</option>
<option value = “Win 7/Linux”>Win 7/Linux</option>
</select>
</form>

The page reloads when I select an option, but a blank white page comes up after this. Please let me know if you have any idea about this.

So you are convinced your JS is not at fault, so put this at the very top of the page then.


<?php
var_dump($_POST);
?>

Actually I am working on this page along with other team mates and this is an additional feature I am trying to implement. This page will appear after user logs in and selects a type of form he wants to view.

So I think because of that when I gave the “var_dump($_POST);” I got:

“array(2) { [“env_select”]=> string(8) “centrify” [“view”]=> string(4) “View” }”

So I tried “var_dump($_POST[“env”]);” and I saw “NULL” was returned. So there is a problem with the form returning the value here. Please let me know your thoughts

You’ve got several things wrong here I think.

id attributes are only used on the DOM, name attribute is passed via html to PHP.

form names are not passed to PHP.


<?php
var_dump ($_POST);
?>
<form id=myform name=myform method=POST action="">
<input type=text id=mytext name=mytext value =test>
<input type=submit name=mysubmit>
</form>

In that example mytext is passed along, but myform is not.

If using JS to bundle up the form elements you will have to shovel the id into a name attribute and pass it on, or pass it on some other way.

Or use a hidden form element read this.

I am not sure what you said there. But I will tell you the thing I tried recently.

I used the code which I have been trying to use in the page I am working on, in a separate PHP page and I ran it. It is the same code I have been trying so far and it is as follows:

<script>
function formSubmit()
{
document.getElementById(“report_filter”).submit();
}
</script>

<form method=“POST” id=“report_filter” action=“” >
<select name=“try” onchange=“formSubmit();”>
<option value=“all”>All</option>
<option value=“Windows”>Windows</option>
<option value=“Win 2008”>Win 2008</option>
<option value=“Win 7”>Win 7</option>
<option value=“Win Vista”>Win Vista</option>
<option value =“Linux”>Linux</option>
<option value = “Win 7/Linux”>Win 7/Linux</option>
</select>
</form>

<?php

if(!(isset($_POST[“try”])))
{
echo “none”;
}
else
echo $_POST[“try”];

?>

This created a dropdown box with all the elements listed above, and when I select one of the options I see that it is being echoed. Like, initially “none” is echoed, and then the option being selected in the dropdown box such as “Windows”, “Windows 2007” or whatever is being echoed.

Here I am not posting the form name, but the “<select>” menu’s name “try” only as you said, and I see that the option being selected is echoed as expected.

The same code is not working in the php page that I am currently working on. Please let me know if you need any more information

The same code is not working in the php page that I am currently working on. Please let me know if you need any more information

So how is the PHP page different then?

I am not quite sure about that. I am still new to PHP programming and so probably I am missing to see some point here. If it gives out some form of error then I can debug, but seeing just a white screen is not helping me at all.

This PHP page I am working on, will appear after the user logs in and selects a company name from a dropdown menu. This is being worked on by other batchmates so I am not entirely familiar with the code there. But still I think this dropdown code must work since its working independently.

It will be really helpful if you can help me debug this. Please let me know if you need any more information about this

My immediate guess is that either a header(“Location: xyz.php”) is causing a magic relocate somewhere in the code (to a blank page), or the page that is being served up does not have error reporting turned on – or maybe even both of the above.

Just one more info for you. I asked the other batchmates here and they say there are some Session variables being used in this PHP page, and the session is started every time the page is loaded. I also see that there is some code like “session_start();” and some variables are also passed thru the session like “$_SESSION[‘username’]” etc. Please let me know if there is something related to this session to the filter I am trying to build.

When I commented out the code containing the “session_start();” and any other code getting value from session, I see that when I select the dropdown option, the option is echoed as I have been trying. SO I think this has got something to do with the “session”. Maybe I need to look more into this, but if you know something about this that might help me, then please let me know.

When I goto this page straightaway without logging in, and by commenting out the session commands like “session_start()” and all, the page can echo the required string, based upon the selection in the dropdown box.

So can you tell me how to stop this issue, or if you think there is something wrong with the way I found out the cause of the issue, then please notify that as well. Please let me know if you need more details.