Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP get value of selected item in drop down box

Status
Not open for further replies.

perdog06

Technical User
Jan 21, 2005
3
US
Hello,

I suspect this question is either really simple, or I'm asking something that PHP won't do.

I have a drop down box, I select an item "computers" and I want to be able to access that value using php. What I'm really trying to do is base a POST action command on the fact that "computers" is selected.

I'm trying to make the action="nextpage.php?f=X" where X is a number that corresponds to "computers". So the action would be nextpage.php?f=1 for computers, nextpage.php?f=2 for cars. and so on.

I just can't for the life of me get the value of that drop down box. If I get "computers" I can obviously do an if and asign it 1, that's no problem, but how to get "computers" from the drop down. I'm much more familiar with c and java than html and php, so I might be barking up the wrong tree.

Thank you very much for any help. Any suggestion might help me to an answer!
 
$var = $_POST['dropdownName'];

Bastien

Cat, the other other white meat
 
Hey, good question.
There is a way to do this, so it's not out of the scope of PHP.
It is not however, possible to do it that way.
This solution is a little sloppy, but it will do the job, otherwise, as suggested by Bastien, you would have POST navagation for your website, which is not a good idea, you want to post navagation variables in the URL using GET.

Okay, the first page 'index.php' make a redux form (a form that submits to itself). So the action="index.php" and the method="POST" and the values for the dropdown menu should be the number you want to use for 'f'.

Next step, (I can't test this code right now, but) put this code in the <head> tag of the page 'index.php'

<? if ($dropdown) { echo '<meta http-equiv="REFRESH" content="0;url=nextpage.php?f=' . $_POST[dropdown] . '">'; } ?>

This will redirect immediately, and will enable GET navagation.
Hope it helps, let me know.

 
OK well thanks,

I think I understand how this will work now, and I'm happy about that. Using your code however, I get an unidentified variable ($dropdown) on my webpage. I changed this to "child" the name of my dropdown, and get that error.

So is there something missing to identify the variable before that point in the code?

Thanks a lot for both your help though, like I said, I think at least I see how to do this now....
 
You could even do:

Code:
if(!isset($_POST['child'])) {
  require("_form.php");
}
else {
  require("_result.php");
}

eg:
at first, user browses the index.php. The variable child does not excist. Therefore, it glues in the form there.
When the user has submitted the form, with a child value, the form will not be show again. Now it shows the result instead.

You still have the child variable, as it's still in the index you are. (partial content).

if you wish to make some "new search" link, you simply make:
<a href="index.php" title="Make a new search">Make a new search</a>, as that will clear the variables.

Olav Alexander Mjelde
Admin & Webmaster
 
Alright well thank you all for your help. I basically used a collection of what everyone wrote and ended upwith a different solution. I put a page in between my index and my page I wanted to end up at. This seems to solve my problem, basically POST the data to the in between page, process it, POST it again. It's minimal data in the POST so I don't think this is too big a waste.

Thanks again, I always appreciate help in forums like these...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top