Dpk_Gopi
Dpk_Gopi

Reputation: 267

To stop form submit on every refresh after one submit in codeigniter

After form submission , any number of refresh is storing the data that many times in the db. ie. !empty($_POST) is always true after on submission.

How can I stop the submission without redirecting the page/url. As I want to stay in the same page.

I am using form_open() and form_submit() i codeigniter.

in view

    <?php echo form_open("",array('method' => 'POST'));?>
  /* form fields */
'Send', 'value' => 'Send Notifications',"name"=>"send_notification")); ?>

in controller

`if (!empty($_POST['send_notification'])) {
      /* validation rules  & data*/
      if ($this->form_validation->run() == TRUE){
         $this->model->insert($data);
      }
 }`

How can I stop the duplicate record insertion, I have tried , unset($_POST) , isset() , if($this->input->post()) , nothing seems to work.

Upvotes: 3

Views: 4030

Answers (2)

Abdulla
Abdulla

Reputation: 471

This worked for me. It may help someone

Controller file :

public function success()
{
    $_SESSION['txnid'] = $this->input->post('txnid');
    $this->bind->set_order();
    $this->load->view('success');
}

Model file :

public function set_order()
{
    $txnid = $_SESSION['txnid'];
    $qry = $this->db->query("SELECT * from orders where transaction_id = '$txnid'");
    $result = $qry->result_array();
    if($qry->num_rows() == 0 )
    {
     //add to database here
    }

    else
    {
      // do nothing
    }
}

I am adding txnid to database on submit. So if it tries to resubmit, it checks if txnid already exist or not.

Upvotes: 1

AL DI
AL DI

Reputation: 560

Just do a redirect(); to the same age after the insert function.

Upvotes: 2

Related Questions