Click to See Complete Forum and Search --> : multi-select
diamonds
06-27-2003, 11:13 AM
I made this short code, wondering if and how php gets all the values from a multi select:
<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?
When you set up the select box, make it an array by going name[]. Then, in PHP, you can loop through the selected items and print them out. Like this:
<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>
diamonds
06-27-2003, 11:44 AM
can you return, say if the 2nd on the list is selected or not?
Yes, like this:
<?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";
}
};
}
?>
diamonds
06-27-2003, 07:12 PM
when I select 1 and 2, it says "number 2 is not selectednumber 2 is selected
"
All i want is a list, going all the way down:
1 = yes
2 = no
3 = no
4 = yes...
how is this done?
Well, the problem is the fact that it only sends the ones that are selected. So, the only way (that I know of) would be to do it manually...
diamonds
06-29-2003, 07:28 PM
I know how to do it in JS, but how do you do string commands in PHP? What is the command for indexof() ?
If i can use that, than
if($multi.indexof('1')!= -1){echo "yes";}else{echo "no!";}
Right?
how about somthing to that sort?
I believe the PHP equivalent of indexOf is strstr() (http://us2.php.net/manual/en/function.strstr.php)
diamonds
06-30-2003, 02:56 PM
How do you use it?-
err... I just relised you had a link...
you mean strpos() (http://us2.php.net/manual/en/function.strpos.php) (I did my homework :D )
do you have an example?
diamonds
06-30-2003, 03:05 PM
found my own(well, modified from php.net):
<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>
(finished extra credit :D )
Originally posted by diamonds
you mean strpos() (http://us2.php.net/manual/en/function.strpos.php) (I did my homework :D )Hmm... not sure which would be technically closest to the javascript indexOf()
From the javascript 1.4 manual:
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.
From php.net manual
strstr -- Find first occurrence of a string
From php.net manual
strpos -- Find position of first occurrence of a string
diamonds
07-01-2003, 03:52 PM
Well, mabye you are correct, but I was figuring from the fact that they both return the position of a string.
Now, how can I incorperate it into a full script? I'm havinf some troubles!
What exactly are you trying to do? From the looks of it, a regexp might be more appropriate anyway...
diamonds
07-01-2003, 04:41 PM
Well, now I'm trying to use the function to find if a value is returned in a muli select or not.
So you are just trying to see if they selected an option or not? Try something like this:
<?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>
diamonds
07-01-2003, 05:23 PM
Well, thats not quite it.
What I want is to see if a one, single value is returned / selected by the user.
I just made this code:
<?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>";
}
}
}
?>
How come it says $v is an undefined variable, and yet when I go echo $v; it returns everything I selected?
strstr -- Find first occurrence of a string
strpos -- Find position of first occurrence of a string
Actually, pyro, I think that strpos() is closer to indexOf() than strstr is, because indexOf() gets the position (index) of the first occurrence of a string, whereas something like split(string)[0] would be more like strstr()--the first occurrence of a string.
[J]ona
diamonds
07-03-2003, 11:27 AM
The problem was on this line:
$v = $v.$value;
It defined $v, than made it equel to $v, a new variable that haden't come before. The second time it ran through, the variable $v was alredy defined, explaining why the error didn't come up 100,000,000,000 times:rolleyes: (it was in a loop)
here is the new script:<?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>";
}
}
}
?>
diamonds
07-03-2003, 11:34 AM
Hmmm...
The script works! BUT only when I select one item from the list! I think numbers/strings are at fault!
heres what I think is happining:
does 1 = 12?
no.
does 1 = 1?
yes!
what I want to happen is this:
is the string '1' inside '12'?
yes!
is the string '1' inside '1'?
yes!
Is there a function that can make numbers into strings?
diamonds
07-03-2003, 11:47 AM
Originally posted by pyro
<!-- The brackets in the select name are important, once we get to the PHP part... It tells the script that it is an array -->
I just noted that your comment claims you can send it as an array. how do you have PHP read it?
I would make your $v thing like this:
$v .= $value;
[J]ona
diamonds
07-03-2003, 02:14 PM
that works, too;) (I tryed that but got the sum, because I was using += :rolleyes: ) However, I still need to set the variable beforehand!
Originally posted by diamonds
However, I still need to set the variable beforehand!
Always. :) Try making a habit of that.
[J]ona
diamonds
07-03-2003, 03:23 PM
Now, like I said, Is there any way to make turn a variable into a string (just like you can use the touppercase() function to make a string uppercase...), because I need to do that to some numbers like I explained!
Unlike JavaScript, I do believe that PHP treats numbers--whether or not they are in quotes--as numbers and not strings, although they can be used as strings as well. Example, the following would compare the numbers, even though they are in quotes (as strings):
<?PHP
if("12" > "10"){ echo("12 is greater than 10.");}
?>
[J]ona
diamonds
07-03-2003, 04:22 PM
I read somewhere(not on php.net) that there was a simple PHP function that would return if a variable was a string or a number-I tryed it on my server and it worked!
Yes! I just found It it is at http://www.php.net/manual/en/ref.variables.php
diamonds
07-03-2003, 04:29 PM
I knew there was a function for this - php has a function for everything: is the var set, echo, which horse will come second in the race:D -well, anyway, its
settype() (http://www.php.net/manual/en/function.settype.php)
diamonds
07-03-2003, 04:44 PM
:( grr...
the script STILL only works if you select only one thing!
Than I changed it to letters... same result:
1. a was not found in abc
2. b was not found in abc
3. c was not found in abc
anyone even a clue whats happining? I'm out of theroies!
I'll go through the script later when I have more time and see what you've got--I haven't really followed this thread in detail.
[J]ona
Okay, I played around with it a little, and this is what I came up with. If you want to print out $pos, use print_r($pos); while in the for() loop--otherwise you'll just get "Array." The file is named, "selAry.php" just so you know.
<?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>
<?}?>
[J]ona