0

i need to store multiple elements of listbox item into the database , but i am unable to pass the elements from the html form to php. Please help

<html>
<head>
</head>
<body>
<form method="post" action="Save.php">
    <select name="country[]" multiple="multiple">
        <option value="Belgium">Belgium</option>
        <option value="France">France</option>
        <option value="Germany">Germany</option>
        <option value="Holland">Holland</option>
        <option value="Greece">Greece</option>
    </select>
    <input type="Submit" value="Submit" />
</form>
</body>
</html>

    <?php

    if(isset($_POST['submit']))
    {
    $con = $_POST['country'];
    foreach($con as $selected)
    {
    echo 'selected'.$selected;

    }
    ?>
Vivek
  • 363
  • 8
  • 25

3 Answers3

1

This code shall solve your issue

    <?php

if(isset($_POST['submit']))
{

    if (isset($_POST['country'])){
        $con = $_POST['country'];
        foreach($con as $selected)
            echo 'selected'.$selected;
    }
}
?>

<html>
<head>
</head>
<body>
<form method="post" action="Save.php">
    <select name="country[]" multiple="multiple">
        <option value="Belgium">Belgium</option>
        <option value="France">France</option>
        <option value="Germany">Germany</option>
        <option value="Holland">Holland</option>
        <option value="Greece">Greece</option>
    </select>
    <input type="Submit" value="Submit" name="submit" />
</form>
</body>
</html>
Chris Lim
  • 424
  • 4
  • 9
  • its not showing any error but still i am not able to print the elements – Vivek Feb 27 '14 at 03:38
  • You need to select the value inside the list, so that it will display you the result. No selection will have the empty POST['country'] value. – Chris Lim Feb 27 '14 at 06:03
0
//the post request takes the Tag NAMES only
<input type="Submit" value="Submit" name="submit"/>

see this phpfiddle it is working fine only

ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
0

You don't have a POST index of submit hence your isset() is failing. You are also missing a closing brace for your if statement in your code example. I've modified your example to use the $_POST['country'] for the isset(). This bit worked for me as far as returning selected values goes:

<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="country[]" multiple="multiple">
        <option value="Belgium">Belgium</option>
        <option value="France">France</option>
        <option value="Germany">Germany</option>
        <option value="Holland">Holland</option>
        <option value="Greece">Greece</option>
    </select>
    <input type="Submit" value="Submit" />
</form>
</body>
</html>

<?php

    if(isset($_POST['country']))
    {
        $con = $_POST['country'];
        foreach($con as $selected)
        {
            echo 'selected'.$selected;
        }
    }
?>
Crackertastic
  • 4,958
  • 2
  • 30
  • 37
  • its not showing any error anymore but still i am unable to echo the listbox elements. – Vivek Feb 27 '14 at 03:25
  • Are you passing the form submission back into the same page where this test code is running? When I run the code, plus a call to `print_r($_POST['country']` I get the output of `selectedFranceselectedGermanyArray ( [0] => France [1] => Germany )` in my browser window. – Crackertastic Feb 27 '14 at 03:27
  • yes, html and php code are in the same page. – Vivek Feb 27 '14 at 03:35
  • Hmmm...odd. Like I said, I get code output from what I have above, so what you see there is doing the job of getting at the countries I select in the list. – Crackertastic Feb 27 '14 at 03:38