I made this short code, wondering if and how php gets all the values from a multi select:
[code=php]<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body>
<form name=”form1″ method=”post” action=”index.php?action=post”>
<p><select name=”select” size=”6″ multiple>
<option value=”1″ selected>1</option>
<option value=”2″>2</option>
<option value=”3″ selected>3</option>
<option value=”4″>4</option>
<option value=”5″ selected>5</option>
<option value=”6″>6</option>
</select>
</p>
<p>
<input type=”submit” name=”Submit” value=”Submit”>
</p>
</form>
<p>
<?php if(@$HTTP_GET_VARS[‘action’]==’post’){echo $HTTP_POST_VARS[‘select’];} ?></p>
</body>
</html>
and found it only returned the last one selected. How can I make a script returning all of them as a list, like:
is no. 1 selected? yes
no. 2? no
no 3? yes…
how can I do this?
[code=php]<form name="form1" method="post" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">
<p><select name="myselect[]" size="6" multiple="multiple">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
<p>
<?php if(isset($_POST['submit'])) {
foreach ($HTTP_POST_VARS['myselect'] as $value) {
echo $value;
};
}
?>
</p>[/code]
[code=php]<?php if(isset($_POST['submit'])) {
foreach ($HTTP_POST_VARS['myselect'] as $value) {
if ($value == "2") {
echo "number 2 is selected";
}
else {
echo "number 2 is not selected";
}
};
}
?>[/code]
[code=php]<html>
<head>
</head>
<body>
<?php
if(isset($HTTP_POST_VARS['s'])){
$mystring = @$HTTP_POST_VARS['s'];
$findme = @$HTTP_POST_VARS['f'];
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
}
?>
<form name="form1" method="post" action="find.php">
<p>
<input name="s" type="text" id="s">
string<br>
<input name="f" type="text" id="f">
to find<br>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
[/code]
[i]Originally posted by diamonds [/i]Hmm... not sure which would be technically closest to the javascript indexOf()
[B]you mean[URL=http://us2.php.net/manual/en/function.strpos.php]strpos()[/URL] (I did my homework ? )[/B][/QUOTE]
indexOf
Returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, or -1 if the value is not found.[/quote]
strstr -- Find first occurrence of a string[/quote]
strpos -- Find position of first occurrence of a string[/quote]
[code=php]<?PHP
if (isset($_POST["submit"])) {
if (isset($_POST["myselect"])) {
echo "Options were selected";
}
else {
echo "No options were selected";
}
}
?>
<html>
<head>
</head>
<body>
<form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="post">
<!-- The brackets in the select name are important, once we get to the PHP part... It tells the script that it is an array -->
<select name="myselect[]" multiple="multiple">
<option value="1">1
<option value="2">2
<option value="3">3
</select>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>[/code]
[code=php]<?php if(isset($_POST['myselect'])) {
foreach($HTTP_POST_VARS['myselect'] as $value) {
$v = $v.$value;
};
for ($i = 1; $i <= 6; $i++) {
echo $i.". ";
$pos = strpos($v, $i);
if ($pos === false) {
echo "No<br>";
} else {
echo "Yes<br>";
}
}
}
?>[/code]
[b]strstr -- Find first occurrence of a string
strpos -- Find position of first occurrence of a string[/b][/quote]
[code=php]$v = $v.$value;[/code]
[code=php]<?php if(isset($_POST['myselect'])) {
$v = '';
foreach($HTTP_POST_VARS['myselect'] as $value) {
$v = $v.$value;
};
for ($i = 1; $i <= 6; $i++) {
echo $i.". ";
$pos = strpos($v, $i);
if ($pos === false) {
echo "No<br>";
} else {
echo "Yes<br>";
}
}
}
?>[/code]
[i]Originally posted by pyro [/i]
[B]<!-- The brackets in the select name are important, once we get to the PHP part... It tells the script that it is an array -->[/B][/QUOTE]
[code=php]
$v .= $value;
[/code]
[i]Originally posted by diamonds [/i]
[B]However, I still need to set the variable beforehand! [/B][/QUOTE]
[code=php]
<?PHP
if("12" > "10"){ echo("12 is greater than 10.");}
?>
[/code]
[code=php]
<?PHP
if(!isset($_GET["node"]) || $_GET["node"] != "process"){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head><title>Testing with Select Boxes and Arrays</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<form action="selAry.php?node=process" method="POST"><div>
<select size="4" name="selObj[]" multiple="multiple">
<option value="A">One</option>
<option value="B">Two</option>
<option value="C">Three</option>
<option value="D">Four</option>
</select><br>
<input type="submit">
</div></form>
</body></html>
<?} else {?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head><title>Testing with Select Boxes and Arrays</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<p><?
$v = "";
foreach($_POST['selObj'] as $value) {
$v .= $value;
for ($i=1; $i<5; $i++) {
echo $i.". (";
echo $value.") ";
$pos = split($value, $i-1);
if($pos == $i){echo "No<br>";}
else{echo "Yes<br>";}
}
};
?></p>
</body></html>
<?}?>
[/code]
0.1.9 — BETA 6.16