0

I already use multiple dropdown box with data from MySQL database, but I face problem when I try to $_POST it. This is the code I use:

<?php try {
    $dbo = new PDO('mysql:host=' . $dbhost_name . ';dbname=' . $database, $username, $password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

                        $sql = "SELECT city FROM dropdown";?>
                          
                        <select name="cities" multiple="multiple">
                          
                        <?php foreach ($dbo->query($sql) as $row) {
                        echo "<option value=>$row[city]</option>";
                        }                        
                        ?>
                            
                        </select></td>

This is the code I try to call it:

$test = $_POST['cities'];
echo $test;

  • The data successfully imported from database but it's doesn't $_POST

2 Answers 2

3

Make sure your form method is set to POST.

<form action="..." method="post">

And also, you're best to set the select name as an array to store said selections:

<select name="cities[]" multiple="multiple">

Not to mention that your actual options within this select need values...otherwise they'd come up empty.

<option value="YOUR VALUE HERE..">...</option>
Sign up to request clarification or add additional context in comments.

3 Comments

Thnx, It's work when I add value: echo "<option value=$row[city]>$row[city]</option>";
Did you do the other stuff I showed you? Change cities to cities[] then Loop through -> foreach($_POST['cities'] as $city) { echo $city; }
@SamirJunaid perfect!
0

Use Post Method

<form action="..." method="post">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.