【问题标题】:Radio Button INPUT not sending the input to the PHP code单选按钮输入未将输入发送到 PHP 代码
【发布时间】:2015-08-29 04:15:49
【问题描述】:

下面给出的代码显示了错误:

<div class="field form-inline radio">
<form method="post" action="">
<div>
<label><input type="radio" name="eatable" value="fruit_in"/> Fruit</label>
</div>  
<div>
<label><input type="radio" name="eatable" value="vegetable_in"/> Vegetable</label>
</div>  
<div>
<label><input type="radio" name="eatable" value="bread_in"/> Bread</label>
</div>  
<div>
<label><input type="radio" name="eatable" value="milk_in"/> Milk</label>
</div>
</form> <?php 
$veg = $_POST['eatable']?>

谁能告诉代码中的问题是什么? 错误说: 注意:未定义索引:可在 C:\xampp\htdocs\k\upload.php 中的第 250 行食用

【问题讨论】:

  • 这些都在同一个文件中吗?

标签: php forms post radio-button


【解决方案1】:

注意:未定义索引是由于$_POST没有索引eatable造成的,因为您的表单尚未发布。

你可以这样做:

<?php if(isset($_POST['eatable'])){
$veg = $_POST['eatable'];
} ?>

【讨论】:

  • 那是写问题时的错误(格式)。您更正的是实际代码。这不起作用
【解决方案2】:

如果您正在寻找要发送到“提交”表单上的 php 代码的值,请使用

$_POST['可食用'];

否则你可以在点击事件上使用“ajax”

【讨论】:

    【解决方案3】:
    <form method="post" action="">
        <div>
            <label><input type="radio" name="eatable" value="fruit_in"/> Fruit</label>
        </div>
        <div>
            <label><input type="radio" name="eatable" value="vegetable_in"/> Vegetable</label>
        </div>
        <div>
            <label><input type="radio" name="eatable" value="bread_in"/> Bread</label>
        </div>
        <div>
            <label><input type="radio" name="eatable" value="milk_in"/> Milk</label>
        </div>
        <input type="submit" name="submit" value="Submit">
    </form>
    
    <?php
    if(isset($_POST['submit']))
    {
        //php code goes here.
        //this will only run when only submit button clicked.
        $veg = $_POST['eatable'];
    }
    

    【讨论】:

      【解决方案4】:

      问题仅仅是因为在您打开文件时尚未定义“可食用”。 'eatable' 只会在您提交表单后定义,因此请将其更改为..

      <?php 
         if(isset($_POST['eatable'])){
           $veg = $_POST['eatable']
         }
      ?>
      

      同样在您的表单上,您需要添加一个提交按钮。

      【讨论】:

        【解决方案5】:

        修改旅游代码如..

        <form method="post" action="" name="items"><div><label><input type="radio" name="eatable" value="fruit_in" <?php if($_POST[eatable=="fruit_in"]) { ?> checked="checked" <?php } ?> /> Fruit</label>
        </div>  
        <div>
        <label><input type="radio" name="eatable" value="vegetable_in" <?php if($_POST[eatable=="vegetable_in"]) { ?> checked="checked" <?php } ?> /> Vegetable</label>
        </div>  
        <div>
        <label><input type="radio" name="eatable" value="bread_in" <?php if($_POST[eatable=="bread_in"]) { ?> checked="checked" <?php } ?> /> Bread</label>
        </div>  
        <div>
        <label><input type="radio" name="eatable" value="milk_in" <?php if($_POST[eatable=="milk_in"]) { ?> checked="checked" <?php } ?> /> Milk</label>
        </div>
        </form>
        

        【讨论】:

          猜你喜欢
          • 2021-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-11
          • 2012-01-26
          • 2016-06-04
          • 1970-01-01
          相关资源
          最近更新 更多