在codeigniter中提交一次后,在每次刷新时停止表单提交

时间:2016-07-04 07:13:44

标签: forms codeigniter post insert duplicates

表单提交后,任意数量的刷新都会在db中多次存储数据。 即。提交后!empty($_POST)始终为真。

如何在不重定向网页/网址的情况下停止提交。因为我想留在同一页面。

我正在使用form_open()和form_submit()i codeigniter。

在视图中

    <?php echo form_open("",array('method' => 'POST'));?>
  /* form fields */
  '发送','价值'=&gt; '发送通知',“名称”=&gt;“发送通知”)); ?&GT;  

在控制器

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

如何停止重复记录插入,我尝试过,unset($_POST),isset(),if($ this-&gt; input-&gt; post()),似乎没有任何效果。

2 个答案:

答案 0 :(得分:1)

在插入函数后,只需执行redirect();到相同的年龄。

答案 1 :(得分:0)

这对我有用。它可能会帮助某人

控制器文件:

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

模型文件:

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
    }
}

我在提交时将txnid添加到数据库。因此,如果它尝试重新提交,它会检查txnid是否已经存在。

相关问题