SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    carrying over form field values foreach()

    Hi, I hope I explain this correctly.

    i have a form with a bunch of dropdowns created by a foreach() statement

    PHP Code:
    $factor[1] = "val1";
    $factor[2] = "val2";
    $factor[3] = "val3";
    $i 0;
    foreach(
    $f as $factor){
    $i++;
    <
    select name='answer[]'>
    <
    option value 'A'>A</option>
    <
    option value 'B'>B</option>
    <
    option value 'C'>C</option>

    I need the choices to carry over to another version of the form after the submit button is pressed but I need the dropdown menus to be filled out with the prior entries.

    I have unsuccessfully tried using:

    PHP Code:
    $_POST['answer']['$i']; 
    How can I use the foreach() loop again and have the value of those dropdowns filled in with the value from the array without having to hard code each choice?

    Thanks, I hope I explained this clearly enough.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print_r($_POST);

    The only thing available is the single value that the user selected for each form element. So you would still need a list of the different options they could have chosen from.

    I don't know where the different options come from, but assuming its a database, then you could just query the question_id to get the list of possible options. You could also store them in an array in an include file. Use the value stored in $_POST to decide which one to make selected.


    123, and 227 would be example question_id's from your database or array.
    Code:
    <select name="answer[123]">
    <option value = 'A'>A</option> 
    <option value = 'B'>B</option> 
    <option value = 'C'>C</option>
    </select>
    
    <select name="answer[227]">
    <option value = 'A'>A</option> 
    <option value = 'B'>B</option> 
    <option value = 'C'>C</option>
    </select>
    Then when receiving
    PHP Code:
    foreach ($_POST['answer'] as $question_id => $user_selected_value) {
        echo 
    '<select name="answer[' $question_id ']">';
        
    $options_for_this_select_menu get_values_from_database($question_id);
        foreach (
    $options_for_this_select_menu as $option) {
            
    $selected = ($option == $user_selected_value) ? ' selected="selected"' '';
            echo 
    "<option value=\"$option\" $selected>$option</option>\n";
        }
        echo 
    "</select>";

    Does that make sense?

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The different options are the same for each dropdown. It is asking for a percentage from 0 - 100 for different factors. I am basically doing an error check to make sure every field is filled out correctly but I don't want the previous entries lost.


Bookmarks

Posting Permissions

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