【问题标题】:To stop form submit on every refresh after one submit in codeigniter在 codeigniter 中提交一次后,在每次刷新时停止表单提交
【发布时间】:2016-11-05 20:07:26
【问题描述】:

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

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

我正在使用 form_open() 和 form_submit() 进行代码点火。

在视图中

    <?php echo form_open("",array('method' => 'POST'));?>
  /* form fields */
'发送', '值' => '发送通知',"name"=>"send_notification")); ?>

在控制器中

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

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

【问题讨论】:

  • 您可以使用redirect('controller/function'); 将页面重定向到同一个控制器,如果您想显示消息,则可以使用附加的内容进行重定向,例如redirect('controller/function/?success');。然后在控制器if(isset($_GET['success'])){ $data['success'] ="Your success message here"; }
  • 谢谢,在重定向到同一个控制器之前闪现消息有帮助。!!

标签: forms codeigniter post insert duplicates


【解决方案1】:

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

控制器文件:

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 是否已经存在。

【讨论】:

    【解决方案2】:

    只需在插入函数后对同龄做一个redirect();即可。

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多