Jump to content

Echo Value Of A Checkbox


warrenk

Recommended Posts

I am trying to view the value of a checkbox.  When I use the code below, I get the value with extra information

 

if($_SERVER['REQUEST_METHOD']=='POST') {

    $a=$_POST['checkbox'];

    print_r($a);

}

 

The value I am getting from the above code - 'Array ( [0] => mm_r.gif)'

 

....I just want the actual value - 'mm_r.gif'

Link to comment
https://forums.phpfreaks.com/topic/37659-echo-value-of-a-checkbox/
Share on other sites

if($_SERVER['REQUEST_METHOD']=='POST') {

  $a=$_POST['checkbox'];

  echo $a;

  echo "<br>";

  print_r($a);

}

 

Here are the results:

 

Array  <==========results of echo $a

Array ( [0] => 27292181.jpg )  <=========results of print_r($a)

 

The value I am looking for is the file name (ie. 27292181.jpg).

 

Thanks for any help,

Warren

the above will only work for one check box, other wise you need to place the checkboxes in an array. So... If your checkboxes look like so...

 

Square brace must be on the end, it defines an array

<input type="checkbox" value="123.jpg" name="view[]">
<input type="checkbox" value="456.jpg" name="view[]">
<input type="checkbox" value="789.jpg" name="view[]">

 

then your php will look like this:

<?php
if($_SERVER['REQUEST_METHOD']=='POST') {
     foreach($_POST['view'] as $value){
          echo $value.'<br>';
     }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.