0

I want to get multiple return value for checkbox. But problem is with following code check box only can echo a single value. Even I select multiple checkbox I am getting only single value form it. How to solve it

$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");

while ($row = mysqli_fetch_array($q2)) {
    $product_img_url = $row['product_img_url'];
    $target_dir = "../assets/img/temp_img/";
    $img_link = $target_dir.$product_img_url;

    echo '<br>Add it<br><input type="checkbox" name="pic_tobe_add" value="'.$img_link.'"><br><img src="'.$img_link.'" class="img-rounded" alt="Uploaded image" width="152" height="118"><br><br>';
}
if(isset($_POST['submit_p'])) {
   if (empty($_POST['pic_tobe_add'])) {
      echo "Error: select a pic";
  }else{echo $pic_tobe_add = $_POST['pic_tobe_add'];}
}
echo '<br><input type="submit" name="submit_p" value="Add this product"><form>';
1
  • 1
    Since nameof each check -box is same that's why you get only single value.change name="pic_tobe_add" to name="pic_tobe_add[]" now you will get all values. On the next page confirm it by printing out POST value using echo "<pre/>";print_r($_POST); Commented Jun 23, 2016 at 5:01

2 Answers 2

1

Actually name of each check-box is same that's why you get only single value which is last-one.

change name="pic_tobe_add" to name="pic_tobe_add[]" (Make it array type so that you will get all the value)

On the next page confirm it by printing out POST value using echo "<pre/>";print_r($_POST);

Sign up to request clarification or add additional context in comments.

4 Comments

how can i receive all of these values in $_POST then?
I already told you. Also you have </form>. but i didn't see any form open code. that's strange
yes. i have open form too above that code but i didnt post here to make it small and easy to understand. since my above codes are long about 100+ lines
your form have action = 'somepage.php' ? on that action page you have to recieve all the value . so on that page write echo "<pre/>";print_r($_POST); and check did you get all the values of check-boxes or not?
1

use <input type="checkbox" name="pic_tobe_add[]" value="'.$img_link.'">

$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");

while ($row = mysqli_fetch_array($q2)) {
    $product_img_url = $row['product_img_url'];
    $target_dir = "../assets/img/temp_img/";
    $img_link = $target_dir.$product_img_url;

    echo '<br>Add it<br><input type="checkbox" name="pic_tobe_add[]" value="'.$img_link.'"><br><img src="'.$img_link.'" class="img-rounded" alt="Uploaded image" width="152" height="118"><br><br>';

}
//
    if(isset($_POST['submit_p'])) {
        if (empty($_POST['pic_tobe_add'])) {
            echo "Error: select a pic";
        }else{echo $pic_tobe_add = $_POST['pic_tobe_add'];}

}

echo '<br><input type="submit" name="submit_p" value="Add this product"><form>';

3 Comments

Why your answer is useful and why OP should have to try that. please add some description. thanks
yes i have add name="pic_tobe_add[]" instate of name="pic_tobe_add" but problem now how can i receive it from $_POST? I need all of these results in variable.
$pic_tobe_add_arr=$_POST['pic_tobe_add']; here $pic_tobe_add_arr contains all selected pic_tobe_add values or use directly foreach($_POST['pic_tobe_add'] as $item){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.