Right now I'm trying to learn and test Laravel 4, and I have a little concern about script.
I wanted to insert information into the database, then redirect to the index page; the problem is that when I refresh the page, it displays the message to resend.
Here is the code I use:
public function store()
{
$new_cat = new Cat;
$new_cat->name = Input::get('new_cat');
$new_cat->age = Input::get('age_cat');
$new_cat->save();
return Redirect::action('CatsController#index');
}
The correct way to handle a POST request with a redirect is to issue a 303 status code.
http://en.wikipedia.org/wiki/HTTP_303
Using your original code:
return Redirect::action('CatsController#index', array(), 303);
Or Ochi's:
return Redirect::to('route_name', 303)->withInput();
maybe that will work
return Redirect::to('route_name')->withInput();
Related
Form.php(controller)
public function dispdata()
{
$result['data']=$this->Form_model->displayrecords();
$this->load->view('display_records',$result);
if (!$this->session->userdata('ci_session'))
{
redirect('Form/login');
}
else
{
$this->session->set_tempdata('item',$result,5);
}
}
display_records(view)
<?php
if($this->session->tempdata('item')){
redirect('Form/login');
}
?>
im trying to work with the tempdata concept. i have a registration form where i have stored all the registered details in the datbase and those store details of database i have displayed
it in the view page.
how that i have displayed all the database details in a view page that view page im trying to display only for 5sec and after 5sec it should redirect to the login page. i have tried with the above code but its not working please can anyone tel me where im going wrong ?
The tempdata feature for sessions only affects how long the data is allowed to be stored. The removal of the data, after 5 seconds in your case, won't cause anything else to change on the page.
As far as I can tell you don't need tempdata at all. Try this to see if you get the behavior you want.
public function dispdata()
{
if (!$this->session->userdata('ci_session'))
{
redirect('Form/login');
}
$result['data']=$this->Form_model->displayrecords();
$this->load->view('display_records',$result);
// sleep for 5 seconds
sleep(5);
redirect('Form/login');
}
Why did I remove the else from your code? Because redirect does not return - it ends script execution. So, when the if evaluates to true and the redirect executes this script is done.
I have a very simple web app created in Laravel 5.5:
There is a database with a list of coupon codes that have either not been redeemed or been redeemed. 0 is not redeemed and 1 is redeemed.
When someone enters a string into a HTML form input and submits it, Laravel goes to a route with that string as a variable.
The Controller code is as follows:
public function redeemCoupon ($coupon_code)
{
$coupon = Coupon::where('coupon_code', $coupon_code)->first();
if ($coupon === null) {
return view ('pages.no-coupon');
}
else if ($coupon->redeemed == 1) {
return view ('pages.used-coupon');
}
else {
$coupon->redeemed = 1;
$coupon->update();
return view('pages.redeemed-coupon', compact('coupon') );
}
}
Route:
Route::post('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
You can try it out here:
http://178.62.4.225
Everything works fine when done normally, tested on the code "code01". When I enter it and it hasn't been redeemed, it says so, and redeeming it changes the column in the database from 0 to 1. If I try the process again it tells me it has already been redeemed.
The issue is when I'm on the page that tells me it's been redeemed:
http://178.62.4.225/redeem-coupon/code01
If I refresh it with CTRL + R, it just reloads and says it's already been redeemed. But if I paste the URL into a new tab or click into it and refresh by clicking enter, it gives " MethodNotAllowedHttpException" and the resulting debug screen, from what I can tell, offers nothing of use.
Help!
Changing
Route::post('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
to
Route::any('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
Did the trick
You are doing a GET request and define a post route change
Route::post('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
to:
Route::get('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
Is redeemed set as protected? Also displaying app_debug true displays all your DB connection info (user and pass)
More than likely to be due to the _method.
What page is _method = "POST" on?
I have a activate button in my CMS that allows the user to quickly toggle a active/inactive flag for news articles. When clicked the toggle makes a POST request to the url /news/toggle_active/$id, after which the news_model updates the boolean variable in the database for the specific articles id, and the controller redirects back to the index.
Controller
function toggle_active()
{
$url = $this->uri->uri_string();
$last_segment = count($this->uri->segment_array());
$id = $this->uri->segment($last_segment);
$row = $this->news_model->set_active($id);
redirect('news/index', 'refresh');
}
Model
function set_active($id)
{
$data = array(
'display_flg' => 'NOT display_flg'
);
$this->db->where('id', $id);
$this->db->update($this->tbl_news, $data);
}
The first time the activate button is clicked it works, but subsequent clicks do nothing. If I close the browser window and re-open the page, the button works again, but only for one click.
I haven't specifically enabled any caching, so I'm not sure whats going on.
If I remove the redirect from the end of the toggle_active() method, and output the current value of the display_flg variable, and POST to /news/toggle_active/$id it appears to do nothing on the first request. However on each subsequent refresh the variable changes values. Why is it not doing anything the first time the link is hit?
I found some people with what seems to be a similar issue over at the codeigniter forums, although there was no solid solution.
I don't think this was a caching problem in the end. I made two major changes to fix the problem.
Firstly, I not check to see whether or not the database request was successful before redirecting.
I also re-wrote my the models set_active() method. Passing the SET clause as an array to the update functions second parameter wasn't working. I needed to use the set functions third parameter to stop the query from escaping the NOT part of the query.
I adjusted my functionality as follows:
Controller
function show_toggle($id)
{
if($this->news_model->set_active($id))
{
redirect('news/index', 'refresh');
}
}
Model
function set_active($id)
{
$this->db->where('id', $id);
$this->db->set('display_flg', 'NOT display_flg', FALSE);
$query = $this->db->update($this->tbl_news);
return $query;
}
I have a relatively simple class which deletes a post:
function delete_post($postid, $reason){
//Stuff to delete post
$this->delete_response = 'Thanks, your course has been removed.';
}
This function is called at the top of a page with a form on. If the form is submitted, the same page checks the POST[] and carries out the function, like so:
if(!empty($_POST['removecourse'])){
$courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
echo $courseManager->delete_response;
};
So my problem is... when I refresh the page, the message keeps displaying. I know this is because I am re-submitting the form, and because there is no such P/R/G pattern going on, but as i am new to OOP, im wondering if im doing this the right way, or if anyone could suggest a way similar to PRG or something?
Add an if that test if somthing changed, like mysql_affected_rows
function delete_post($postid, $reason)
{
//Stuff to delete post
if(mysql_affected_rows())
{
$this->delete_response = 'Thanks, your course has been removed.';
}
}
I would like to make a form I have in my website self referencing. Or if that's not an option, how would I go for, for example, showing the results of a search I make in my site?
I have a site in which you search for places and it returns a list of places for your preferences. At the moment my script creates a new node every time a user searches but this isn't convenient anymore. How do I change it so that the page content is changed and I see the results instead of the search form?
Thanks,
You should redirect your form to a page passing a query string with the string of what the user searched and then use $_GET['search_param'] in your search/restuls page to handle what will be displayed to the user.
function yourform_form($form_state) {
$form = array();
//$form['your_search_field']
$form['#submit'][] = 'yourform_form_submit';
return $form;
}
function yourform_form_submit(&$form, $form_state) {
$query = 'search_param='. $form_state['values']['your_search_field'];
drupal_goto('search/results', query);
}
If you're using Drupal 7 your submit function should look like:
function yourform_form_submit(&$form, $form_state) {
$options['query']['search_param'] = $form_state['values']['your_search_field'];
drupal_goto('search/results', $options);
}
After you submit you should be redirected to http://yoursite.com/search/results?search_param=my_search_value
Note that this technique is used by popular search engines:
https://www.google.com/search?q=my_search_value
Your form should include $form['#action'] to lead you to specific page after submit:
function example_form($form_state) {
$form = array();
//your form code
//...
$form['#action'] = url('search/results');
return $form;
}
On submitting form (example_form_submit) you should take all your values and save them to cookies using user_cookie_save function and on your page you can use this cookies.
You also could serialize your values to deal only with one cookie if you want, and then unserialize them on your page. You can delete cookie using user_cookie_delete function.
You should define path search/results where you could take those cookies data and manipulate with it.