简体   繁体   中英

How to pass value on next page from checkbox, which i checked?

I have multiple checkbox and want to pass their value on next page when i checked them. My code is

<form method="POST">
    <input type="checkbox" name="city" id="city" value="Gold" />
         <input type="checkbox" name="city" id="city" value="Platinum" />
         <input type="checkbox" name="city" id="city" value="Silver" />
    <input type="submit" value="Submit" />
</form>

By defining the name attribute as an array name="city[]"

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
    print_r($_POST);
    /*
    Array
    (
        [city] => Array
            (
                [0] => Gold
                [1] => Platinum
                [2] => Silver
            )

    )
    */
}
?>
<form method="POST">
    <input type="checkbox" name="city[]" value="Gold" />
    <input type="checkbox" name="city[]" value="Platinum" />
    <input type="checkbox" name="city[]" value="Silver" />
    <input type="submit" value="Submit" />
</form>

Try this.

<?php
    include("config.php");
    if($_POST) {
        $city = $_POST['card'];
            header("location:target_page.php?card=".$city);
    }
?>
<form method="POST">
    <input type="checkbox" name="card[]" id="card" value="Gold" />
    <input type="checkbox" name="card[]" id="card" value="Platinum" />
    <input type="checkbox" name="card[]" id="card" value="Silver" />
    <input type="submit" value="Submit" />
</form>

This is not working when both fields have the same name.

<form method="POST">
    <input type="checkbox" name="city" id="city" value="Gold" />
         <input type="checkbox" name="cities[]" id="city" value="Platinum" />
         <input type="checkbox" name="cities[]" id="city2" value="Silver" />
    <input type="submit" value="Submit" />
</form>

When you write the fieldname like cities[] then you get an array in your PHP-Script.

forearch($_POST['cities'] as $city) {
    var_dump($city);
}

And an ID should be unique.

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