【问题标题】:Passing data from HTML form to PHP in controller在控制器中将数据从 HTML 表单传递到 PHP
【发布时间】:2014-08-20 13:34:20
【问题描述】:

我一直在尝试做的是将数据从我视图中选定的单选按钮传递到控制器中的 php。然后,我将现有的代币余额添加到选定的金额并将其提交回数据库。

问题是我无法从 html 单选按钮获取值到 php 控制器/操作。 我正在使用最新版本的 php 和 YII 框架。

景色

$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>

和控制器中的动作

  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;
    }

【问题讨论】:

  • 即不要在标签和属性名称中使用大写字母,尽量将标签放在一行中,不要在标签中使用空的新行...
  • 我已经整理好了,html 将在稍后阶段正确完成这只是为了展示它与后端的东西一起工作

标签: php html forms yii controller


【解决方案1】:

您为这对单选按钮使用了不同的名称。用户将能够同时选择这两个单选按钮。

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

您应该将两个名称更改为相同的名称。示例:

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

然后,您将能够使用以下方法获取选定的单选按钮值:

$qty = $_POST['qty'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2018-06-28
    • 2014-12-02
    • 1970-01-01
    • 2021-12-11
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多