Tim Tim - 7 months ago 35
HTML Question

Using POST to get select option value from HTML form

I have a form where i get some different information. But then i have a Select with multiple options and i want to be able to send this to the database in 1 or 0 / true or false. But it is not working. This is my code so far:

<select multiple>
<option value="" disabled selected>Choose your option</option>
<option name="esea" value="1">ESEA</option>
<option name="faceit" value="1">FaceIT</option>
<option name="matchmaking" value="1">Matchmaking</option>
</select>
<label>What are you looking to play?</label>


And this is my php:

$sql = "INSERT INTO users
( profilename, profileurl, avatar, region,
age, esea, faceit, matchmaking, textarea1 )
VALUES (

'{$mysqli->real_escape_string($_POST['profilename'])}',
'{$mysqli->real_escape_string($_POST['profileurl'])}',
'{$mysqli->real_escape_string($_POST['avatar'])}',
'{$mysqli->real_escape_string($_POST['region'])}',
'{$mysqli->real_escape_string($_POST['age'])}',
'{$mysqli->real_escape_string($_POST['ranks'])}',
'{$mysqli->real_escape_string($_POST['esea'])}',
'{$mysqli->real_escape_string($_POST['faceit'])}',
'{$mysqli->real_escape_string($_POST['matchmaking'])}',
'{$mysqli->real_escape_string($_POST['textarea1'])}')";

$insert = $mysqli->query($sql);


I know that this code doesnt work but i don't know what to do to make it work. I wan't to send a 1 or a 0 depending on if the chose the alternative or not.

Answer

Your <select> for a multiple selectable dropdown needs to be an array, defined by name="enterNameThatMakesSense[]"

<select name="enterNameThatMakesSense[]" multiple>

And then processed like this in PHP, for example

foreach ( $_POST['enterNameThatMakesSense'] as $val) {
    echo $val;
}
Comments