PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 12th, 2012, 04:10 AM
prashu88 prashu88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 prashu88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old December 12th, 2012, 04:47 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,836 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 53 m 10 sec
Reputation Power: 811
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.

Reply With Quote
  #3  
Old December 12th, 2012, 07:58 PM
prashu88 prashu88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 prashu88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old December 12th, 2012, 09:07 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,836 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 53 m 10 sec
Reputation Power: 811
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.

Reply With Quote
  #5  
Old December 12th, 2012, 09:14 PM
prashu88 prashu88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 prashu88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old December 12th, 2012, 09:35 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,836 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 53 m 10 sec
Reputation Power: 811
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.

Reply With Quote
  #7  
Old December 12th, 2012, 09:39 PM
prashu88 prashu88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 prashu88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old December 12th, 2012, 10:07 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,836 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 53 m 10 sec
Reputation Power: 811
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.

Reply With Quote
  #9  
Old December 12th, 2012, 10:18 PM
prashu88 prashu88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 prashu88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #10  
Old December 12th, 2012, 10:44 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,836 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 53 m 10 sec
Reputation Power: 811
When you build the options list, you check each name against the submitted option. And if it matches, you add the attribute selected="selected".

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Passing drop down selected value to same form

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap