简体   繁体   中英

Retrieve two GET parameters from a url and echo out in PHP

I have a form with multiple checkboxes and hidden inputs, which I'm passing to a second page using GET. I'm then trying to retrieve the value of each checkbox and the input in a loop and echo out the combined value.

HTML:

<form action="criteria.php" method="GET">
<input name="id[]" type="hidden" value="<? echo $criteria_id; ?>" />
<input type="checkbox" name="checked[]" class="checkbox-md" id="<? echo $criteria_id; ?>" value="Y">
<button type="submit" class="btn btn-lilac" role="button">Complete</button>
</form>

PHP:

$criteria_id = $_GET['id']; //get all criteria id
$criteria_checked = $_GET['checked']; //get checked criteria id
foreach($criteria_id as $id) //get id of all checkboxes {
    echo "<BR>Criteria = ".$id."Checked = ".$criteria_checked; //returns id + array?
    if ($checked='Y')//check if checked {
        echo "<BR>Criteria =".$id." Checked = Y";
    } else {
        echo "<BR>Criteria =".$id." Checked = N";
    }
}

You will need to make sure that the inputs have matching array keys:

<input name="id[0]" type="hidden" . . .
<input name="checked[0]" type="checkbox" . . .

<input name="id[1]" type="hidden" . . .
<input name="checked[1]" type="checkbox" . . .

Depending on how you create these you could use the $criteria_id :

<input name="id[<? echo $criteria_id; ?>]" type="hidden" . . .
<input name="checked[<? echo $criteria_id; ?>]" type="checkbox" . . .

This way the id and checked array keys will match. All hidden inputs will be passed from the form but only the checked checkboxes, so check if the key of the id is set in the checked array:

foreach($_GET['id'] as $key => $id) {
    if (isset($_GET['checked'][$key])) {
        echo "<BR>Criteria =".$id." Checked = Y";
    } else {
        echo "<BR>Criteria =".$id." Checked = N";
    }
}

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