Best Way To Get Select Values

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Best Way To Get Select Values

Post by Aleeious »

I have a form:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Aleeious - Initiate Challange</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="templates/css/style.css" media="screen" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="mobile-web-app-capable" content="yes" />
</head>
 <body>
<h1>Start Challenge!</h1>
<form action="" method="post">
<label for="defender">Defender</label><input type="text" name="defender" id="defender" title="type the username of the person you want to challenge" inputmode="latin" tabindex="1">
<fieldset>
<legend>Attacks</legend>
<select name="attack1" tabindex="2">
<option value="3">Attack High</option>
<option value="2">Attack Medium</option>
<option value="1">Attack Low</option>
</select>
<select name="attack2 "tabindex="3">
<option value="3">Attack High</option>
<option value="2">Attack Medium</option>
<option value="1">Attack Low</option>
</select>
<select name="attack3" tabindex="4">
<option value="3">Attack High</option>
<option value="2">Attack Medium</option>
<option value="1">Attack Low</option>
</select>
<select name="attack4" tabindex="5">
<option value="3">Attack High</option>
<option value="2">Attack Medium</option>
<option value="1">Attack Low</option>
</select>
<select name="attack5" tabindex="6">
<option value="3">Attack High</option>
<option value="2">Attack Medium</option>
<option value="1">Attack Low</option>
</select>
<select name="attack6" tabindex="7">
<option value="3">Attack High</option>
<option value="2">Attack Medium</option>
<option value="1">Attack Low</option>
</select>
</fieldset>
<fieldset>
<legend>Blocks</legend>
<select name="block1" tabindex="8">
<option value="3">Block High</option>
<option value="2">Block Medium</option>
<option value="1">Block Low</option>
</select>
<select name="block2" tabindex="9">
<option value="3">Block High</option>
<option value="2">Block Medium</option>
<option value="1">Block Low</option>
</select>
<select name="block3" tabindex="10">
<option value="3">Block High</option>
<option value="2">Block Medium</option>
<option value="1">Block Low</option>
</select>
<select name="block4" tabindex="11">
<option value="3">Block High</option>
<option value="2">Block Medium</option>
<option value="1">Block Low</option>
</select>
<select name="block5" tabindex="12">
<option value="3">Block High</option>
<option value="2">Block Medium</option>
<option value="1">Block Low</option>
</select>
<select name="block6" tabindex="13">
<option value="3">Block High</option>
<option value="2">Block Medium</option>
<option value="1">Block Low</option>
</select>
</fieldset>
<input type="submit" name="submit" id="submit" title="click here to send challenge" tabindex="14" value="challenge">
</form>
<p><a href="main.php" title="Click here to return to the main menu" accesskey="1">back</a></p>
</body>
</html>
I want to get the values of all the select fields without having to write an if statement for every possible value for every select field. I intend to turn each value into an array. For Example:
High,Low,Mid,Low,Mid,High
Mid,Low,High,Low,High,Mid

Code: Select all

312123 213132
The other player of course would do the same after accepting a challenge but that's for a different time :P Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Best Way To Get Select Values

Post by Winawer »

You mean something like

Code: Select all

<?php
for( $i = 1; $i <= 6; $i++ ) {
    ?>
    <select name="attack[<?php echo $i ?>]" tabindex="2">
    <option value="3">Attack High</option>
    <option value="2">Attack Medium</option>
    <option value="1">Attack Low</option>
    </select>
    <?php
}
and

Code: Select all

<?php
foreach( $_POST['attack'] as $key => $attackType ) {
    //do stuff
}
?
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Best Way To Get Select Values

Post by KyleMassacre »

couldnt you just do something like:

Code: Select all

foreach($_POST as $type => $value)
{
     //do all your stuff here, possibly json_encode() it for insertion?
}
 
The problem I see with the above is that you are limiting it in a way. Lets say you want to expand on it, you will have to change the for()'s expr2. This way you are grabbing all the post's key/value pairs.
Post Reply

Return to “Beginner Help and Support”