Reputation: 69
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 name="multipleSelect[]" multiple>
<option value="" disabled selected>Choose your option</option>
<option name="esea" value="esea">ESEA</option>
<option name="faceit" value="faceit">FaceIT</option>
<option name="matchmaking" value="matchmaking">Matchmaking</option>
</select>
<label>What are you looking to play?</label>
And this is my php:
$esea = 0;
$faceit = 0;
$matchmaking = 0;
foreach ( $_POST['multipleSelect'] as $value ) {
if ( $value == 'esea' ) { $esea = 1; }
if ( $value == 'faceit' ) { $faceit= 1; }
if ( $value == 'matchmaking' ) { $matchmaking= 1; }
}
// Sätt in dataN
$sql = "INSERT INTO users ( steamid, profilename, profileurl, avatar, region, age, ranks, esea, faceit, matchmaking, textarea1 ) VALUES (
'{$mysqli->real_escape_string($_POST['steamid'])}',
'{$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'])}',
$esea,
$faceit,
$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.
Upvotes: 0
Views: 2509
Reputation: 94642
Your <select>
for a multiple selectable dropdown needs to be an array, defined by name="NameThatMakesSense[]"
<select name="NameThatMakesSense[]" multiple>
This will then return you an field called $_POST['NameThatMakesSense'] which itself is an array containing 1 or more values indicating which items in the dropdown were selected. From the value=""
attribute of the <option>
tag
Then your will need to preprocess the NameThatMakesSense
array to pick up which values were actually selected form the multi select dropdown.
$esea = 0;
$esea = 0;
$esea = 0;
foreach ( $_POST['NameThatMakesSense'] as $value ) {
if ( $value == 'esea' ) { $esea = 1; }
if ( $value == 'faceit' ) { $faceit= 1; }
if ( $value == 'matchmaking' ) { $matchmaking= 1; }
}
$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'])}',
$esea,
$faceit,
$matchmaking,
'{$mysqli->real_escape_string($_POST['textarea1'])}')";
Upvotes: 1
Reputation: 2404
Your select
html tag needs to have a name
attribute in order for the form onSubmit to send to the "backend".
like so:
<select name="enterNameThatMakesSense" multiple> // <----- HERE
<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>
Upvotes: 2