2

ボタンのクリック時に Yii で html ボタンを作成しようとしています。 UserController.php と Actioncreate を呼び出したいと思います。

私はこのようにやっています、

 echo CHtml::button('Signup', array('button' => array('Users/create'))); ?>

しかし、それは私に次のエラーを与えています

htmlspecialchars() expects parameter 1 to be string, array given 

これはスタックトレースです

D:\wamp\www\yii\framework\web\helpers\CHtml.php(85)

73      */
74     public static $liveEvents = true;
75 
76     /**
77      * Encodes special characters into HTML entities.
78      * The {@link CApplication::charset application charset} will be used for encoding.
79      * @param string $text data to be encoded
80      * @return string the encoded data
81      * @see http://www.php.net/manual/en/function.htmlspecialchars.php
82      */
83     public static function encode($text)
84     {
85         return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
86     }
87 
88     /**
89      * Decodes special HTML entities back to the corresponding characters.
90      * This is the opposite of {@link encode()}.
91      * @param string $text data to be decoded
92      * @return string the decoded data
93      * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
94      * @since 1.1.8
95      */
96     public static function decode($text)
97     {

Stack Trace
#0  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(85): htmlspecialchars(array("Users/create"), 3, "UTF-8")
#1  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(2216): CHtml::encode(array("Users/create"))
#2  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(140): CHtml::renderAttributes(array("button" => array("Users/create"), "name" => "yt1", "type" => "button", "value" => "Signup"))
#3  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(436): CHtml::tag("input", array("button" => array("Users/create"), "name" => "yt1", "type" => "button", "value" => "Signup"))
#4  
–
 D:\wamp\www\nurseweb\protected\views\site\login.php(50): CHtml::button("Signup", array("button" => array("Users/create")))

45         <?php echo $form->error($model,'rememberMe'); ?>
46     </div>
47 
48     <div class="row buttons">
49         <?php echo CHtml::submitButton('Login');  
50  echo CHtml::button('Signup', array('button' => array('Users/create'))); ?>
51 <?php $this->endWidget(); ?>
52 </div><!-- form -->

#5  
+
 D:\wamp\www\yii\framework\web\CBaseController.php(127): require("D:\wamp\www\nurseweb\protected\views\site\login.php")
#6  
+
 D:\wamp\www\yii\framework\web\CBaseController.php(96): CBaseController->renderInternal("D:\wamp\www\nurseweb\protected\views\site\login.php", array("model" => LoginForm), true)
#7  
+
 D:\wamp\www\yii\framework\web\CController.php(870): CBaseController->renderFile("D:\wamp\www\nurseweb\protected\views\site\login.php", array("model" => LoginForm), true)
#8  
+
 D:\wamp\www\yii\framework\web\CController.php(783): CController->renderPartial("login", array("model" => LoginForm), true)
#9  
–
 D:\wamp\www\nurseweb\protected\controllers\SiteController.php(98): CController->render("login", array("model" => LoginForm))

093             // validate user input and redirect to the previous page if valid
094             if($model->validate() && $model->login())
095                 $this->redirect(Yii::app()->user->returnUrl);
096         }
097         // display the login form
098         $this->render('login',array('model'=>$model));
099     }
100 
101     /**
102      * Logs out the current user and redirect to homepage.
103      */

#10     
+
 D:\wamp\www\yii\framework\web\actions\CInlineAction.php(50): SiteController->actionLogin()
#11     
+
 D:\wamp\www\yii\framework\web\CController.php(309): CInlineAction->runWithParams(array("r" => "site/login"))
#12     
+
 D:\wamp\www\yii\framework\web\CController.php(287): CController->runAction(CInlineAction)
#13     
+
 D:\wamp\www\yii\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array())
#14     
+
 D:\wamp\www\yii\framework\web\CWebApplication.php(283): CController->run("login")
#15     
+
 D:\wamp\www\yii\framework\web\CWebApplication.php(142): CWebApplication->runController("site/login")
#16     
+
 D:\wamp\www\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()
#17     
–
 D:\wamp\www\nurseweb\index.php(13): CApplication->run()

08 defined('YII_DEBUG') or define('YII_DEBUG',true);
09 // specify how many levels of call stack should be shown in each log message
10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
11 
12 require_once($yii);
13 Yii::createWebApplication($config)->run();

このエラーの意味を教えて、解決策を教えてもらえますか?

4

1 に答える 1

3

CHtml button() メソッドは文字通り単なるボタンです。それが受け入れる 2 番目のパラメーターは、配列の配列ではなく、キーと値のペアの配列である必要があります (あなたの例のように) 詳細についてはCHtml::button()を参照してください情報。このメソッドを使用する方法は、たとえば次のようになります。

echo CHtml::button('myButton',array(
    'class' => 'buttonClass',
    'id'    => 'buttonId',
));

これは次のような出力になります。

<input class="buttonClass" id="buttonId" name="yt1" type="button" value="myButton">

ボタンでフォームを送信する場合は、CHtml::submitButton()を使用して、ボタンが含まれているフォームを送信します。

ボタンが実際に何も送信せずにユーザーを別のページにリダイレクトするようにしたい場合は、アンカーリンクのある画像を使用するか、ボタンが押されたらjQueryを使用してユーザーをリダイレクトするのが最善の解決策だと思います.

その場合は、次の質問といくつかの回答を参考にしてください: jQuery または Javascript のみを使用してボタンを別のページにリダイレクトする方法

于 2012-10-02T20:09:09.750 に答える