0

I tried to pass a checkbox array to the next page but it poop out this error. or is there any way to pass the array to the next page in my situation? I searching way to compare the product after checkbox checked

<form method="post" action="">
    <div class="card">
        
        <div class="pack_image"><img src="./image/china.jpg"/></div>
        <div class="column">
            <div class="pack_name"><a href="pack.php?view&pack_id=<?php echo $pack_id;?>"><?php echo $pack_name;?></a></div>
            <div class="pack_price"><?php echo "RM ", $pack_price;?></div>
            
            <div class="button">
                <!-- <span class="pack_id" name="pack_id" value=""><?php echo $pack_id;?></span>
                <a href="#" class="more" name="compare" value="">Add to Compare</a> -->
                <!-- <input type="hidden" name="pack_id" id="pack_id" class="pack_id"><?php echo $pack_id;?></input> -->
                
                <input type="checkbox" name="array[]" id="pack_id" class="pack_id" value="<?php echo $pack_id;?>"/><Label>Add to Compare</Label>

                <a href="favourite.php?favourite&pack_id=<?php echo $pack_id?>" name="favourite"><i class="fa-solid fa-heart"></i></a>
                <a href="cart.php?cart&pack_id=<?php echo $pack_id?>" name="cart"><i class="fa-solid fa-cart-shopping"></i></a>
            </div>
        </div>  
    </div>
            
        

<input type="submit" name="submit" value="Submit"/>
</form>

Below are the PHP to pass it.

<?php
if(isset($_POST['submit'])){//to run PHP script on submit
        if(!empty($_POST['array'])){
             // Loop to store and display values of individual checked checkbox.
            foreach($_POST['array'] as $selected){
                echo $selected."</br>";
            header('Location: compare.php?submit&pack_id='.$selected);
            }
        }
        
    }
            
?> 
Rinka Wai
  • 11
  • 3
  • `header()` does not cause the script to stop execution, you should `exit` if you want to force the redirect. – Jaquarh Apr 18 '22 at 20:35

1 Answers1

0

you cannot place header after echo, comment out echo or raise header higher

or use javascript for redirect:

foreach($_POST['array'] as $selected){
            echo $selected."</br>";
            echo '<script>window.location.href="/compare.php?submit&pack_id='.$selected.'";</script>';
        }
ooo
  • 43
  • 1
  • 9