Facing Issues With Ajax Update On Page Load - php

I'm having troubles getting content displayed on page load using ajax. The ajax is calling the right action in the respective controller. The first part of the action code where i update the database is working fine. But the part where i'm calling renderPartial is not working.
**EDIT***
Ok here is the controller action ::
public function actionUpdateProductData() {
Yii::import('application.components.DataScraper.*');
require_once('GetProductData.php');
$productRealTime = RealTime::model()->findAll();
if (count($productRealTime) === 0) {
$symbolData = new GetProductData();
$symbolData->getAmazonProductData();
} else {
echo CJSON::encode( array(
'status' => 'OK',
'div' => $this->renderPartial('_productDataGrid', array(
'model' => $productRealTime),
true, true ),
));
}
}
The if part is working fine. But the else portion is not working.
Here is the view index.php::
<?php
/*
* Include the ajax Stock update script
*
*/
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl . '/js/ajaxProductDataUpdate.js');
?>
<div>
<hr>
<ul class="breadcrumb">
<li>
Home <span class="divider">/</span>
</li>
<li>
Product Info
</li>
</ul>
<hr>
</div>
<div class="span-10">
<div id="section2">
</div>
</div>
Here is the partial view file _productDataGrid.php
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'real-time-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'category',
'price'
'rating'
array(
'class' => 'bootstrap.widgets.TbButtonColumn',
),
),
));
?>
And here is the jQuery file which is making the ajax request
var productParameters = {
ajaxUpdate: function() {
$.ajax({
url: "/ProductAnalysis/index.php/realTime/updateProductData",
type: "GET",
dataType:"json",
error: function(xhr, tStatus, e) {
if (!xhr) {
alert(" We have an error ");
alert(tStatus + " " + e.message);
} else {
alert("else: " + e.message); // the great unknown
}
},
success: function(data) {
$.fn.yiiGridView.update('real-time-grid', {
data: $(this).serialize()
});
}
});
}
};
$(document).ready(function() {
productParameters.ajaxUpdate();
});
Upon loading the page /realTime/index.php i'm getting an error which says
else:
undefined
Obviously the ajax call is failing, but i don't know how will i fix it. Also in Firebug, the echo date() function in the controller is working, but the Gridview is not working.
Please provide some help on how to solve this. Let me know where i'm doing wrong. I can't seem to make any headway around this.
Thanks in advance,
Maxx

Your actionUpdateStockData() is echoing the date before the actual JSON content is encoded. As a result you're not transmitting correct JSON, and XHR will fail.
Remove the echo date ... line and you should be fine. And as you're just at it - you should add some response for the case where count(RealTime::model()->findAll()) === 0.

Well it seems that the gridview widget won't work with findall(). So i changed the dataprovider to simple model and it works now.
Here is the working code ::
public function actionUpdateStockData() {
Yii::import('application.components.DataScraper.*');
require_once('GetStockData.php');
$productRealTime = new RealTime();
if (count($productRealTime->model()->findAll()) === 0) {
$symbolData = new GetproductData();
$symbolData->getAmazonProductData();
} else {
echo CJSON::encode( array(
'status' => 'success',
'div' => $this->renderPartial('_productDataGrid', array(
'model' => $productRealTime),
true, true ),
));
}
}

Related

Problem updating dynamically ActiveForm values with Ajax + RenderPartial/Ajax

Solution
Since the php is not going to be re-executed someone told me to use a return value from the controller as a parameter of the "success" function into ajax's request.
**CONTROLLER**
if(Yii::$app->request->isAjax){
$query = new \yii\db\Query();
$rows = $query->select([])
->from('task')
->where('date(start) <= date(\'' . $this->request->getBodyParam('start_date') . '\')')
->all();
$items = "";
foreach (ArrayHelper::map($rows,'id','name') as $key => $value)
$items .= '<option value="'. $key .'">'. $value .'</option>\n';
//<option value="16">test</option> example output
echo $items;
}
VIEW
$this->registerJs("$('#task-start').on('change',function(){
$(\".btn, .btn-success\").prop(\"disabled\",true);
var date = $(this).val();
$.ajax({
url: \"".Yii::$app->request->baseUrl."/task/create\",
data: {start_date: date},
type: \"post\",
success: function(dependency_options){
//dependency_options contains what's returned with 'echo' from the controller
$('#task-dependencies').find('option:not(:first)').remove();
$('#task-dependencies').append(dependency_options);
$(\".btn, .btn-success\").prop(\"disabled\",false);
},
error: function () {
console.log('ERROR')
$(\".btn, .btn-success\").prop(\"disabled\",false);
}
});
});",View::POS_READY);
Hoping this might help someone.
My problem
I have a DropDownList with no elements and I want to update the list with elements coming from a Query whenever the user changes the datepicker without refreshing the page.
An example of something similar to this would be those forms in which you type your region and it changes the dropdownlist based on what you choose on the field before.
Maybe I'm using a wrong approach to this cause I'm still new to the MVC model and the Yii2 framework so any idea on how to change it's well appreciated.
What I've tried
With this code below as is I had issues cause after the form was created the first time I could not change it later, I've tried to change the html, as you can see from the script in the success ajax function of the View but the script was executed only the first time the view was loaded.
Controller calling the render
public function actionCreate()
{
$model = new Task();
if(Yii::$app->request->isAjax){
$query = new \yii\db\Query();
$rows = $query->select([])
->from('task')
->where('date(start) <= date(\'' . $this->request->getBodyParam('start_date') . '\')')
->all();
$items = ArrayHelper::map($rows,'id','name');
$model->setItems($items);
return $this->renderPartial('_form',[
'partial' => true,
'model' => $model
]);
}
else if ($this->request->isPost) {
..unnecessary code..
return $this->render('create', [
'model' => $model,
]);
}
The view "create" basically renders the view _form (autogenerated by Gii)
View _form
<?php
//Here it takes the items in the model
//(which will contain the new items to append after ajax call)
$objects = json_encode($model->getItems() ?? []);
var_dump($model->getItems() ?? []);
//Here whenever the datepicker is change will fire ajax request
$this->registerJs("$('#task-start').on('change',function(){
var date = $(this).val();
var items = ". json_encode($model->getItems() ?? []) .";
alert(items);
$.ajax({
url: \"".Yii::$app->request->baseUrl."/task/create\",
data: {start_date: date},
type: \"post\",
success: function(){
//$('#task-dependencies').find('option:not(:first)').remove();
$.each(items, function(key, value) {
$('#task-dependencies')
.append($('<option>', { value : key })
.text(value));
});
},
error: function () {
console.log('ERROR')
}
});
});",View::POS_READY);
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'start')->widget(\kartik\date\DatePicker::className(), [
...datepicker that triggers onChange...
]) ?>
//The dropdownlist that should dynamically change
<?= $form->field($model, 'dependencies')->dropDownList(
$model->getItems(),
['prompt' => 'Seleziona una o piĆ¹ dipendenze', 'multiple' => true, 'selected' => 'selected'] // options
);
?>
<?php ActiveForm::end(); ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery#3.6.0/dist/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
You can use Pjax in, yii2:
Controller:
public function actionCreate() {
$model = new Task();
if( Yii::$app->request->post() && Yii::$app->request->isPjax ) {
$query = new \yii\db\Query();
$rows = $query->select([])
->from('task')
->where('date(start) <= date(\'' . $this->request->getBodyParam('start_date') . '\')')
->all();
$items = ArrayHelper::map($rows,'id','name');
$model->setItems($items);
// Alert widget renders a message from session flash.
// Yii::$app->session->setFlash('info', "Ok....");
return $this->renderAjax('_form',[ // Or renderPartial
'partial' => true,
'model' => $model
]);
}
// else if ($this->request->isPost) {
// ..unnecessary code..
return $this->render('create', [
'model' => $model,
]);
}
}
view
// submitEvent: The jQuery event that will trigger form handler. Defaults to "submit".
<?php Pjax::begin(['submitEvent' => 'change']); ?>
<?php $form = ActiveForm::begin([
'options' => ['data-pjax' => ''],
]);
// if(hasFlash ....){ // If used ...
// Yii::$app->session->getFlash('info')
// }
?>
<?= $form->field($model, 'start')->widget......
<?= $form->field($model, 'dependencies')->dropDownList(
$model->getItems(),
// ......
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
Tip: The above codes will meet your goal. But you can use the following code to change more and respond to Pjax events.
jQuery or JavaScript...
$(document).on('pjax:send', function() {
// code ... Example: $('#loading').css({"visibility":"visible"}); //show()
});
$(document).on('pjax:complete', function() {
// code ... // hide()
});
// Or use if Pjax::$submitEvent = 'submit'
// $(document).on('change', '#id', function(event) { $(this).submit(); });
If you still have problems, check the date format method in SQL and ....
Good luck.

Dropdownlist is not updating using Yii

I'm using
PHP language , yii-1.1.13 framework and MySQL DB.
In my Views , I have this code:
Views Code of Main Page
/** Start Widget **/
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'dialog',
'options' => array(
'title' => 'Locations Management',
'autoOpen' => false,
'modal' => true,
'resizable' => false,
'dialogClass' => 'managelocation-dialog-class',
'show'=>array(
'effect'=>'drop',
'duration'=>500,
),
'hide'=>array(
'effect'=>'drop',
'duration'=>500,
),
),
));
/**
* Render dialog view.
*/
echo $this->renderPartial('manageLocationDialog', array(
'model' => $model,
'locationInfo' => $locationInfo,
));
$this->endWidget('zii.widgets.jui.CJuiDialog');
/**
* Filter Dialog widget
*/
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'filter-dialog',
'options' => array(
'title' => 'Filter',
'autoOpen' => false,
'modal' => true,
'resizable' => false,
'width' => 350,
'dialogClass' => 'location-dialog-class',
'show'=>array(
'effect'=>'drop',
'duration'=>500,
),
'hide'=>array(
'effect'=>'drop',
'duration'=>500,
),
),
));
/**
* Render the filter dialog view.
*/
echo $this->renderPartial('manageLocationFilter', array(
'filterFormloc' => $filterFormloc,
'locationInfo' => $locationInfo,
));
$this->endWidget('zii.widgets.jui.CJuiDialog');?>
Views Code of Add/Edit Dialog
<div id="action-button-div" class="row">
<?php
echo CHtml::button('Create New', array(
'id'=>'action-button',
'class'=>'submit-button',
'onclick'=>"{submitActionJs();}",
'update' =>'#filter_province_name',
));
?>
<?php
echo CHtml::button('Cancel', array(
'id'=>'cancel-button',
'onclick'=>'{$("#dialog").dialog("close");}',
));
?>
</div>
Views Code of Filter Dialog
<div id="dialog-contents-container">
<div class="row">
<div id="filter-mode-div">
<?php
echo $form->labelEx($filterFormloc, 'filter_mode', array(
'label' => 'Filter Mode',
));
?>
<div>
<?php
echo $form->radioButtonList($filterFormloc, 'filter_mode', array(
1=>'ON',2=>'OFF'),array('id'=>'filter_mode'
));
?>
</div>
</div>
<div id="reset-button-div">
<?php
echo CHtml::button('Reset Settings', array(
'id'=>'reset-button',
'onclick'=>'{$(this.form).find("textarea, :text, select").val("").end().find(":checked").prop("checked", false);$("#ManageLocationFilterForm_filter_mode_1").attr("checked",true);}',
));
?>
</div>
</div>
<div id="under-container">
<div class="row">
<div id="province_name">
<?php
echo $form->labelEx($filterFormloc, 'province_name', array(
'label' => 'Province *',
));
?>
<div>
<?php
echo $form->dropDownList($filterFormloc, 'province_name',
$locationInfo->getAllProvincesForSelection(true, 'Select Province'),
array(
'id' => 'filter_province_name',
'class' => 'selectbox',
)
);
?>
</div>
</div>
</div>
</div>
<div id="action-button-div" class="row">
<?php
echo CHtml::button('Apply Filter Settings', array(
'id'=>'action-button_2',
'onclick'=>"{submitFilterActionJs();}"
));
?>
<?php
echo CHtml::button('Cancel', array(
'id'=>'cancel-button_2',
'onclick'=>'{$("#filter-dialog").dialog("close");}',
));
?>
</div>
</div>
In my controller, below is my code:
public function actionRegisterLocation() {
$model = new ManageLocationForm;
if (isset($_POST['ManageLocationForm']))
{
$model->attributes = $_POST['ManageLocationForm'];
if (Yii::app()->request->isAjaxRequest)
{
if ($model->hasErrors())
{
$errors = '';
foreach ($model->getErrors() as $e) $errors .= implode($e).'<br>';
echo CJSON::encode(array(
'status'=>'failure',
'messages'=>$errors
));
}
else
{
$locationInfo = new LocationInfo;
if ($model->operation_mode === AdminGeneralHelper::OPERATION_MODE_UPDATE)
{
$locationInfo=LocationInfo::model()->findByPk($model->location_id);
}
$locationInfo->short_name = $model->short_name;
$locationInfo->town_name = $model->town_name;
$locationInfo->province_name = $model->province_name;
$locationInfo->save();
if ($model->operation_mode === AdminGeneralHelper::OPERATION_MODE_CREATION)
$_message = 'Create operation completed.';
if ($model->operation_mode === AdminGeneralHelper::OPERATION_MODE_UPDATE)
$_message = 'Update operation completed.';
if ($locationInfo->hasErrors())
{
$errors = '';
foreach ($locationInfo->getErrors() as $e) $errors .= implode($e).'<br>';
echo CJSON::encode(array(
'status'=>'failure',
'messages'=>$errors
));
}
echo CJSON::encode(array(
'status' => 'success',
'messages' => $_message,
));
}
exit;
}
}
else
{
echo "FALSE";
}
Yii::app()->end();
}
Below is the scenario:
I open the dialog for Adding a location. (Parameters are short_name, town, and province)
Location was Successfully added in the Grid View and DB.
I open the dialog for filter. It can only filter by Province.
When I take a look at the Dropdown lists, Location that was successfully added is not on the lists.
My question is how to update the dropdown lists of filter dialog after I successfully added a location from Add/Edit Dialog. Alternative solution is I need to refresh the browser then open the filter dialog. But it is not that user friendly. Its really a bug.
update or replace requires the response for the query to be html (see the source for CHtml::ajax() for more detail). Your query returns json. You have several options:
You can change the controller to return all the options for the dropdown as html and continue using update.
You can change the controller to return the full dropdown as html and use replace instead of update.
You can change controller to return the id and value of the newly added location and use a custom javascript function to add this option to the dropdown.
I tried the Option No.3 and its working.
Below is the working code from my Javascript file.
function submitActionJs() {
var fareCat = document.getElementById("name").value;
var newFareCat = toTitleCase(fareCat);
$.ajax({
url: 'registerFareCategory',
type: 'POST',
datatype: 'json',
data: $('form').serializeArray(),
timeout: 10000,
beforeSend: function(){
$('#dialog-msg').html('Processing...');
},
success: function(data){
var res = eval('(' + data + ')');
$('#dialog-msg').html(res.messages);
if (res.status == 'success'){
$("#message-label").html(res.messages);
$.fn.yiiGridView.update('fare-category-grid');
$("#dialog").dialog("close");
window.parent.$('#filter_name').append('<option value = "' + newFareCat + '">' + newFareCat + '</option>');
//sort fare category dropdownlist from filter dialog
$("#filter_name").html($('#filter_name option').sort(function(x, y) {
return $(x).text().toUpperCase() < $(y).text().toUpperCase() ? -1 : 1;
}));
$("#filter_name").get(0).selectedIndex = 0;
e.preventDefault();
}
},
error: function(){
$('#dialog-msg').html('Ajax Communication Error.');
}
}
);
}
Thanks #topher for your suggestion.

Yii popup confirmation

I'm trying my best to alter the below code so that it produces a popup box with a warning and asking for confirmation.
echo CHtml::ajaxButton(Yii::t('mc', 'Wipe Server'), '', array(
'type'=>'POST', 'data'=>array('ajax'=>'wipe', Yii::app()->request->csrfTokenName=>Yii::app()->request->csrfToken,),
'success'=>'function(e) {if (e) alert(e);}'
),
I expect adding 'confirm' => 'Wipe your server?' to add a dialog box but I'm not having much success.
I have this in ServerController:
case 'wipe':
if (Yii::app()->user->can($id, 'wipe'))
{
if (!McBridge::get()->serverCmd($id, 'run:builtin:script wipe'))
echo McBridge::get()->lastError();
}
break;
I would be grateful if anyone can point out where I am going wrong or generally point me in the right direction.
Thank you
Try this one
In Yii ajax button, beforesend function is there. Use that.
Example
<?php
echo CHtml::ajaxButton(
'Submit',
array('controlleraction'),
array(
'success' => 'js:
function (data){
}
',
'type' => 'POST',
'beforeSend' => 'js:
function(){
var r = confirm("Are you sure?");
if(!r){return false;}
}
',
));
?>

Example of calling CakePHP function from jQuery

I am new to CakePHP and I am trying to figure you how to make an asynchronous call from a CakePHP view to a function in the controller. I would like the controller function to return a string and have the view display this string. I would also like to to do this without using helpers. I have been trying to find examples on the web but have been unable to do so. Does anyone have a simple example? I am also using jQuery.
Thanks
CakePHP has a built-in JS Helper to help write aJax functions. The only catch is to include jquery in your head or cake will throw jQuery errors. Heres more information http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html
Your Form:
<?php
echo $this->Form->create('User', array('default'=>false, 'id'=>'YourForm'));
echo $this->Form->input('username');
echo $this->Form->submit('Check Username');
echo $this->Form->end();
?>
The Ajax Function: ('update'=>'#na') is the id of the element you want to update in your view.
<?php
$data = $this->Js->get('#YourForm')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#YourForm')->event(
'submit',
$this->Js->request(
array('action' => 'checkUsername', 'controller' => 'user'),
array(
'update' => '#na',
'data' => $data,
'async' => true,
'dataExpression'=>true,
'method' => 'POST'
)
)
);
echo $this->Js->writeBuffer();
?>
The Function in User Controller
function checkUsername(){
$this->autoRender = false;
$username = $this->User->find('first', array('conditions'=>array('User.username'=>$this->request->data['User']['username'])));
if ( $username == true )
echo 'Username is taken';
else
echo 'Username is not taken';
}
EDIT**
*If you want to use jQuery to do this and not the CakePHP Helper you can use aJax to call an action, then update your element like below*
$('#element').on('click', function() {
$.ajax({
url : '/controller/action',
type: 'POST',
success : function(response){
$('#elementToUpdate').html(response);
}
});
}
});
In your Controller Action you can return the "string" you would like to show in the view
function action(){
$string = 'Show this in the view';
return $string;
}
The above example would be executed when you "Click" an element with an id of "element" then upon "Success" would change element with id of "elementToUpdate" to the String "Show this in the view" Since it was returned from the controller action.

Passing parameter from controller to view in code igniter

I am new in code Igniter so I don't know how to do it. So what my problem is I have a form which I am submitting from ajax. So what I want to do is as the form submit successfully then a notification or a css div class will appear above the form and then disappear it.I don't know how can I perform this as after accepting the parameter from view page to controller I don't know how to send the parameter controller to view or how can I perform all this .Here is my controller:
class categoryController extends CI_Controller {
function index(){
$data['main_content'] = 'categoryView';
$this->load->view('dashboardTemplate/template',$data);
}
function addCategory(){
//getting parameters from view
$data = array(
'cat_name' => $this->input->post('cat_name')
);
$is_ajax = $this->input->post('ajax'); //or use this line
$this->load->model('categoryModel');
$query = $this->categoryModel->addCategories($data);
if ($query && $is_ajax){
$page['main_content'] = 'categoryView';
$page['v'] = '1'; // i dont know how this variable is not accessing in view page by echo $v
$this->load->view('dashboardTemplate/template',$page);
}
else
{
//
}
}}
Here is my view:
<?php
$attributes = array('id' => 'form-horizontal',
'class' => 'form-horizontal'
);
echo form_open('categoryController/addCategory', $attributes);
$cat_name = array(
'name' => 'cat_name',
'id' => 'cat_name',
'class' => 'cat_name');
$button = array(
'name' => 'button',
'id' => 'btn',
'class' => 'btn btn-primary',
'value' => 'submit',
'type' => 'submit',
'content' => 'Submit'
);
?>
<h3>Add Category</h3>
//here i want to do this .. that if form is submitted succesfully then this class will load only and the whole page remain the same
<div> class="alert-heading">sucess or not success!<div>
</div>
<div class="control-group">
<label for="basicround" class="control-label">Category Name:</label>
<div class="controls">
<?php echo form_input($cat_name); ?>
<div class="form-actions">
<?php echo form_button($button); ?></div>
<script type="text/javascript">
$('#btn').click(function() {
var cat_name = $('#cat_name').val();
if (!cat_name || cat_name == 'Name') {
alert('Please enter Category Name');
return false;
}
var form_data = {
cat_name: $('#cat_name').val(),
ajax: '1'
};
$.ajax({
url: "<?php echo site_url('categoryController/addCategory'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
$('#message').html(msg);
}
});
return false;
});
</script>
Since it is an Ajax submit, So you need to pass JSON array from controller to view
echo json_encode($page);
Controller
$page['main_content'] = 'categoryView';
$page['v'] = '1'; // i dont know how this variable is not accessing in view page by echo $v
echo json_encode($page);
For the above step, you need to define
data-type:JSON
in your ajax function.
Ajax Function
$.ajax({
url: "<?php echo site_url('categoryController/addCategory'); ?>",
type: 'POST',
data: form_data,
data-type: "json",
success: function(msg) {
// Here you can access the values from controller like msg.v
$('#message').html(msg);
}
});
Based on the response, you can show the success message using
$(".alert-heading").show();

Categories