[英]Passing data from HTML form to PHP in controller
我一直試圖做的是將數據從我視圖中選定的單選按鈕傳遞到控制器中的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;
}
您對這對單選按鈕使用了不同的名稱。 用戶將能夠選擇兩個單選按鈕。
<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'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.