Save fileds changes with ajax in laravel - php

I want to change some parts of my data from index page without loading edit page with Ajax.
For example my reviews has status where value is 0 or 1 and also comment section. I want to change these values from my index page.
All the code i have is simply my index method only, where i load my list
public function index()
{
$ratings = Rating::orderby('id', 'desc')->paginate(10);
return view('admin.ratings.index', compact('ratings'));
}
I need help to make my index page as a form with Ajax in order to edit from there but not to use my Update method because I'll need it for my edit.blade.php (maybe add another method in my controller?)
Thanks.

First of all you need a controller that updates the post status. As discussed it totally up to you want you want to do.
My suggestion is to create a new controller called UpdatePostStatusController and then have update method that takes post id and the status either 1 or 0 and update it.
Your index file should have javascript that gets triggred everytime you change the status like so
checkbox html
<input type="checkbox" data-uuid="{{ $post->id }}" class="update" #if($post->status) checked #endif/>
Now your ajax should
$('.update').change(function() {
data = {'status' : this.checked }
post_id = $(this).data('uuid');
//do your ajax post request here here
}

Related

insert url parameter in mysql with codeigniter

I need your help!
I am new to codeigniter and I am trying to pass a fetch parameter from url, get me the value using the following
My example url is the following: http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816
Where what I am rescuing is the value "NroContacto", I have managed to bring me the value with the following line in the controller:
'NroContacto' => $this->input->get('NroContacto');
and show it in the view with the following:
<td>
Numero Folio: <?php echo $nrocontacto; ?>
</td>
that way I have managed to show it in the user's view, but now I can't find a way to save that value in the database when the user clicks the "save" button
You have not shared much information related to your issue. I am assuming that you need two server calls to save the URL parameter into data.
First "http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816", is used to render the page with a form to submit.
Second, When the user clicks the submit button in the form, data is stored in a database.
If this is the case, on first page, you can collect nrocontacto as you have already done in your code and add a hidden input field <input type="hidden" name="nrocontacto" value="<?php echo $nrocontacto; ?>"> inside the form in the view file.
When the user submit the form, you can grab the value using $nroContacto = $this->input->post('NroContacto', true); (this code goes to form processing controller method). Now you can use CI query builder $this->db->insert('tableName', array('field_name' => $nroContacto)); (Note: add other required fields for insert array) to save it to the Database.
Codeigniter support seo friendly urls so you can arrange your url like this
http://example.com/index.php/news/local/metro/crime_is_up
for your url it will be
http://localhost/infocargacasosfinal/index.php/creacionacta/1/103386816
Now you can fetch like this
'NroContacto' => $this->uri->segment(3, 0);
now pass the value to your view page.
You can get detailed documentation about url segments here
https://codeigniter.com/userguide3/libraries/uri.html

Best way to keep pagination after editing a record

I'm very new to Laravel and I'm trying to make a crud application with a view for each db table that has a list with edit buttons on each record.
So far I have the paginated list which is working good and an edit view that is working as well.
The problem I'm facing is that whatever page I'm in I can click the edit button and edit the record successfully, but when I save the record I always go back to page 1.
I don't know and I'd like to know which is the correct (best) way to achive this.
Should I use session? Or maybe append the page parameter to the edit form action? Or else?
EDIT:
this is what edit method returns:
return redirect('/lyrics')->with('success', 'Lyric updated!');
Thank you for any help that points me in the right direction.
You can pass the page parameter to the edit page and then after the user update the record, you can redirect to the page you were in.
Something like this:
In the link where you call the edit page:
Edit
In your edit form:
<form>
... fields
<input type="hidden" name='page' value={{ app('request')->input('page') }}
</form>
Then in your update method at the controller
public function update($request)
{
// update
return redirect('homepage', ['page' => $request->page]);
}

CodeIgniter: View name different than Controller name, URL showing Controller's function name after POST instead of view

I'm using CodeIgniter v3.1.3
I have a Controller called User.php that mainly contains all of User management functions ( login, register, update_user, forgot_password, update_settings, ... )
And I have a View called Profile.php where on this view, two forms are available, one let's call it Form1, that updates the user info ( email, first_name, last_name ) and another, let's call it Form2, that updates the user settings ( news_letter_subscription, website_theme, allowed_countries_login ... )
Form1 submits to update_user function in the User controller
Form2 submits to update_settings function in the User controller
Whenever I submit one of these forms, after submitting, my URL becomes www.example.com/update_user or www.example.com/update_settings instead of staying www.example.com/profile ( since my view is called profile ).
My routes are the following:
$route['update_user']['POST'] = 'user/update_user';
$route['update_settings']['POST'] = 'user/update_settings';
My update_user function in the User.php controller:
http://pastebin.com/zQNn6Ykz
How can I make it that after the submission, the URL stays /profile and not /update_user or /update_settings ?
One thing you should know as well, I can't redirect to the profile page after the submission, as I'm using load->view since I'm passing data as well.
Thanks!
Edit: I can fix this problem by many other turnarounds, I could for instance make AJAX calls, or merge update_user and update_settings functions to one function called the same as the view, Profile() and send a hidden value with the action to take. But I'm honestly looking for a solution to fix my problem as it is, is it possible ?
If I understood, you are at server.com/profile, you save the form by calling another function in the same controller but still have the browser say you are at server.com/profile.
If that's the thing I can think of two ways to accomplish this.
The easy/hacky one would be to manipulate the history browser's object to change it with Javascript.
If you add this Javascript code in your profile view, every time it loads it will show the new_url. Change parameters accordingly.
window.history.pushState("object or string", "Title", "/new-url");
https://developer.mozilla.org/en-US/docs/Web/API/History_API
Another solution would be to call /profile from every form. This would call the profile function in the User controller. In there you can check first what has been passed.
If there is a $_POST with data, you know you have to save first before showing the profile. Then it is just a matter of detecting which form was sent to call the proper function in the User controller (i.e. $this->update_settings()).
You can add a hidden iframe with a name (for ex. my_iframe ) to your view (you need to add some css in order to hide it):
<iframe src="_blank" name="my_iframe"></iframe>
Then, put the "target" attribute to match you iframe name:
<form action="update_settings" method="post" target="my_iframe">
So, when you submit the form, it will do it in background. Maybe you need to use some javascript to retrieve the result.
Hope it helps.

Retrieve POST-data from view in controller

I'm trying to do something very basic: retrieve a post value from a hidden field. The hidden field is obviously in my view file and I want to retrieve the value in my controller. I'm using the framework SimpleMVCFramework.
I have a hidden field in my projects.php file (the list with projects). When you click on a project, a method in the controller renders the clicked project and the corresponding page. This corresponding page is called project.php
The hidden field in my projects.php view:
<form method="post" action="project.php">
<input type="hidden" name="project-id" value="<?php echo $project['id'];?>">
</form>
This hidden form is displayed correctly in my lists with projects. I checked them in the console.
In my ProjectController.php, I try to retrieve the data using
$data['id'] = $_POST['project-id'];
Then, I send the $data variable with the rendered page, so that I can use the id. So every project in projects.php has a hidden file that outputs correctly. When I try and click on a project, it brings me to project.php, but when I check out the $data variable, the id is just empty.
The routing works like a charm, because e.g. $data['title'] = "Project"; works great and is visible when I check the $data variable. When I change
$data['id'] = $_POST['project-id'];
to
$data['id'] = "foobar";
the id in project.php isn't empty anymore, but shows foobar. So I guess that something goes wrong with retrieving the value.
I also tried to remove the action=".." from the form, but that also didn't work.
The thing I'm trying to achieve is so simple, that I don't understand what is going wrong. Is it possible that the problem lies with the framework and that the code is right?
Thanks in advance and sorry for my bad English.

Lost data when moving page to page

I have a form to post with for example "title" data, once the submit button is pressed, the model will be used to process the data. the controller then uses the processed data to display in a view to the user. That is the flow I follow.
After getting the view, the user will choose to edit something. he thus needs to get back to the edittable form (same as the original one) to fix the title data. Here is the form
<?php form_open("blog/edit_updated")?>
Title:<input type="text" col="30" value="<?=$edit[0]['title']?>" name="edit_title"/><br/>
Comment:<br/><textarea name="edit_comment" width="20" col="5"><?=$edit[0]['comment']?></textarea><br/>
<input type="hidden" name="postcomment" value="TRUE"/>
<input type="submit" value="Update"/>
<?=form_close()?>
That form will call the following controler function
public function edit_updated()
{
if(!file_exists('application/views/blog/edit_succeed.php'))
{
show_404();
}
else
{
print_r($this->url_title);
if($this->blog->update_row_with_title(XXXXX,$_POST['edit_title'],$_POST['edit_comment']))
$this->load->view('blog/edit_succeed');
}
}
the function update_row_with_title is only used to seach and update the specified item with exact match of the given title. I test and it always return one (UPDATE command is used). XXXX is the title of the NOT_YET_TO_FIX title used to search in the database, the rest of parameters are those newly entered fields that will be used for SET in UPDATE command. However, XXXXX is always empty. Could you tell me a way to retrieve the original title ?
Summary
Form 1 (with data)--> post --> use data as part of url for GET --> fix the form (with new data) --> UPDATE [right here I lost the original data that is used as an identity for db searching]
It's a bit ambiguous your description, I'm not sure I understand what you want, but I'll try to offer a solution.
If you need the old title in the controller method you can actually send that as a parameter to it in the URL. To do this you have to make the following changes:
in the view containing the form change the argument of form_open to something like
<?php form_open("blog/edit_updated/" . $edit[0]['title'])?>
add an argument to the edit_updated() controller method like this
public function edit_updated($old_title = null) {
...
...
}
and now in the controller method you can replace that XXXXXX variable with $old_title.
Was this helpful ?

Categories