The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Passing drop down selected value to same form
Discuss Passing drop down selected value to same form in the PHP Development forum on Dev Shed. Passing drop down selected value to same form PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 12th, 2012, 04:10 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 44 m 3 sec
Reputation Power: 0
|
|
|
PHP-General - Passing drop down selected value to same form
Hi,
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.
Thanks
Prashanth
|

December 12th, 2012, 04:47 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
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
Code:
selected_value := POST.name
rows := query('SELECT column FROM table WHERE value = (selected_value)')
for row in rows:
print "<option>(row.column)</option>"
(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.)
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, 07:58 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 44 m 3 sec
Reputation Power: 0
|
|
|
Hi,
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.
Cheers
Prashanth
|

December 12th, 2012, 09:07 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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, 09:14 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 44 m 3 sec
Reputation Power: 0
|
|
Thanks. I tried something like this:
<body>
<form action="" name="client-form" method="post">
<select name="client">
<option value=0>Choose Client
<?=$clientOptions?> //this fetches some values from the DB, that part is working fine
</select>
</form>
<?php
if(isset($_POST["client"]))
{
$a = $_POST["client"];
echo $a;
}
else
{
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, 09:35 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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, 09:39 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 44 m 3 sec
Reputation Power: 0
|
|
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, 10:07 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
A live validation of the form before you submit it is a completely different thing. You cannot do that with pure HTML and PHP, you need JavaScript (or Ajax, to be more specific).
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, 10:18 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 44 m 3 sec
Reputation Power: 0
|
|
|
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, 10:44 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
When you build the options list, you check each name against the submitted option. And if it matches, you add the attribute selected="selected".
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|