Yii提交后如何打开另一个视图
How to open another view after submit in Yii
我有一个搜索提交按钮,我希望我的应用程序在单击时打开另一个视图。这是我的代码:
views/supermarkets/index.php:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use app\views\supermarkets\search;
?>
<h1>Supermarkets</h1>
<ul>
<p>
Search by Name
</p>
<Form Name ="form1" Method ="POST" ACTION = "app\views\supermarkets\search.php">
<INPUT TYPE = "Text" VALUE ="search by name" NAME = "searchname">
<input type="submit" value="Search">
<h3> </h3>
views/supermarkets/search.php:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
if (isset($_POST['searchname']) && $_POST['searchname']!='') {
$sname = $_POST['searchname'];
$row = Yii::app()->db->createCommand(array(
'select' => '*',
'from' => 'supermarkets',
'where' => array('like', 'Name','%'.$keyword.'')
))->queryRow();
$array = (array) $row;
}
echo $array;
function build_table($array){
// start table
$html = '<table class="altrowstable" id="alternatecolor">';
// header row
$html .= '<tr>';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
echo build_table($array);
?>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
提交时出现此错误:
找不到对象!
在此服务器上找不到请求的 URL。引用页面上的 link 似乎有误或已过时。请将该错误告知该页面的作者。
如果您认为这是服务器错误,请联系站长。
我知道我的代码有问题,但我就是想不通。而且我不确定这是否是将 POST 值提交到我的搜索视图的正确方法。
您的问题源于此行:
<Form Name ="form1" Method ="POST" ACTION = "app\views\supermarkets\search.php">
操作必须是可调用的 URL。相反,您提供了视图文件的路径。
使用 Yii MVC 约定,您需要生成控制器函数并将表单操作指向该路径。
在您看来:
< form name ="form1" method ="POST" action="<?php Yii::createUrl('supermarket/search'); ?>" >
在你的控制器中:
class SupermarketController extends CController
{
public function actionSearch()
{
// ... Your code here.
$this->render('another_view', array(...);
}
}
你不应该在你的项目中使用绝对路径。这是我在 Yii1 中使用的方法列表。希望它能在 Yii2 中工作。
创建URL
$url = Yii::app()->createUrl('site/index', array('id'=>100));
从控制器创建URL
Yii::app()->controller->createUrl("index", array("id"=>100));
创造绝对URL
Yii::app()->createAbsoluteUrl('site/index',array('id'=>100));
创建Link
echo CHtml::link('text', array('site/index', 'id'=>100));
重定向浏览器
$this->redirect(array('site/index','id'=>100));
删除没有值的数组('id'=>100)
用户无法直接访问 "protected" 文件夹中的查看文件。
工匠的回答是正确的。
see here 有关控制器的信息
我有一个搜索提交按钮,我希望我的应用程序在单击时打开另一个视图。这是我的代码:
views/supermarkets/index.php:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use app\views\supermarkets\search;
?>
<h1>Supermarkets</h1>
<ul>
<p>
Search by Name
</p>
<Form Name ="form1" Method ="POST" ACTION = "app\views\supermarkets\search.php">
<INPUT TYPE = "Text" VALUE ="search by name" NAME = "searchname">
<input type="submit" value="Search">
<h3> </h3>
views/supermarkets/search.php:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
if (isset($_POST['searchname']) && $_POST['searchname']!='') {
$sname = $_POST['searchname'];
$row = Yii::app()->db->createCommand(array(
'select' => '*',
'from' => 'supermarkets',
'where' => array('like', 'Name','%'.$keyword.'')
))->queryRow();
$array = (array) $row;
}
echo $array;
function build_table($array){
// start table
$html = '<table class="altrowstable" id="alternatecolor">';
// header row
$html .= '<tr>';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
echo build_table($array);
?>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
提交时出现此错误:
找不到对象!
在此服务器上找不到请求的 URL。引用页面上的 link 似乎有误或已过时。请将该错误告知该页面的作者。
如果您认为这是服务器错误,请联系站长。
我知道我的代码有问题,但我就是想不通。而且我不确定这是否是将 POST 值提交到我的搜索视图的正确方法。
您的问题源于此行:
<Form Name ="form1" Method ="POST" ACTION = "app\views\supermarkets\search.php">
操作必须是可调用的 URL。相反,您提供了视图文件的路径。
使用 Yii MVC 约定,您需要生成控制器函数并将表单操作指向该路径。
在您看来:
< form name ="form1" method ="POST" action="<?php Yii::createUrl('supermarket/search'); ?>" >
在你的控制器中:
class SupermarketController extends CController
{
public function actionSearch()
{
// ... Your code here.
$this->render('another_view', array(...);
}
}
你不应该在你的项目中使用绝对路径。这是我在 Yii1 中使用的方法列表。希望它能在 Yii2 中工作。
创建URL
$url = Yii::app()->createUrl('site/index', array('id'=>100));
从控制器创建URL
Yii::app()->controller->createUrl("index", array("id"=>100));
创造绝对URL
Yii::app()->createAbsoluteUrl('site/index',array('id'=>100));
创建Link
echo CHtml::link('text', array('site/index', 'id'=>100));
重定向浏览器
$this->redirect(array('site/index','id'=>100));
删除没有值的数组('id'=>100)
用户无法直接访问 "protected" 文件夹中的查看文件。 工匠的回答是正确的。 see here 有关控制器的信息