简单的Radio HTML表单,在提交时传递PHP变量

时间:2019-07-08 08:00:58

标签: javascript php html

我需要能够使用使用radio选项的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>

然后,我希望能够在提交表单后检索三个变量。我看到的问题是单选选项必须使用相同的“ name =”。

2 个答案:

答案 0 :(得分:1)

如果您只添加以下内容,则可以在提交表单时发送一些变量:

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

答案 1 :(得分:0)

我在php和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;
    }
}