user3640056
user3640056

Reputation: 732

How to open another view after submit in Yii

I have a search submit button, and I want my application to open another view on click. This is my code:

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]) ?>

On Submit I'm getting this error:

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

I know there is something wrong in my code, but I just can't figure it out. And I am not sure if this is the right way to submit the POST value to my search view.

Upvotes: 1

Views: 909

Answers (3)

yanis
yanis

Reputation: 53

View files inside the "protected" folder are not accessible directly by user. crafter's answer is correct. see here for information about controller

Upvotes: 1

Ramesh Murugesan
Ramesh Murugesan

Reputation: 5013

You should not use absolute path in your project. Here the list of methods I used in Yii1. Hope it will works in Yii2.

  1. Create URL

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

  2. Create URL from Controller

    Yii::app()->controller->createUrl("index", array("id"=>100));

  3. Create absolute URL

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

  4. Create Link

    echo CHtml::link('text', array('site/index', 'id'=>100));

  5. Redirect Browser

    $this->redirect(array('site/index','id'=>100));

Remove array('id'=>100) for without values

Upvotes: 1

crafter
crafter

Reputation: 6296

Your issue arises from this line :

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

The action must be a callable URL. Instead, you are providing a path to the view file.

Using the Yii MVC convention, you will need to generate a controller function and point the form action to that path.

In your view :

< form name ="form1" method ="POST" action="<?php Yii::createUrl('supermarket/search'); ?>" >

In your controller:

    class SupermarketController extends CController
    {
        public function actionSearch()
        {
            // ... Your code here.
            $this->render('another_view', array(...);
        } 
}

Upvotes: 1

Related Questions