CodeIgniter Forums
avoid sending data when refreshed page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: avoid sending data when refreshed page (/thread-22024.html)



avoid sending data when refreshed page - El Forum - 08-27-2009

[eluser]ranjitbd[/eluser]
how to avoid sending data into database when click on refresh button.
i already set validation.so no one can submit any empty feedback.but when a feed back is submitted and show the session message to the
user that feedback submitted successfully.then if i click on refresh in the browser it submitted again.
i can redirect to another page to avoid this.but i want to stay in the same feed back page..

here is my code

function feedback()
{
$data = array();
$data['mainContent'] = $this->load->view('tmbd/feedback', $data, true);
$this->load->view('index', $data);
}

function feedbackSave()
{
$data=array();

$data['subject'] = $this->input->post('subject', true);
$data['name'] = $this->input->post('lname', true);
$data['address'] = $this->input->post('address', true);
$data['email'] = $this->input->post('email', true);
$data['mobile'] = $this->input->post('mobile', true);
$data['comments'] = $this->input->post('comments', true);

$datetime = date('Y-m-d H:iConfused');
$data['date'] = $dateTime;

$this->load->model($this->config->item('generalModel'), 'save_feedback',TRUE);
$this->save_feedback->feedbackSave($data);

$this->session->set_flashdata('feedback', 'Feedback send successfully');
$this->feedback();
}
//maybe it can be solved by using headers function to reload the page....but i dnt knw how to use it?
// thanks in advance for the solution.


avoid sending data when refreshed page - El Forum - 08-27-2009

[eluser]WildPhoenix[/eluser]
I would add another function called "thanks" or whatever you want, then just redirect them if the data is saved successfully.

Code:
redirect('feedback/thanks');

Then just load your "success" view and any other associated data you may need to pass.


avoid sending data when refreshed page - El Forum - 09-01-2009

[eluser]ranjitbd[/eluser]
if i redirect then i have to go indifferent page but i want to show the user that same feedback page?

so what to do?


avoid sending data when refreshed page - El Forum - 09-01-2009

[eluser]budbud[/eluser]
Usually I use if clause to handle this problem.
I catch name of the submit button from view to the if clause.
If the button clicked, the code successfully execute, else do nothing.
Example :

Code:
<?php
  function feedbackSave()
  {
    if($this->input->post('submit'))//button name depending what you set it
    {
      $data=array();
    
      $data[‘subject’] = $this->input->post(‘subject’, true);
      $data[‘name’] = $this->input->post(‘lname’, true);
      $data[‘address’] = $this->input->post(‘address’, true);
      $data[‘email’] = $this->input->post(‘email’, true);
      $data[‘mobile’] = $this->input->post(‘mobile’, true);
      $data[‘comments’] = $this->input->post(‘comments’, true);
    
      $datetime = date(‘Y-m-d H:i:s’);
      $data[‘date’] = $dateTime;

      $this->load->model($this->config->item(‘generalModel’), ‘save_feedback’,TRUE);
      $this->save_feedback->feedbackSave($data);
    
      $this->session->set_flashdata(‘feedback’, ‘Feedback send successfully’);
      $this->feedback();
    }
  }
?>

Hope I can help you,.


avoid sending data when refreshed page - El Forum - 09-02-2009

[eluser]lordmontie[/eluser]
Why not just do a
Code:
redirect('/controller/feedback/');
instead of
Code:
$this->feedback();
. The redirect will clear the POST data and reload the feedback function.