0

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.

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

And if you actually want multiple values in $_POST you need an array: enterNameThatMakesSense[]
But <option> tags do not have a name attribute
Thank you Pedro for taking the time to help! :)
1

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'])}')";

10 Comments

How do i pick this up post tho? Do I still use '{$mysqli->real_escape_string($_POST['esea'])}', '{$mysqli->real_escape_string($_POST['faceit'])}', '{$mysqli->real_escape_string($_POST['matchmaking'])}',
Thanks for you help RiggsFolly! :)
what should i put in here: '{$mysqli->real_escape_string($_POST[''])}', for it to pick up the $val?
@Tim I have amended my question to include how to process the data received from the multi select dropdown
i've updated my question with your answer but now it doesnt work at all. It's not sending any of my data to the database.
|

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.