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 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决