[英]When i refresh my page it automactically input values to the database Codeigniter
我創建了一個使用codeigniter。 當用戶第一次進入表單頁面時,用戶然后提交值,然后將值存儲到數據庫中,並再次將用戶定向到表單頁面。 現在問題是如果用戶在第一次提交表單后刷新頁面,最后插入的值會一次又一次地自動添加到數據庫中,因為用戶刷新頁面如何停止這個???
這是我的查看代碼...
<?php echo form_open("site/upload_data"); ?>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Movie Name:
</div>
<div id="field" style="float:left; width:411px;">
<input type="text" name="movie_name" />
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Release Date:
</div>
<div id="field" style="float:left; width:411px;">
<input type="text" name="date" />
</div>
</div>
<div id="container" style="float:left; width:561px; height:160px">
<div id="name" style="float:left; width:150px;">
Genres:
</div>
<div id="field" style="float:left; width:411px;">
<input type="checkbox" name="genres" value="Action">Action
<input type="checkbox" name="genres" value="Adventure">Adventure
<input type="checkbox" name="genres" value="Animation">Animation
<input type="checkbox" name="genres" value="Biography">Biography<br />
<input type="checkbox" name="genres" value="Comedy">Comedy
<input type="checkbox" name="genres" value="Crime">Crime
<input type="checkbox" name="genres" value="Documentary">Documentary
<input type="checkbox" name="genres" value="Drama">Drama<br />
<input type="checkbox" name="genres" value="Family">Family
<input type="checkbox" name="genres" value="Fantasy">Fantasy
<input type="checkbox" name="genres" value="Film-Noir">Film-Noir
<input type="checkbox" name="genres" value="Game-Show">Game-Show<br />
<input type="checkbox" name="genres" value="History">History
<input type="checkbox" name="genres" value="Horror">Horror
<input type="checkbox" name="genres" value="Music">Music
<input type="checkbox" name="genres" value="Musical">Musical<br />
<input type="checkbox" name="genres" value="Mystery">Mystery
<input type="checkbox" name="genres" value="News">News
<input type="checkbox" name="genres" value="Reality-TV">Reality-TV
<input type="checkbox" name="genres" value="Romance">Romance<br />
<input type="checkbox" name="genres" value="Sci-Fi">Sci-Fi
<input type="checkbox" name="genres" value="Sport">Sport
<input type="checkbox" name="genres" value="Talk-Show">Talk-Show
<input type="checkbox" name="genres" value="Thriller">Thriller<br />
<input type="checkbox" name="genres" value="War">War
<input type="checkbox" name="genres" value="Western">Western
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Rating:
</div>
<div id="field" style="float:left; width:411px;">
<input id="ttcontact" type="radio" checked="checked" value="G" name="rating">
G
<input id="ttcontact" type="radio" value="PG" name="rating">
PG
<input id="ttcontact" type="radio" value="PG-13" name="rating">
PG-13
<input id="ttcontact" type="radio" value="R" name="rating">
R
<input id="ttcontact" type="radio" value="NC-17" name="rating">
NC-17
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Stars:
</div>
<div id="field" style="float:left; width:411px;">
<input type="text" name="star" placeholder="Eg: Max, John" />
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Director:
</div>
<div id="field" style="float:left; width:411px;">
<input type="text" name="director" />
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Writers:
</div>
<div id="field" style="float:left; width:411px;">
<input type="text" name="writers" />
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px">
<div id="name" style="float:left; width:150px;">
Movie Path:
</div>
<div id="field" style="float:left; width:411px;">
<input type="text" name="path" />
</div>
</div>
<div id="container" style="float:left; width:561px; height:50px; text-align:center">
<div id="btn" style="float:left;margin-left: 375px; margin-top: 25px;">
<input type="submit" value="Submit" />
</div>
</div>
<?php echo form_close(); ?>
</div>
這是我的控制器代碼......
public function upload_data()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('movie_name', 'Movie Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('date', 'date', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('genres', 'genres', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('star', 'star', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('director', 'director', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('writers', 'writers', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('path', 'path', 'trim|required|min_length[4]|xss_clean');
if($this->form_validation->run() == FALSE)
{
$this->admin();
}
else
{
$result = $this->site_upload->add_data();
if ($result)
{
$this->load->view('admin');
}
}
}
這是我的標准代碼......
public function add_data()
{
$values = array (
'movie_name' => $this->input->post('movie_name'),
'date' => $this->input->post('date'),
'genres' => $this->input->post('genres'),
'rating' => $this->input->post('rating'),
'star' => $this->input->post('star'),
'director' => $this->input->post('director'),
'writers' => $this->input->post('writers'),
'path' => $this->input->post('path')
);
$rows = $this->db->insert('data',$values);
return $rows;
}
成功插入后可以重定向頁面以防止重復插入。
header("Location: your_submit_page.php?fresh");
if($this->form_validation->run() == FALSE)
{
$this->admin();
}
else
{
$result = $this->site_upload->add_data();
if ($result)
{
redirect('/path/you/want/to/redirect'); //do the redirection
}
}
redirect()
將達到目的。
您還可以替換具有給定ID的簡單按鈕的提交輸入,然后使用(例如)jQuery來監聽該按鈕的“onclick”事件,然后執行POST請求,而不是使用瀏覽器執行此操作你現在。
我發現這種方式更優雅,因為它允許您顯示錯誤消息,而不會讓您重新開始填寫表單,這有時是單調乏味的。 當然,由於當前頁面中沒有帖子標題,您可以根據需要重新加載多次,而無需實際發布。
您可能想向我們展示“表單打開”功能如何解析,主要是為了知道POST的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.