Reputation: 125
I'm totally new to Yii and I need help please even if it looks trivial. I have a page where I generate a table from my database, I added a search option and I need to generate the result as a table also.
The problem is that when I click the button nothing happens. I also tried using submit but it didn't work.
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>
<INPUT TYPE = "Text" VALUE ="" NAME = "searchname">
<button onclick="myFunction($_POST['searchname'])">Search</button>
<h3> </h3>
<?php
$array = (array) $supermarkets;
function myFunction($sname){
if (isset($sname) && $sname!='') {
$row = Yii::app()->db->createCommand(array(
'select' => '*',
'from' => 'supermarkets',
'where' => array('like', 'Name','%'.$sname.'')
))->queryRow();
$array = (array) $row;
}
echo $array;
$this->render('index',array('supermarkets' => $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]) ?>
Even in the debug it doesn't pass through myFunction. Any suggestions please?
Upvotes: 1
Views: 6470
Reputation: 198
You should follow proper Yii syntax, ie define your function inside controller and call that function by creating button on index.php or other view file like:
Button on view file:
<?= Html::a('Search', ['search-function', 'Name' => $this->$sname], ['class' => 'btn btn-success']) ?>
And in your controller:
//As you define your function
public function actionSearchFunction($sname){
if (isset($sname) && $sname!='') {
$row = Yii::app()->db->createCommand(array(
'select' => '*',
'from' => 'supermarkets',
'where' => array('like', 'Name','%'.$sname.'')
))->queryAll();
$array = (array) $row;
Upvotes: 1
Reputation: 8033
I will explain you step by step. First, Note that everything inside <?php ?>
tag will be generate at the server BEFORE rendering of the view. So, you can't execute php code on the client. If you need to execute some server code from client, You need to use Ajax(asynchronous JavaScript and XML). I recommend you to learn ajax, because this is very useful in web applications. OK, let's solve your problem. First step is to define an event for button click. You have myFunction
already and I change it to this:
<button onclick="myFunction();">Search</button>
And I change your input to this:
<input type="text" value ="" name="searchname", id="searchname">
Now, I write myFunction()
body in the <script></script>
tag:
<script>
function myFunction()
{
$.ajax({
url: '<?php echo Yii::app()->baseUrl . '/supermarkets/sample' ?>',
type: 'post',
data: {searchname: $("#searchname").val()},
success: function (data) {
alert(data);
}
});
}
</script>
Now, you need to define an action in your controller for ajax request. I supposed you have SupermarketsController
. Try to write actionSample()
in it like this:
public function actionSample()
{
$array = (array) $supermarkets;
$sname = $_POST['searchname'];
...
echo "ok";
}
You can access the search input value in that function and all of your php code must be there. at the last line of that action, you must echo something(data or message). Everything you put after echo, you can get it as data
parameter of success
event of ajax request. In my example, you will see "ok" alert in your browser. I hope my explanation can help you to learn ajax. Note you need to include jquery library for using ajax. Good luck friend :)
Upvotes: 0