Php 如何在Yii中提交后打开另一个视图

Php 如何在Yii中提交后打开另一个视图,php,html,yii,submit,Php,Html,Yii,Submit,我有一个搜索提交按钮,我想我的应用程序打开另一个点击查看。这是我的代码: views/supermarkes/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&

我有一个搜索提交按钮,我想我的应用程序打开另一个点击查看。这是我的代码:

views/supermarkes/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/supermarkes/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。参考页面上的链接似乎错误或过时。请将错误告知该页的作者

如果您认为这是服务器错误,请联系网站管理员


我知道我的代码有问题,但我就是想不出来。我不确定这是否是将帖子值提交到我的搜索视图的正确方式。

您的问题来自以下行:

<Form Name ="form1" Method ="POST" ACTION = "app\views\supermarkets\search.php">

您不应该在项目中使用绝对路径。这里是我在Yii1中使用的方法列表。希望它能在Yii2中起作用

  • 创建URL

    $url=Yii::app()->createUrl('site/index',array('id'=>100))

  • 从控制器创建URL

    Yii::app()->controller->createUrl(“索引”,数组(“id”=>100))

  • 创建绝对URL

    Yii::app()->createAbsoluteUrl('site/index',array('id'=>100))

  • 创建链接

    链接('text',数组('site/index','id'=>100))

  • 重定向浏览器

    $this->redirect(数组('site/index','id'=>100))

  • 删除数组('id'=>100),因为用户无法直接访问“受保护”文件夹中的无值视图文件。 克拉夫特的回答是正确的。
    有关控制器的信息

    我认为您做错了什么。应用程序的视图不得包含与数据库交互的逻辑。您需要在控制器中创建操作,从中可以获得POST参数并使用数据呈现视图。
    < 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(...);
            } 
    }