Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 12 of 12

Thread: PHP - How to INSERT values from Dropdown list?

  1. #1

    PHP - How to INSERT values from Dropdown list?

    Hi,

    I am working from a form that has a drop down list with two options in it. The selected option needs to be inserted into a mysql database along with the rest of the data from the text fields in the form.

    I have been able to properly insert all the text field values but I am stuck on the values from the drop down list and would appreciate some help.

    The drop down list is as follows:

    HTML Code:
       <select name="user_type">
          <option value="" selected="selected">-- Select --</option>
          <option value="admin">Administrator</option>
          <option value="vendor">Vendor</option>
        </select>
    For the text values I have been using the following PHP to test for values:

    PHP Code:
    if (empty($_POST['last_name'])) {
            
    $errors[] = 'You forgot to enter the last name.';
        } else {
            
    $ln escape_data($_POST['last_name']);
        } 
    How do I do the equivalent for the values in the drop down list?

    Thanks in advance.

  2. #2
    I don't see where you're getting the last_name post variable from, that's not in the form you provided.

    Essentially your post var for user_type will either be "", "admin," or "vender" depending on which is selected.

    Maybe I'm misunderstanding your question?

  3. #3
    The last_name one is an example of how am doing the text versions. I just need to figure out how to pull the data in for the drop down list.

  4. #4
    Quote Originally Posted by fanderson View Post
    The last_name one is an example of how am doing the text versions. I just need to figure out how to pull the data in for the drop down list.
    It would be exactly the same as the text box.
    PHP Code:
    if (empty($_POST['user_type'])) {
            
    $errors[] = 'You must select a user permission.';
        } else {
            
    $ln escape_data($_POST['user_type']);
        } 
    just think of a select box as a text field with predefined answers
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  5. #5

    Getting closer - but

    Quote Originally Posted by simplistik View Post
    It would be exactly the same as the text box.
    PHP Code:
    if (empty($_POST['user_type'])) {
            
    $errors[] = 'You must select a user permission.';
        } else {
            
    $ln escape_data($_POST['user_type']);
        } 
    just think of a select box as a text field with predefined answers
    Thanks for the response. I tried the above code and I get the error even after selecting a type, so I am still missing something. Any more ideas?

  6. #6
    Post your error. There aren't "any more ideas" this is essentially the only way to do it. Post some more of your code if you don't mind.

  7. #7
    Here is the code. If you need more please let me know:

    PHP Code:
    if (!empty($_POST['password1'])) {
            if (
    $_POST['password1'] != $_POST['password2']) {
                
    $errors[] = 'The password did not match the confirmed password.';
            } else {
                
    $p escape_data($_POST['password1']);
            }
        } else {
             
    $errors[] = 'You forgot to enter a password.';
        }
     
        if (empty(
    $_POST['user_type'])) { 
            
    $errors[] = 'You must select a user type.'
        } else { 
            
    $ut escape_data($_POST['user_type']); 
        } 
    Here is the form part:

    HTML Code:
       <label>User type:</label>
        <select name="usertype">
          <option value="" selected="selected">-- Select --</option>
          <option value="admin">Administrator</option>
          <option value="vendor">Vendor</option>
        </select>
    The error message is:

    Error!

    The following error(s) occurred:
    - You must select a user type.
    Please try again.

    Thanks.

  8. #8
    You named your select "usertype" and you are trying to read from "user_type" - they need to be the same.

  9. #9
    Quote Originally Posted by fanderson View Post
    Thanks for the response. I tried the above code and I get the error even after selecting a type, so I am still missing something. Any more ideas?
    Get rid of the selected="selected" that's my ONLY other guess w/o seeing anymore code.
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  10. #10
    Why would that have anything to do with it? If it's selected it'll output "" if not it'll output admin or vendor, don't see why it would affect anything.

  11. #11
    Quote Originally Posted by nobody View Post
    Why would that have anything to do with it? If it's selected it'll output "" if not it'll output admin or vendor, don't see why it would affect anything.
    It shouldn't have anything to do with it at all, but if you're eliminating options may as well start with the ridiculously simple things that shouldn't make a difference.

    But... it also doesn't help that I didn't read see his post w/ the rest of the code AND I didn't see your reply either which is why in my reply I quoted the last thing that I read
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  12. #12
    Dear Simplistik and Nobody,

    Thanks for sticking with me and providing the extra eyes. It was indeed the non-matching values that caused the trouble. When I put those to user_type in the form and the php it all came together and ran like it was supposed to.

    Much appreciated. I am sure I'll be back as my project has more steps ahead.

    Until next time...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012