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

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Multiple <select name="xxx[]"> with PHP


    Good evening guys -

    I have run into a HUGE ROAD BLOCK!!! I am just minutes away from ripping my hair out! Please help :-)

    I am currently working with the following form:

    Code:
    <form>
    	<select name="state[]">
    		<option value="CA">California</option>
    		<option value="AZ">Arizona</option>
    	</select>
    	<select name="state[]">
    		<option value="IL">Illinois</option>
    		<option value="PA">Pennsylvania</option>
    	</select>
    	<select name="state[]">
    		<option value="MI">Michigan</option>
    		<option value="MO">Missouri</option>
    	</select>
    </form>
    As an example, lets pretend I am using the following PHP script to handle this piece of the form:

    PHP Code:
    <?php

    $state 
    = array();

    $state $_POST['state'];

    foreach(
    $state as $key){
        echo 
    $key.'<br />';
    }
    ?>
    However, this code doesn't seem to work. My fear is that either A; this is not possible in HTML/PHP, or B; I am doing something wrong with the <select name="state[]"> tag. Unfortunately, I have to have multiple <select> tags in order to achieve the functionality I am looking for. This means that using <select name="" multiple="multiple"> is out of the question... :-(

    Below is a short description of what I want to do (just for example purposes):

    A user is presented with a drop down menu of states. When they select a state, they are then presented with the option to add another state. If they click yes here, another <select name="state[]"> tag is drawn to the page with javascript. When they select their second state, again, they can click a button to add another state. And they can do this as many times as they want. From there, they click save and they run the PHP script I have provided above which should simply display the states they picked.

    Can someone please help me identify what (if anything) I am doing wrong here? If nothing is wrong, can someone explain why this isn't possible in HTML/PHP? I have been working on this one function since 2pm this afternoon (currently 7:30pm where I am now) and can't seem to figure it out. I am not a quitter by any means so I won't rest until I find a solution...hopefully one exists within the built in capabilities of HTML/PHP.

    Any help would be greatly appreciated here guys....thanks in advance!!!

    Chris
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    You should print out your array to make sure it is holding everything you desire to start with. If not, then we gotta figure out why that is.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    Tripple, thanks for the quick response.

    I just tried the following code:

    PHP Code:
    echo $_POST['state']; 
    And received the following error message:

    Code:
     Undefined index: state in
    Usually, I look to simply echo or print the variable I am working with to ensure I am capturing the data....in this case, I am not. Any ideas?

    Thanks again!
    Chris
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    In your javascript, as it makes each drop-down, can you make it increment the state[] instead of blank? i.e. state[0], state[1], state[2]...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    Same exact result....still telling me that state is an undefined index...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    So, in your HTML section, your items are like:

    Code:
    <select name="state[0]">
    <select name="state[1]">
    <select name="state[2]">
    <select name="state[3]">

    If they are, I'm not too sure why it is not working. Unless it recognizes the fulle name as state[#]. Does this echo?

    PHP Code:
    echo $_POST['state[0]']; 
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    My select statements are exactly as you suggest...

    PHP Code:
    <select name="state[0]">
        <
    option value="MI">Michigan</option>
    </
    select
    When attempting the following echo:

    PHP Code:
    echo $_POST['state[0]']; 
    I receive the name undefined index error, only 'state' now says 'state[0]' or whatever index I attempt to echo. Are you getting this to work for you at all?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Wow. I overlooked it from the begining. the state[] is just fine. In your form tag, you are missing your method.

    method="POST"

    By default, GET gets used, so all your values you were looking for were posted to your address bar.
    Last edited by Triple_Nothing; December 8th, 2012 at 08:38 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    Sorry but that's not it either...the form I gave you is something I drew up in about 10 seconds. The full form would take up maybe 20 pages here! lol.

    I am working with a test form which is what you are seeing. The form code there is:

    Code:
    <form id="stateForm" method="post" enctype="multipart/form-data">
    </form>
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    I haven't touched your php, but just added method="POST" to the form tag, and all worked.

    http://projects.hellzoneinc.com/devshed/formarray.html
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    I GOT IT!!!! Thanks to you pointing out the form action, i finally figured out what the big issue was.

    I am sending the form to a temporary js function I use to debug data-related php scripts. This function is a simple fetch of data though to a php/js function....no post or get methods can be passed this way (duhhhh) so none of my post data is going through. I have fixed this issue by utilizing the actual function to post data....I feel like a moron!!!! thanks again! :-)

    Chris

IMN logo majestic logo threadwatch logo seochat tools logo