Jump to content


Multiple Form Values?


6 replies to this topic

#1 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 14 July 2006 - 01:16 AM

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?

#2 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

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...

<?
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 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

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. :love:

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. :ph34r:

Edited by Demonslay, 14 July 2006 - 01:57 AM.


#4 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

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 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

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 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 14 July 2006 - 10:24 AM

View PostLang, 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?


View Post.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 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

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.
<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