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

将数据从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;
    }

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'];