user3046061
user3046061

Reputation: 353

How to get all checked options out of a select form via php through POST?

This is driving me nuts. I know there are other questions that addressed this and I think I've pretty much tried everything I've read in them. Either I'm misunderstanding something very basic or need another pair of eyes on this.

So there's a session already started when this code is being met with the log in credentials and whatnot. I'm trying to get the checked options out of a select form and use them.

if(isset($_POST['select']))
{    
    if (is_array($_POST['select'])) {
        foreach ($_POST['select'] as $x)
            echo "<p>$x</p>";
        }
    else {
        echo "<p> There is no selection </p>";
    }
}

 echo <<<_END
 <form method='post' action='profile.php' enctype='multipart/form-data'>
 <h3>Enter or edit your details </h3>
 <textarea name='text' cols='50' rows='3'>$text</textarea><br />
 <br />
 <span class='fieldname'>YY:</span>
 <select name="select[]"  multiple="multiple" >
 <option value="Opt1"> Choice1</option>
 <option value="Opt2"> Choice2</option>
 <option value="Opt3"> Choice3</option>
 <option value="Opt4"> Choice4</option>
 </select>
 </span><br /><br />

_END;
?>

<input type='submit' value='Save Profile' />
</form></div><br /></body></html>

I've only pasted the relevant bits.

When I do my selection and click Save I'm only getting the else branch.

EDIT: It seems that trying with the default encoding on the form did not resolve this issue.

Upvotes: 0

Views: 91

Answers (2)

ATechGuy
ATechGuy

Reputation: 1270

you can use a foreach

if(isset($_POST['select']))
{    
foreach ($_POST['select'] as $x)
        echo "<p>$x</p>";
    }
else {
    echo "<p> There is no selection </p>";
}
}



 <form method='post' action='profile.php' enctype='multipart/form-data'>
 <h3>Enter or edit your details </h3>
 <textarea name='text' cols='50' rows='3'>$text</textarea><br />
 <br />
 <span class='fieldname'>YY:</span>
 <select name="select"  multiple="multiple" > // remove [] from here //
 <option value="Opt1[]"> Choice1</option>     //change your options to have [] //
 <option value="Opt2[]"> Choice2</option>
 <option value="Opt3[]"> Choice3</option>
 <option value="Opt4[]"> Choice4</option>
 </select>
 </span><br /><br />

bassicaly you need to add [] to your options in your select group and take away [] from your select name. And use the code above

Upvotes: 0

Ryan Vincent
Ryan Vincent

Reputation: 4513

This code should make it clear what is happening.

If none of the select options is clicked then there is no entry created in the $_POST array.

the name of 'select[]' tells PHP to convert the input into an array if there is any input selected for this widget.

<?php

if(isset($_POST['select']))
{
    if (is_array($_POST['select'])) {
        foreach ($_POST['select'] as $x)
            echo "<p>$x</p>";
        }
    else { // this cannot happen as nothing comes in when not selected
        echo "<p> There is no selection -- cannot happen</p>";
    }
}
else {
    echo "<p> There is no selection </p>";
}

if (isset($_POST['select'])) {
  var_dump($_POST['select'], 'select'); // you can see what comes in!
}
else {
  echo '<strong>no selected options...</strong>';
}


/*
 * do appropriate 'select' action(s)...
 */
if (isset($_POST['select'])) {

  if (in_array('Opt1', $_POST['select'])) {
     echo '<br/>do Opt1 action...<br/>';
  }

  if (in_array('Opt2', $_POST['select'])) {
     echo '<br/>do Opt2 action...<br/>';
  }

   if (in_array('Opt3', $_POST['select'])) {
     echo '<br/>do Opt3 action...<br/>';
  }

    if (in_array('Opt4', $_POST['select'])) {
     echo '<br/>do Opt4 action...<br/>';
    }
    echo '<br/>';
 }


?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Show Select</title>
  </head>

  <body>
    <div class="main" id="main">

        <!-- heading -->
        <strong><?php echo 'Select setting...'?></strong><br/>

          <form method='post' action='' enctype='multipart/form-data'>
           <h3>Enter or edit your details </h3>
           <textarea name='text' cols='50' rows='3'>$text</textarea><br />
           <br />
           <span class='fieldname'>YY:</span>
           <select name="select[]"  multiple="multiple" >
           <option value="Opt1"> Choice1</option>
           <option value="Opt2"> Choice2</option>
           <option value="Opt3"> Choice3</option>
           <option value="Opt4"> Choice4</option>
           </select>
           </span><br /><br />

          <input type='submit' value='Save Profile' />
          </form>
    </div><br />
  </body>
</html>

Upvotes: 1

Related Questions