Jump to content
Larry Ullman's Book Forums

Question On An Array Variable In A Checkbox Input


Recommended Posts

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

Link to comment
Share on other sites

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

  • Upvote 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

 Share

×
×
  • Create New...