tom030888 tom030888 - 4 months ago 31
PHP Question

how to get items from the option of a <datalist> in php

$_POST
return null value in the database. Why does this happen?

<datalist name="countries" class="form-control">
<option value="India">India</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Germany">Germany</option>
<option value="France">France</option>
</datalist>

<?php
if (isset($_POST['countries'])){
$country=$_POST['countries'];
}else{
echo 'no value';
}
?>

Answer

This is just a normal form submission.

<?php

if(isset($_POST['submit'])) {
    $country = $_POST['countries'];
    echo $country;
}

?>

<form method="POST">
    <input list="countries" name="countries" />
    <datalist id="countries">
        <option value="India">India</option>
        <option value="United States">United States</option>
        <option value="United Kingdom">United Kingdom</option> 
        <option value="Germany">Germany</option> 
        <option value="France">France</option> 
    </datalist>
    <input type="submit" name="submit" />
</form>

Notice: This is a relatively new tag, so some browsers might not support this. I suggest just use plain <select> tag