Hi, I have a view that displays a table from my database to the users. I added a search option so that the user can search this table. I’m totally new to this and I don’t know why nothing happens when I click the search button.
This is my code, I’m just trying to echo “ok” as a test:
Views/Supermarkets/index.php:
<script>
function myFunction()
{
$.ajax({
url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
type: 'post',
data: {searchname: $("#searchname").val()},
success: function (data) {
alert(data);
}
});
}
</script>
?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use app\views\supermarkets\search;
?>
<h1>Supermarkets</h1>
<ul>
<p>
Search by Name
</p>
<input type="text" value ="" name="searchname", id="searchname">
<button onclick="myFunction()">Search</button>
<h3> </h3>
<?php
$array = (array) $supermarkets;
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]) ?>
Controllers/SupermarketsController.php:
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Supermarkets;
use yii\data\ActiveDataProvider;
class SupermarketsController extends Controller
{
public function actionIndex()
{
$query = supermarkets::find();
$pagination = new Pagination([
'defaultPageSize' => 20,
'totalCount' => $query->count(),
]);
$supermarkets = $query->orderBy('Name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
$dataProvider=new ActiveDataProvider('Supermarkets', array(
'pagination'=>array(
'pageSize'=>20,
),
));
return $this->render('index', [
'dataProvider'=>$dataProvider,
'supermarkets' => $supermarkets,
'pagination' => $pagination,
]);
}
public function actionSample(){
echo "ok";
}
In the debug I noticed that actionSample isn’t being run. Could you please help me get through this?