I've no clue if this is possible, I was just wondering to myself.
Is it possible to make a form input have more than one value? Like putting an array or something.
Like say I wanted one select input have the values 'blue', 'solid', and '1'. Can I make it into an array somehow, and then retrieve each value with PHP?
Multiple Form Values?
Started by Demonslay, Jul 14 2006 01:16 AM
6 replies to this topic
#1
Posted 14 July 2006 - 01:16 AM
#2
Posted 14 July 2006 - 01:34 AM
i'm not so sure on your question, but if you got predefined values then sure, it'd be pretty easy, hers a little somethin somethin...
i haven't tested it, but it should work, basically you define the value and use the explode() function to seperate the value!
<?
if( isset($_POST['submit']) )
{
// splits the value up
$array = explode("|", $_POST['select1']);
$color = $array[0];
$type = $array[1];
$width = $array[2];
echo $color . $type . $width ."<br />\n";
}
else
{
echo "<form action=\"". $_SERVER['PHP_SELF'] ."\" method=\"post\">\n";
echo " <select name=\"select1\">\n";
echo " <option value=\"blue|solid|1\">Blue / Solid / 1</option\n";
echo " </select>\n";
ehco "</form>\n";
}
i haven't tested it, but it should work, basically you define the value and use the explode() function to seperate the value!
#3
Posted 14 July 2006 - 01:55 AM
Ah! I see. Now it makes sense, thanks. 
I never thought of using explode(), lol, I'm too used to list(). Plus I was thinking something about multi-dimensional arrays and making something stupid like 'value="something[]"', but that's for adding multiple values from different inputs into one value, but that wouldn't be what I wanted.
I don't have an immediate use for this, it was just something that passed my mind as I was doing something else.
Your code looks logical enough that it should work, so thanks.
Oh, and you missed a greater-than sign in your closing options tag, and you have some un-needed space in your form tag's method attribute, by the way.
I never thought of using explode(), lol, I'm too used to list(). Plus I was thinking something about multi-dimensional arrays and making something stupid like 'value="something[]"', but that's for adding multiple values from different inputs into one value, but that wouldn't be what I wanted.
I don't have an immediate use for this, it was just something that passed my mind as I was doing something else.
Your code looks logical enough that it should work, so thanks.
Oh, and you missed a greater-than sign in your closing options tag, and you have some un-needed space in your form tag's method attribute, by the way.
Edited by Demonslay, 14 July 2006 - 01:57 AM.
#4
Posted 14 July 2006 - 09:09 AM
Coolaid, this may be irrelevant but why did you use | as a seperator? Or is it jsut a matter of preference?
#5
Posted 14 July 2006 - 09:14 AM
Its completly a matter of preference, many people use the pipe symobol ( | ) as its not commonly used.
#6
Posted 14 July 2006 - 10:24 AM
Lang, on Jul 14 2006, 02:08 PM, said:
Coolaid, this may be irrelevant but why did you use | as a seperator? Or is it jsut a matter of preference?
.Matt, on Jul 14 2006, 02:13 PM, said:
Its completly a matter of preference, many people use the pipe symobol ( | ) as its not commonly used.
Lang: As Matt just said, however make sure you don't use a separator that could be used in the data input
#7
Posted 14 July 2006 - 12:21 PM
If you want a select box that can select multiple values, and you want to get all the values used, you have to make the name an array.
i.e.
i.e.
<select name="something[]"> <!-- options --> </select> <? if($_POST['something']) highlight_string(print_r($_POST['something'],1)); ?>
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
