简单的无线电html表格通过提交时传递PHP变量



我需要能够使用无线电选项使用HTML表单发送三个变量。

因此,根据选择哪个无线电选项,它需要发布相关的三个变量,其中一个变量是一个数组。该框架允许仅使用三个变量,其中一个必须为$ option(array(

<?php   $onx = 10;
    $onx_eta = "1 day";
    $onx_aw = 3;
    $bud = 8;
    $bud_eta = "2 days";
    $bud_aw = 2;
    $ecc = 6;
    $ecc_eta = "3 days";
    $ecc_aw = 1;
    $qty = 1;
    //$option = array($onx_eta, $onx_aw) //if ONX option is selected
    //$option = array($bud_eta, $bud_aw) //if BUDAIR option selected
    //$option = array($ecc_eta, $ecc_aw) //if ECC option is selected
    ?>
    <h2>HTML Forms</h2>
    <h2>Radio Buttons</h2>
    <form action="" method="post">
        <input type="radio" name="service" value="onx"> ONX
        <br>
        <!-- if this option is selected it needs to post $onx, $qty, $option -->
        <input type="radio" name="service" value="bud"> BUDAIR
        <br>
        <!-- if this option is selected it needs to post $bud, $qty, $option -->
        <input type="radio" name="service" value="ecc"> ECC
        <br>
        <!-- if this option is selected it needs to post $ecc, $qty, $option -->
        <input type="submit" value="Submit">
    </form>
    <p>Select which service you require</p>

Welcome <?php echo $_POST["service"]; ?><br>

然后,我希望一旦提交表单后,我希望能够检索三个变量。我看到的问题是无线电选项必须使用相同的"名称="。

,如果您添加类似的内容,则可以在提交表格时发送一些变量:

<form method="POST" action="abc.php">
    <input type="hidden" name="myhiddenvar" value="<?php echo 'some_value'; ?>" />
    <input type="submit" value="Send" />
</form>

我在PHP&amp;HTML代码。我以JSON格式提交了数据,该数据将在服务器端解码。

<?php   
    $qty = 1;
    $service_onx = [
        'service' => 'onx',
        'onx' => 10,
        'qty' => $qty,
        'option' => [
            'onx_eta' => '1 day',
            'onx_aw' => '3'
        ]
    ];
    $service_bud = [
        'service' => 'bud',
        'bud' => 8,
        'qty' => $qty,
        'option' => [
            'bud_eta' => '2 days',
            'bud_aw' => '2'
        ]
    ];
    $service_ecc = [
        'service' => 'ecc',
        'onx' => 6,
        'qty' => $qty,
        'option' => [
            'ecc_eta' => '3 days',
            'ecc_aw' => '1'
        ]
    ];
?>
<h2>HTML Forms</h2>
<h2>Radio Buttons</h2>
<form action="" method="post">
    <input type="radio" name="service" value="<?php echo htmlentities(json_encode($service_onx));?>"> ONX
    <br>
    <!-- if this option is selected it needs to post $onx, $qty, $option -->
    <input type="radio" name="service" value="<?php echo htmlentities(json_encode($service_bud));?>"> BUDAIR
    <br>
    <!-- if this option is selected it needs to post $bud, $qty, $option -->
    <input type="radio" name="service" value="<?php echo htmlentities(json_encode($service_ecc));?>"> ECC
    <br>
    <!-- if this option is selected it needs to post $ecc, $qty, $option -->
    <input type="submit" value="Submit">
</form>
<p>Select which service you require</p>

服务器端代码在表单提交后

if(!empty($_POST['service'])) {
    $service = json_decode($_POST['service'], true);
    switch($service['service']) {
        case 'onx':
            // ...
            break;
        case 'bud':
            // ...
            break;
        case 'ecc':
            // ...
            break;
        default:
            // invaid service
            break;
    }
}

最新更新