AjaxSubmitButton in Yii framework does not save - php

after succeeding in not refreshing the page after submission, my ajaxSubmitButton does not save the data. When I click on the button, it removes the content of the input fields but does not save the data. No message is displayed despite the error and success parameters.
In my form submission, the code for submit button is the following
<?php echo CHtml::ajaxSubmitButton(
'Save',
Yii::app()->createUrl('post/viewComment'),
array(
'success'=>'js:function(data){
alert("commentSubmitted");}',
'error'=>'js:function(data){
alert("comment NOT Submitted");}',
)
);
?>
My controller action code is
public function actionViewComment()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->renderPartial('_viewComment',array(
'model'=>$post,
'comment'=>$comment,
));
the viewCommment view, display the _view view to list the comments and at the end display the _form view to make the user input the new comment
the newComment function contains the following
protected function newComment($post)
{
$comment=new Comment;
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
if(isset($_POST['Comment']))
{
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
$this->refresh();
}
}
return $comment;
}
Actually, I use the blog demo from Yii Framework, I though it was a good starting point to learn Yii Framework.
the home page display list of posts. (it renderPartial _view.php.)
I create the ajax button to be able to list the commments associated with a given post and display an input form for new comment at the bottom of the form.
I added a second button with the "classical submit"
<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>
it works but redirect me to the list of the comments, that's what I wanted to avoid;
By using AjaxsubmitButton. When I put my cursor over the classical button or the Ajax button, the link is the same ...../blog/index.php/post/viewComment?id=...
The display is the following
post 1
post 2
comment 1
comment 2
comment 3
Add comment
Input Field
AjaxSubmit Button
post 3
and when I click on the ajaxsubmit button, I have the following
post 1
post 2
comment 1
comment 2
comment 3
Add comment
Input Field
AjaxSubmit Button
Add comment
Input Field
AjaxSubmit Button
post 3
So I got a second form window below the first one. And the comment is not saved.
Thank you in advance for your help

You are processing the data from POST. But you didn't mention in code.
<?php echo CHtml::ajaxSubmitButton(
'Save',
Yii::app()->createUrl('post/viewComment'),
array(
'type'=>'POST',
'dataType'=>'json',
'success'=>'js:function(data){
alert("commentSubmitted");}',
'error'=>'js:function(data){
alert("comment NOT Submitted");}',
)
);
?>

Related

How to pass data from anchor tag in laravel?

I currently have a form in laravel on whos submission the following methods run:
public function validateSave() {
$qualitycheck = new QualityCheck();
$qualitycheck['site-name'] = Request::input('site-name');
$qualitycheck['favicon'] = Request::has('favicon');
$qualitycheck['title'] = Request::has('title');
$qualitycheck['image-optimization'] = Request::has('image-optimization');
$qualitycheck->save();
Session::flash('quality-data', $qualitycheck);
return redirect('/');
}
So i have the below line that passes the data to the next page:
Session::flash('quality-data', $qualitycheck);
But what i would really want to do is, when the form is submitted, i would really just want to show a link on the next page , which will be coded like so:
#if(Session::has('quality-data'))
Submited Quality Check
#endif
Now on click on the link , i would like to show a view with all the data that the user submitted in the form , How do i do this ? I.E. How do i pass the data form from the <a> to the view that will show up when clicked on the <a> ??
So just to put things into perspective, this is how it works now:
STEP-1 :: User submits form , data is flashed to next page.
STEP-2 :: Data user submits is shown on this page.
How i want it to work is:
STEP-1 :: User submits form , data is flashed to next page.
STEP-2 :: A link is shown to the user(Only if user clicks on the link we move to the next step).
STEP-3 :: Data user submited in first step is shown on this page.
Next time you code, please follow the below coding practices.
Prefer using create() function of model.
Put all your request data that is to be used in one variable (like $input)
Prefer using route names like route('route.name') instead of strings inside redirection() function.
Please replace your function with
public function validateSave() {
$inputs = [
'site-name' => request()->get('site_name'),
'favicon' => request()->has('facvicon'),
'title' => request()->has('title'),
'image-optimization' => request()->has('image-optimization')
]);
$qualityCheck = QualityCheck::create($inputs);
$flashMessage = '<a href=' . route('quality.check.show', $qualityCheck) . '>Submitted Quality Check</a>'
Session::flash('quality-data', $flashMessage);
return redirect(route('home.index'));
}
And ensure you have something like this in your routes file.
Route::get('quality-check/{id}', 'QualityCheckController')->name('quality.check.show');
Let me know if something doesn't work...
try this one.
<a href="{{url('routename')}}">

Show data from another table with modal - Codeigniter

i have 2 tables
Now when i click detail , i want to show data from table 2 which have reportcode as i click on table 1 (image 1)
And now i want to show it on modal , so here is the example
1) click detail button -> get reportcode -> show reimbursename,etc to modal
Can you explain to me what should i do first ? and can you suggest me a plan please ,any answers will be appreciated. Thanks
My suggestion is:
1 - Add one class to detail button, i.e: detailButton and a data attribute or href with the especific reportCode.
<table>
<tr>
<td> ... </td>
<td> <button class='detailButton' href='<?php echo $reportCode; ?>' ... </button> </td>
2 - Add jquery to the bottom of the page:
$('.detailButton').click(function(e){
e.preventDefault();
var reportCode = $(this).attr('href');
var url = "yourUrl/controller/function";
$.post(url,{ code:reportCode },function(data){
//do stuff
//i.e: $('.modal').html(data).show();
});
});
Now you have a function that gets the reportCode, sends it to your controller by POST, you return something and the function gets the response and attach to a html.
Note, this way you must return a table from your controller. You could build dinamically too.
Hope it helps!
UPDATE:
You could check the values to your model and then use a exisitin template (for example one that generates the detail table), and return to your view as data to be attached at the correct position (method 1):
function detail(){
$getcode= $this->input->post('reportCode');
$data['showdetail'] = $this->model_expreport->showdetail($getcode);
$ret = $this->load->view('detail_template',$data,true); //return as data
print_r($ret);
}
Or you could use the Method 2:
function detail(){
$getcode= $this->input->post('reportCode');
$data['showdetail'] = $this->model_expreport->showdetail($getcode);
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));
}
This way, the view will recive a JSON that you could iterate and build your own page. Or you could create the full view and return it as data (in order to only append to your view).
You could use both.
In the view, you will recive either a full view:
$.post(url,{ code:reportCode },function(data){
$('#modal').html(data); //put the 'detail' response to the modal
}
Or with JSON you must iterate and build your own div dinamically, there are a lot of tutorials for this: https://uno-de-piera.com/cargar-json-con-jquery-y-codeigniter/

How to get selected from jmultiselect2side in yii and display in a new page

I'm new to yii and I don't understand the extensions much
but I used this extension called jmultiselect2side because I'm trying to make a site where users could reserve stuff like apparatuses in the lab
Anyway, I need a code that would get the Selected Items and then display them in another page for viewing purposes
I haven't put anything in the controller but the name of my controller and model is Apparatus
Here is my view:
<?php
$model= Apparatus::model()->findByAttributes(array('ApparatusCode'=>'1'));
// complete user list to be shown at multiselect order by ApparatusCode
$Apparatus= Apparatus::model()->findAll(
array('order' => 'ApparatusCode'));
?>
<center>
<?php
$this- >widget('application.extensions.jmultiselect2side.Jmultiselect2side',array(
'model'=>$model,
'attribute'=>'ApparatusName', //selected items
'labelsx'=>'Available',
'labeldx'=>'Selected',
'moveOptions'=>false,
'autoSort'=>'true',
'search'=>'Search:',
'list'=>CHtml::listData( // available items
$Apparatus,
'ApparatusCode',
'ApparatusName'),
));
?>
please help as soon as possible :/
put all above elements in a form. Set action for the form. Submit the form then the action in which you are handling this submit request, you can write there
if(isset($_POST))
{
foreach($_POST['Apparatus']['ApparatusName'] as $name)
{
do what ever you want
}
}
$name will represent the each selected item

jquery - x-editable - reset 'new record' after submit

I have been trying to figure this out for hours to no avail.
I have the following
http://vitalets.github.com/x-editable/docs.html#newrecord
the new record module/code. Its functionality is, once all the rows have their data, and it is submitted, it retains its value so that it can stay editable.
I have tried adding input, select clearing code in the success of the ajax request and tried putting the .editable function inside an ajaxComplete function to see if it would reload the element on submit but it didnt. look at the demo. enter data, and submit. it then makes the data "permanent" so that it can continue to be editable.
I have removed the code that hides the button.
What i want it to do is, submit the record and reset so i can submit another with the form being 'Empty' and back to default.
I am developing an equipment tracker and would love if techs could just enter records, one after another. im sure its a simple fix to reset the form, i just cannot figure it out.
I have attached a screencast video of it.
http://www.youtube.com/watch?v=dPDuQCgOOSw
as it's popular question, I've added Reset button to documentation.
$('#reset-btn').click(function() {
$('.myeditable').editable('setValue', null) //clear values
.editable('option', 'pk', null) //clear pk
.removeClass('editable-unsaved'); //remove bold css
$('#save-btn').show();
$('#msg').hide();
});
It's better to use .editable('setValue', null) instead of .text('Empty') as we need also to reset internal value.
HTH
If you look how the "save new user button" is implemented, it just selects the anchor tags that have classname myeditable
Either you bind a function to your 'reset' button or have this script onclick
IF onclick:
jQuery.each($('.myeditable'),function() {
$(this).text('Empty') ; //this is your default text
});
OR as a function
function reset_myeditable()
{
jQuery.each($('.myeditable'),function() {
$(this).text('Empty') ; //this is your default text
});
}
and attach this to the onclick handler of your reset button
This I assume you will be putting a 'reset' button
EDIT
Ok zoom in to your $('#save-btn').click(function() success handler
success: function(data, config) {
if(data && data.id) { //record created, response like {"id": 2}
$(this).editable('option', 'pk', data.id);
//remove unsaved class
$(this).removeClass('editable-unsaved');
//show messages
var msg = 'New user created! Now editables submit individually.';
$('#msg').addClass('alert-success').removeClass('alert-error').html(msg).show();
$('#save-btn').hide();
$(this).off('save.newuser');
/**this the edited part**/
jQuery.each($('.myeditable'),function() {
$(this).text('Empty') ; //this is your default text
});
/**end of edit**/
} else if(data && data.errors){
//server-side validation error, response like {"errors": {"username": "username already exist"} }
config.error.call(this, data.errors);
}
},

To retrieve value from a text box in a form (view) to controller in cake php

How to o retrieve value from a text box in a form (view) to controller in cake php?
Here's an example from the CakePHP book on saving your Model data:
function edit($id) {
//Has any form data been POSTed?
if(!empty($this->data)) {
//If the form data can be validated and saved...
if($this->Recipe->save($this->data)) {
//Set a session flash message and redirect.
$this->Session->setFlash("Recipe Saved!");
$this->redirect('/recipes');
}
}
//If no form data, find the recipe to be edited
//and hand it to the view.
$this->set('recipe', $this->Recipe->findById($id));
}
If your model was Recipe, and your input was named "title", then the value would be in $this->data['Recipe']['title'], if you setup your view like so:
echo $this->Form->create('Recipe');
echo $this->Form->hidden('id');
echo $this->Form->input('title');
echo $this->Form->end('Save Recipe');
So, look here: http://book.cakephp.org/view/1031/Saving-Your-Data
And try to do the Blog tutorial, it might help you get started: http://book.cakephp.org/view/1528/Blog

Categories