December 12th, 2012, 05:10 AM
Passing drop down selected value to same form
I am fairly new to PHP (having worked only on DB2 & MS SQL for so long) and I require the following functionality for one my projects.
1. Choose a value from a given drop down box
2. Take this chosen value, query the database to obtain certain rows
3. Display these rows (from the DB) on another drop down box.
I have achieved step 1 of this. But not sure how to proceed further.
Any help (if possible, so pseudo code) would be really helpful.
December 12th, 2012, 05:47 AM
not sure how we can help you with that. What you need to know is how to make queries and how PHP generally works. The code itself will just be something like
(Please don't take this literally! You cannot just dump the values into the query string or the HTML markup, because this will lead to massive security holes.)
selected_value := POST.name
rows := query('SELECT column FROM table WHERE value = (selected_value)')
for row in rows:
For the query, use prepared statements in the PDO interface or the MySQLi library. Don't use the ancient "mysql_" functions that you still see everywhere. They're long obsolete and shouldn't be used at all in new code.
December 12th, 2012, 08:58 PM
I understand what you are saying. But .POST works if you have to send from one file to another right? But here, I want it within the same file.
December 12th, 2012, 10:07 PM
It doesn't matter if it's the same file. A POST request simply sends data to a URL. The origin of the data is completely irrelevant. It doesn't even have to come from a web form, you might as well write the HTTP request "by hand".
I mean, just try it out: Make a form script and set the action attribute to the script itself.
This is actually a very common practice, because it allows you to easily display error messages in the original form.
December 12th, 2012, 10:14 PM
Thanks. I tried something like this:
<form action="" name="client-form" method="post">
<option value=0>Choose Client
<?=$clientOptions?> //this fetches some values from the DB, that part is working fine
$a = $_POST["client"];
echo "not selected";
But what happens is, I can see my dropdown box with the values and below that, the text "not selected". Even if I choose a value from the drop down box, that is not getting displayed. I am sure I'm missing something, but cannot figure out why.
Too bad I got into PHP pretty late
December 12th, 2012, 10:35 PM
I think you misunderstand how HTML works. The form doesn't do anything until you submit it. You need a submit button to actually send the data to the server. Before that it's just you clicking in your browser window.
That's also why the "not selected" message makes no sense. As long as the user hasn't submitted his form, you don't display anything (except maybe "please fill out the form"). Only when the data has actually been sent to the server, you do the form processing.
You might wanna look into HTML and HTTP basics before you start with PHP. This will make it much easier to understand how and why everything works.
December 12th, 2012, 10:39 PM
Well I think I am aware of the basics of HTML & HTTP yes !! And I do understand that it has to be "submitted" before any control occurs.
When you enter a password and re-enter it below, a validation occurs if the passwords do not match right? There again, you don't really submit the form. I am trying to achieve something like that.
The code above was just an example
December 12th, 2012, 11:07 PM
Since this is a relatively advanced technique and requires a very different style of processing data, I don't think it's a good idea to start with that.
Why don't you start with a simple HTML form and move from there? If you want us to help you with that, post your actual, complete code.
December 12th, 2012, 11:18 PM
Thanks. I suppose I could do that. Meanwhile, if you can just answer this one specific query alone?
Suppose I submit a form to itself:
<form action="same form" method="post">
Within this form I have a drop down box. The user chooses a particular value and clicks on a submit button. So the same form will be loaded. Is there any way for me to retain the value chosen by the user on the drop down itself?
December 12th, 2012, 11:44 PM
When you build the options list, you check each name against the submitted option. And if it matches, you add the attribute selected="selected".