简体   繁体   中英

Relating values in a PHP multiple checkbox array

I searched and can't find an answer to this and nothing I try works. In this form, for each image (pix) and there the user enters the number of copies to be produced.

I know how to build the array for each individually when the submit button is clicked but I can't get both arrays to relate to each other. I want to produce something like: 01.jpg - 3 copies, 02.jpg - 1 copy etc etc.

How can I achieve this?

Thanks

This is my form:

 <form action="self.php" method="post"> <img src="01.jpg" alt="01.jpg"> <input type="checkbox" name="pix[]" value="01.jpg_album"> <input type="text" name="quantity[]"> <img src="02.jpg" alt="02.jpg"> <input type="checkbox" name="pix[]" value="02.jpg_extra"> <input type="text" name="quantity[]"> <img src="03.jpg" alt="03.jpg"> <input type="checkbox" name="pix[]" value="03.jpg_extra"> <input type="text" name="quantity[]"> <input type="submit" name="submit"> </form> 

This is how I extract the values returned:

 echo implode('<br>', $_POST['pix']); echo implode('<br>', $_POST['quantity']); 

EDITED: FURTHER to my question above and in response to the second code posted by Rajdeep ... I've separated the two processes. The first was to determine whether the client wanted the image for his wedding album, so I did:

  $arr = array("01", "02", "03"); echo "<h3>Images required for album: </h3>"; foreach($_POST as $key => $value){ if(in_array($key, $arr)){ $var = $value . ".jpg"; if(isset($_POST[$value]) && !empty($_POST[$value])){ echo $var."<br />"; }//endif } //endif }// end foreach 

This output was:

03.jpg
04.jpg
23.jpg
etc

Then, I wanted to know if he needed any additional individual prints and this code (sort of) worked for that:

 echo "<h3>Extras required: </h3>"; $noextras = false; foreach($_POST as $key => $value){ if(in_array($key, $arr)){ $var = $value . ".jpg - "; if(isset($_POST[$value . "_extra"]) && !empty($_POST[$value . "_extra"])){ $sum = $_POST[$value . '_extra']; if($sum == 1) { $var .= $sum ." copy <br />"; } else { $var .= $sum . " copies <br />"; } echo $var; } else if(empty($_POST[$value . "_extra"])) { $noextras = true; } } //endif in_array }// end foreach if($noextras) { echo "No extra copies needed.";} 

And the output from this was something like

03.jpg - 5 copies
04.jpg - 2 copies
23.jpg - 1 copy

I then discovered a snag. This permitted extra copied to be selected for ONLY for images that were selected for inclusion in the album. I have tried various variations on the code and I am coming up with a blank.

So, I've changed this to a two step process. First page, the person selects the images for the album, which when submitted go to a confirmation page to show what had been selected. Once he clicks the 'confirm' button an email goes to me and him.

Clicking the confirm button also goes to a Thank You page which then refreshes into the second page for ordering individual prints. Here he can select how many copies of a print he needs and this works well with Rajdeep's first 'merged' code BUT it only works for one selection.

I've expanded the selection and this is where I am currently having trouble. He has a choice to decides how many copies he needs of each of the four different sizes available (7x6, 9x5, 10x8, 12x8). I am able to process one size only but am having trouble with expanding it to four inputs.

Any suggestions?

Thanks.

Instead of two different <input> tags with checkbox and text attribute, use only one <input> with number attribute for each image. And make the name attribute as the name of your image, like this:

<form action="self.php" method="post">
    <img src="01.jpg" alt="01.jpg" />
    <input type="number" name="01" min="0" />

    <img src="02.jpg" alt="02.jpg" />
    <input type="number" name="02" min="0" />

    <img src="03.jpg" alt="03.jpg" />
    <input type="number" name="03" min="0" />

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

And this is how you can process the form,

if(isset($_POST['submit'])){
    foreach($_POST as $key => $value){
        if($key == "submit"){
            continue;
        }
        if(!empty($value)){
            $var = $key . ".jpg - " . $value;
            if($value == 1){
                $var .= " copy";
            }else{
                $var .= " copies";
            }
            $var .= "<br />";
            echo $var;
        }
    }
}

A sample output would be like this:

01.jpg - 3 copies
02.jpg - 1 copy
03.jpg - 5 copies

Edited:

Based on your requirement, use <input type="checkbox" name="xx" value="xx" /> for selecting the image(ie when user wants this image to be included in photo album) and use <input type="number" name="xx_extra" min="1" /> for keeping track of how many extra copies of this image user wants.

So your HTML code should be like this:

<form action="self.php" method="post">
    <img src="01.jpg" alt="01.jpg" />
    <input type="checkbox" name="01" value="01" />
    <input type="number" name="01_extra" min="1" />

    <img src="02.jpg" alt="02.jpg" />
    <input type="checkbox" name="02" value="02" />
    <input type="number" name="02_extra" min="1" />

    <img src="03.jpg" alt="03.jpg" />
    <input type="checkbox" name="03" value="03" />
    <input type="number" name="03_extra" min="1" />

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

And this is how you can process the form,

<?php

    if(isset($_POST['submit'])){

        $arr = array("01", "02", "03");

        foreach($_POST as $key => $value){
            if(in_array($key, $arr)){
                $var = $value . ".jpg - ";
                if(isset($_POST[$value . "_extra"]) && !empty($_POST[$value . "_extra"])){
                    $sum = $_POST[$value . '_extra'] + 1;
                    $var .= $sum . " copies <br />";
                }else{
                    $var .= "1 copy <br />";
                }
                echo $var;
            }
        }
    }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM