doucheng5209 2014-08-20 13:34
浏览 57
已采纳

将数据从HTML表单传递到控制器中的PHP

WHat I have been trying to do is pass the data from the selected radio button in my view, to the php in the controller. I then add the existing token balance to the selected amount and submit that back into the database.

Problem is I can't get the values from the html radio button to the php controller/action. I am using the latest version of php and YII framework.

The view

$this->breadcrumbs = array(
'Tokens' => array('index'),
$model->TokenID => 
'buy',
);

?>

<h1>Buy Tokens 
</h1>
<?php
echo  'Your balance is ' .$model->TokenAmount;
?>

<FORM name ="form1" method ="post" action = "">

<Input type = 'Radio' Name ='10tokens' value= '10'

>10

<Input type = 'Radio' Name ='25tokens' value= '25' 

>25

<Input type = "Submit" Name = "Submit1" VALUE = "Purchase Tokens">

</FORM>

and the action in the controller

  public function actionBuy() {

    $_id = Yii::app()->user->getId();
    $model = Tokens::model()->findByAttributes(array('UserID' => $_id));
    if ($model === null)
        throw new CHttpException(404, "Keep calm! If you havent bought tokens before this is normal");

    $this->render('buy', array(
        'model' => $model,));

//        $qty = $_POST['form1'];
//        $newtkamount = ($_model->TokenAmount + $qty);
 //        echo $qty . $newtkamount . $_model->TokenAmount;
    }
  • 写回答

1条回答 默认 最新

  • dre26973 2014-08-20 13:41
    关注

    You are using different names for the pair of radio buttons. Users will be able to select both radio buttons.

    <Input type = 'Radio' Name ='10tokens' value= '10'>10
    <Input type = 'Radio' Name ='25tokens' value= '25'>25
    

    You should change both names to the same. Example:

    <input type='radio' name='qty' value='10' />10
    <input type='radio' name='qty' value='25' />25
    

    Then, you will be able to get the selected radio button value using:

    $qty = $_POST['qty'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决