Jump to content



Photo

Question On An Array Variable In A Checkbox Input


  • Please log in to reply
3 replies to this topic

#1 chop

chop

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 28 January 2012 - 9:33 AM

Looks like an easy question here but I just don't see it.. getting old:

Why doesn't this work:

if (isset($_POST['submitted'])) { // Handle the form

// Trim all the incoming data:
	$trimmed = array_map('trim', $_POST);
echo "The media is ".$_POST['media[0]']; [color=#ff0000]// THIS COMES UP WITH NO VALUE: "The media is  "[/color]
exit();

// CHECK BOX INPUTS WITHIN FORM
<p>* check all that apply but at least one of the following:</p>
	  <p>
		<label>
		  <input type="checkbox" [color=#ff0000]name="media[]"[/color] value="acrylic" id="" />[color=#ff0000]// this is the first in the list[/color]
		acrylic</label>
		<br />
		<label>
		  <input type="checkbox" name="media[]" value="checkbox" id="" />
		  Checkbox</label>
		<br />
		<label>
		  <input type="checkbox" name="media[]" value="charcoal" id="" />

note that "print_r($_POST['media']);" works fine and prints the whole array
  • 0

#2 Edward

Edward

    Advanced Member

  • Members
  • PipPipPip
  • 1,134 posts

Posted 28 January 2012 - 2:26 PM

What is the result of the php error handler, which part exactly doesn't work?
  • 1

#3 rob

rob

    Advanced Member

  • Members
  • PipPipPip
  • 136 posts

Posted 29 January 2012 - 3:44 AM

media will be an array within the $_POST array; to access the values you need to use the method you would for a multimensional array $_POST['media'][0]. Alternatively, you could assign $_POST['media'] to a variable and then access the media values using $array[0].
  • 1

#4 chop

chop

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 31 January 2012 - 7:12 PM

I understand, makes sense now. I knew my syntax was incorrect but wasn't getting an error. This worked for Me;

$mediaArray=$_POST['media'];
echo $mediaArray[0]; //works fine
$_POST['media'][0]; // works as well

thank you
  • 0