#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    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
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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?
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    When you build the options list, you check each name against the submitted option. And if it matches, you add the attribute selected="selected".

IMN logo majestic logo threadwatch logo seochat tools logo