James Osguthorpe
James Osguthorpe

Reputation: 171

Multiple Checkbox Values PHP

I've got this code, anyone help on making it work...

while($fetch = mysql_fetch_assoc($query)){

    $image_name = $fetch['image_name'];
    $image_location = $fetch['image_location'];


    <img src="'.$image_location.'"/>

    <input name="image['.$image_id.'][6x7]" type="checkbox" value="'.$image_name.'" /> 6x4
    <br />
    <input name="image['.$image_id.'][8x9]" type="checkbox" value="'.$image_name.'" /> 7x5

}

Display on different page...

foreach($_POST['image'] as $image_id => $name) {

    $uploaded[] = $name;

    echo''.$name.'<br />';


    foreach($name as $size => $size) {

        $uploaded[] = $size;

        echo''.$size.'<br />';

    }
}

Input Script

mysql_query("INSERT INTO table VALUES('', '$name', 'size')");

I am uploading both image name and size (from the checkboxes 6x4, 7x5) I am able to input the size into the database but image name just says Array.

I know this is an obvious fix but I just can't get my head around the multiple arrays is the checkbox values!

Any help of feedback is welcome! HELP!

Upvotes: 1

Views: 148

Answers (2)

Mateusz Drankowski
Mateusz Drankowski

Reputation: 101

I assume your $image_location and $image_name are fine so you have correct values on your checkboxes? Otherwise we would need some more details..

Try below to display that correctly then (you're using multidimensional array):

foreach($_POST['image'] as $image_id => $image) {

    foreach($image as $size => $name) {


        $uploaded[] = $name;
        echo''.$name.'<br />';


        $uploaded[] = $size;
        echo''.$size.'<br />';
    }
}

Upvotes: 2

Raphioly-San
Raphioly-San

Reputation: 404

<input name="image['.$image_id.'][]" type="checkbox" value="6x7" /> 6x4
<br />
<input name="image['.$image_id.'][]" type="checkbox" value="8x9" /> 7x5

The value should not be the image name, but the size you specified. You already have the image ID, so why include the filename? You can just extract that from your database again...

Also:

foreach($name as $size => $size) {

... is a bit weird: Using the $size var for key and value? That's asking for trouble. ;)

Upvotes: 1

Related Questions