简体   繁体   中英

When i refresh my page it automactically input values to the database Codeigniter

i have created a from using codeigniter. When user first comes to the form page, The user then submit the values and the values are then stored into the database and the user is directed to the form page again. Now the problem is if the user refreshes's the page after first submitting the form the last inserted values are automatically added into the database again and again as the user refreshes the page how to stop this ???

Here is my View Code...

        <?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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Adventure">Adventure&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Animation">Animation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Biography">Biography<br />

                    <input type="checkbox" name="genres" value="Comedy">Comedy&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Crime">Crime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Documentary">Documentary&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Drama">Drama<br />

                    <input type="checkbox" name="genres" value="Family">Family&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Fantasy">Fantasy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Film-Noir">Film-Noir&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Game-Show">Game-Show<br />

                    <input type="checkbox" name="genres" value="History">History&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Horror">Horror &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Music">Music&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Musical">Musical<br />


                    <input type="checkbox" name="genres" value="Mystery">Mystery&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="News">News&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Reality-TV">Reality-TV&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                     <input type="checkbox" name="genres" value="Romance">Romance<br />



                    <input type="checkbox" name="genres" value="Sci-Fi">Sci-Fi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Sport">Sport&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Talk-Show">Talk-Show&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="genres" value="Thriller">Thriller<br />

                    <input type="checkbox" name="genres" value="War">War&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <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>

This is my controller Code...

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');
        }   
    }
}

This is my Model Code...

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() will serve the purpose.

You could also replace that submit input for a simple button with a given ID, and then use (for example) jQuery to listen to the "onclick" event of that button and then do the POST request, instead of doing it with the browser as you're now.

I find this way more elegant for it allows you to display error messages without making you start over with the form filling process, that sometimes is tedious. And of course, since there's no post header in the current page, you can reload as many times as you want without actually posting unadvertely.

You might want to show us how that "form open" function parses, mostly to know where to POST to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM