Form submits more than once during redirect

#1
[eluser]natsabari[/eluser]
After a long googling i couldn't get the solution for my problem ,so i decided to ask u people,

My requirement is save a text from text area to table and send user to the same page where he submited the form.

Every thing works fine until i redirect user to the page where he submited the form.
Now two records added to the DB,first with correct data and the second with "0".

Is it normal.
any one want my code for analysis ,let me know.

#2
[eluser]eoinmcg[/eluser]
seems like when you redirect the script inserts blank data. probably best not to redirect but to once the form has been successfully completed load a different view.

please post your code for a more concrete solution.

#3
[eluser]natsabari[/eluser]
thanks eoinmcg, for the fast reply :-) .
i cant load different view,i want to redirect the user to the same page with data he had added.
here is my controller
Code:
if($this->uri->segment(4) === 'Y'){
            $argument = $this->input->post('argumentLeftText');
            $url = $this->input->post('currUrlLeft');
        }else{
            $argument = $this->input->post(');
            $url = $this->input->post(');
        }
        log_message('DEBUG','Adding new argument-->'.$argument.' side -->'.$this->uri->segment(4).' uri-->'.$this->uri->uri_string());
            $debateArray=array(
            'creater_id' =>$this->dx_auth->get_user_id(),
            'debate_id' =>$this->uri->segment(3),
            'side' => $this->uri->segment(4),
            'type' => $this->uri->segment(6),
            'argument' => $argument,
            'parent_argument_id' => $this->uri->segment(5)
            );
            //print_r($debateArray);
            if($this->debateDb->addArgument($debateArray)){
                log_message('DEBUG','Going to redirect the page to -->'.$url);
                redirect($url,'refresh');
            }else{
                echo 'some problem';
            }

my view


Code:
<form name="rightForm" action="<?=base_url()?>debate/new_argument/<?=$debate->id?>/N/02/P" method="post">
            <input type="hidden" value="<?php echo $this->uri->uri_string()?>" name="currUrlRight"/>
            <textarea  id="argumentRightText" name="argumentRightText" rows="5" cols="50" style="width: 100%;" >default</textarea><br/>
            <div style="text-align: center">&lt;input type="submit" value="Add" id="rightSubmitBtn" name="rightSubmitBtn" /&gt;&lt;/div>
        &lt;/form&gt;


Note:am new to PHP and codeigniter just 1 week before i started to learn PHP and codeigniter,so if some thing wrong in the code just indicate it.
Thanks in advance
nathan

#4
[eluser]jayrulez[/eluser]
[quote author="natsabari" date="1258152847"]thanks eoinmcg, for the fast reply :-) .
i cant load different view,i want to redirect the user to the same page with data he had added.
here is my controller
Code:
if($this->uri->segment(4) === 'Y'){
            $argument = $this->input->post('argumentLeftText');
            $url = $this->input->post('currUrlLeft');
        }else{
            $argument = $this->input->post(');
            $url = $this->input->post(');
        }
        log_message('DEBUG','Adding new argument--&gt;'.$argument.' side --&gt;'.$this->uri->segment(4).' uri--&gt;'.$this->uri->uri_string());
            $debateArray=array(
            'creater_id' =>$this->dx_auth->get_user_id(),
            'debate_id' =>$this->uri->segment(3),
            'side' => $this->uri->segment(4),
            'type' => $this->uri->segment(6),
            'argument' => $argument,
            'parent_argument_id' => $this->uri->segment(5)
            );
            //print_r($debateArray);
            if($this->debateDb->addArgument($debateArray)){
                log_message('DEBUG','Going to redirect the page to --&gt;'.$url);
                redirect($url,'refresh');
            }else{
                echo 'some problem';
            }

my view


Code:
&lt;form name="rightForm" action="&lt;?=base_url()?&gt;debate/new_argument/&lt;?=$debate-&gt;id?&gt;/N/02/P" method="post">
            &lt;input type="hidden" value="&lt;?php echo $this-&gt;uri->uri_string()?&gt;" name="currUrlRight"/>
            &lt;textarea  id="argumentRightText" name="argumentRightText" rows="5" cols="50" style="width: 100%;" &gt;default&lt;/textarea&gt;&lt;br/>
            <div style="text-align: center">&lt;input type="submit" value="Add" id="rightSubmitBtn" name="rightSubmitBtn" /&gt;&lt;/div>
        &lt;/form&gt;


Note:am new to PHP and codeigniter just 1 week before i started to learn PHP and codeigniter,so if some thing wrong in the code just indicate it.
Thanks in advance
nathan[/quote]

it's common practice to check for the presence of $_POST before saving data to the db. with php you can do it like this

Code:
if(isset($_POST)){ //process input and save to db... }else{ // render form }
. You can use the codeignitor equivalent to if(isset($_POST)) although using if(isset($_POST)) should work fine also.

#5
[eluser]natsabari[/eluser]
Quote:it's common practice to check for the presence of $_POST before saving data to the db. with php you can do it like this

Code:
if(isset($_POST)){ //process input and save to db... }else{ // render form }
. You can use the codeignitor equivalent to if(isset($_POST)) although using if(isset($_POST)) should work fine also.

It didn't work for me,still empty value is inserted to DB.
My question is why the controller which adds value to db is called again when i redirect the page.
what will happen(process) when i redirect a page in codeigniter?

#6
[eluser]eoinmcg[/eluser]
hi natsabari,

am i correct in assuming that the condition for submitting the form is ?
Code:
if($this->uri->segment(4) === 'Y'){

if you're redirecting to a url with segment 4 === Y the $argument variable will be empty but the data will be inserted into the db.

i'd recommend changing the flow of your program to sth like
Code:
&lt;?php


    if($this->uri->segment(4) === 'Y' && isset($this->input->post('argumentLeftText')))
    {
        $argument = $this->input->post('argumentLeftText');
        $url = $this->input->post('currUrlLeft');

        log_message('DEBUG','Adding new argument--&gt;'.$argument.' side --&gt;'.$this->uri->segment(4).' uri--&gt;'.$this->uri->uri_string());

        $debateArray=array
        (
            'creater_id' =>$this->dx_auth->get_user_id(),
            'debate_id' =>$this->uri->segment(3),
            'side' => $this->uri->segment(4),
            'type' => $this->uri->segment(6),
            'argument' => $argument,
            'parent_argument_id' => $this->uri->segment(5)
        );
        //print_r($debateArray);

        if(!$this->debateDb->addArgument($debateArray))
        {
            die('some problem');
        }


    }
    else
    {
        $argument = $this->input->post(''); // no need for this
        $url = $this->input->post(''); // i guess this isn't going to work...
    }


    log_message('DEBUG','Going to redirect the page to --&gt;'.$url);
    redirect($url,'refresh');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.