/    Sign up×
Community /Pin to ProfileBookmark

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>[/code]

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?

to post a comment
PHP

29 Comments(s)

Copy linkTweet thisAlerts:
@pyroJun 27.2003 — 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:

[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]
Copy linkTweet thisAlerts:
@diamondsauthorJun 27.2003 — can you return, say if the 2nd on the list is selected or not?
Copy linkTweet thisAlerts:
@pyroJun 27.2003 — Yes, like this:

[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]
Copy linkTweet thisAlerts:
@diamondsauthorJun 28.2003 — 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?
Copy linkTweet thisAlerts:
@pyroJun 28.2003 — 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...
Copy linkTweet thisAlerts:
@diamondsauthorJun 30.2003 — I know how to do it in JS, but how do you do string commands in PHP? What is the command for [COLOR=darkred]indexof()[/COLOR] ?

If i can use that, than

[COLOR=darkred]if($multi.indexof('1')!= -1){echo "yes";}else{echo "no!";}[/COLOR]

Right?

how about somthing to that sort?
Copy linkTweet thisAlerts:
@pyroJun 30.2003 — I believe the PHP equivalent of indexOf is [URL=http://us2.php.net/manual/en/function.strstr.php]strstr()[/URL]
Copy linkTweet thisAlerts:
@diamondsauthorJun 30.2003 — How do you use it?-

err... I just relised you had a link...

you mean [URL=http://us2.php.net/manual/en/function.strpos.php]strpos()[/URL] (I did my homework ? )

do you have an example?
Copy linkTweet thisAlerts:
@diamondsauthorJun 30.2003 — found my own(well, modified from php.net):
[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]


(finished extra credit ? )
Copy linkTweet thisAlerts:
@pyroJul 01.2003 — [i]Originally posted by diamonds [/i]

[B]you mean [URL=http://us2.php.net/manual/en/function.strpos.php]strpos()[/URL] (I did my homework ? )[/B][/QUOTE]
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.[/quote]


From php.net manual

strstr -- Find first occurrence of a string[/quote]

From php.net manual

strpos -- Find position of first occurrence of a string[/quote]
Copy linkTweet thisAlerts:
@diamondsauthorJul 01.2003 — 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!
Copy linkTweet thisAlerts:
@pyroJul 01.2003 — What exactly are you trying to do? From the looks of it, a regexp might be more appropriate anyway...
Copy linkTweet thisAlerts:
@diamondsauthorJul 01.2003 — Well, now I'm trying to use the function to find if a value is returned in a muli select or not.
Copy linkTweet thisAlerts:
@pyroJul 01.2003 — So you are just trying to see if they selected an option or not? Try something like this:

[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]
Copy linkTweet thisAlerts:
@diamondsauthorJul 01.2003 — Well, thats not [i]quite[/i] it.

What I want is to see if a one, single value is returned / selected by the user.

I just made this 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]


How come it says $v is an undefined variable, and yet when I go [COLOR=darkred]echo $v;[/COLOR] it returns everything I selected?
Copy linkTweet thisAlerts:
@JonaJul 02.2003 — 
[b]strstr -- Find first occurrence of a string



strpos -- Find position of first occurrence of a string[/b]
[/quote]


[font=arial][color=maroon]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.[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — The problem was on this line:
[code=php]$v = $v.$value;[/code]
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:[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]
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — 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?
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — [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]


I just noted that your comment claims you can send it as an array. how do you have PHP read it?
Copy linkTweet thisAlerts:
@JonaJul 03.2003 — [font=arial][color=maroon]I would make your $v thing like this:[/color][/font]

[code=php]
$v .= $value;
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — that works, too? (I tryed that but got the sum, because I was using += :rolleyes: ) However, I still need to set the variable beforehand!
Copy linkTweet thisAlerts:
@JonaJul 03.2003 — [i]Originally posted by diamonds [/i]

[B]However, I still need to set the variable beforehand! [/B][/QUOTE]


[font=arial][color=maroon]Always. ? Try making a habit of that.[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — Now, like I said, Is there any way to make turn a variable into a string (just like you can use the [COLOR=darkred]touppercase()[/COLOR] function to make a string uppercase...), because I need to do that to some numbers like I explained!
Copy linkTweet thisAlerts:
@JonaJul 03.2003 — [font=arial][color=maroon]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):[/color][/font]

[code=php]
<?PHP
if("12" > "10"){ echo("12 is greater than 10.");}
?>
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — 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
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — I [B]knew[/B] 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? -well, anyway, its


[URL=http://www.php.net/manual/en/function.settype.php][COLOR=darkred]settype()[/COLOR][/URL]
Copy linkTweet thisAlerts:
@diamondsauthorJul 03.2003 — ? [SIZE=1][B]grr...[/B][/SIZE]

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!
    Copy linkTweet thisAlerts:
    @JonaJul 03.2003 — [font=arial][color=maroon]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.[/color][/font]

    [b][J]ona[/b]
    Copy linkTweet thisAlerts:
    @JonaJul 04.2003 — [font=arial][color=maroon]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.[/color][/font]

    [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]


    [b][J]ona[/b]
    ×

    Success!

    Help @diamonds spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...