1

I've been looking around on SO and I don't -think- this is something that's up here, either that or it's something I've missed. If so, please point me there and I'll simply mark this question as answered.

I have a completely working PHP form where my users input information into input boxes and there's a button at the bottom that submits the information to the MySQL database, so that part works perfectly. What I'm trying to do now is this: have 2 drop down menu's, each one with a static list of choices and I'd like those choices to also get sent to the database.

For instance, currently in my form I have First Name (frame) and Last Name (lname) that the user can input and then if I query the database it spits out First Name and Last Name perfectly. So now I'd like to add to my form a drop down box where the user can pick, for example, Boy or Girl, and then after doing that click the submit button that's already there (I don't want the drop-down to submit the data, and I don't want the drop-down to be populated from the database.)

I'm guessing I need to use Javascript for this part? But I really don't know.

Any advice would be appreciated.

    <html>
    <head>
    <title>MyForm</title>
    </head>
    <body>
    <form id="form" name="form" action="" method="post">
        <Label>First Name/Organization</label>
        <input type="text" name="firstname" value="<?php echo $firstname; ?>"/>
        <input type="submit" name="submit" value="Add entry">
    </form>
    </body>
</html>
4
  • Do you want them to do that AFTER they submit first name and last name, or as part of the same form WITH first name and last name? Commented Feb 5, 2012 at 0:42
  • Ah yes, my fault. I'd like them to be submitted as part of the same form if that's possible. Commented Feb 5, 2012 at 0:42
  • It would be more helpful if you added your code and/or described what you're doing that isn't working. It sounds like you're just asking how to get form data from a drop-down, and the answer is "the same way you get it from an input field", so...I'm confused. Commented Feb 5, 2012 at 0:46
  • Ok I think I was making this way harder then it had to be. For some reason I thought the drop down would be processed differently or outside of the regular form, or that it had to be a different form entirely..I guess I was just over thinking it. Commented Feb 5, 2012 at 0:57

2 Answers 2

1

So, as part of the same form, you simply treat it just like the input boxes. Here's an example:

If the dropdown is named:

<select name="gender">
    <option value="boy">Boy</option>
    <option value="girl">Girl</option>
</select>

Then in your php, you would simply get the value for gender:

$gender = $_POST["gender"];

And add to your SQL statement where you are saving the first/last name the additional field and value for gender.

Of course, you would have to first modify the table to have the column for gender....

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

1

It sound s like you just want a simple html drop down as part if your form. Just use

   <select name = 'gender'>
        <option value = 'boy'> Boy </option>
        < option value = 'girl'> Girl </option>
    </select>

Then access $_POST['gender'] in the receiving script to put it into the database (after validation of course)

1 Comment

I selected cale_b as the answerer because he was helping first, but thanks for your input as well, I up voted you, also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.