php - Submit form, do in-page processing, and submit again -


i have 2 php pages, , b.

page has form, passes $_post['mode'] variable value 'edit' page b.

page b has form, submit button named '_go'. page b checks if $_post['_go'] parameter set , process form within same page b.

the problem that, on page b, if input value on form , submit form first time, works , success message. however, after success message, without refreshing page, re-input value , re-submit form, nothing , re-inputted value not processed.

i looked myself, , found $_post['_mode'] cause of problem. when submit form first time, $_post['_mode'] 'edit'. when re-submit form, $_post['_mode'] still set value empty.

i frustrated here.

  1. page b gets $_post['mode'] value 'edit' page a
  2. page b stores $_post['mode'] variable $mode, , value of $mode 'edit'
  3. page b submits form, $_post['_mode'] set , value of $_post['_mode'] 'edit'.
  4. page b re-submits form, since didn't refresh page or something, value of $mode should still 'edit' , suppose value of $_post['_mode'] 'edit', $_post['_mode'] set empty.

how keep $mode is?

my code in page below (simplified).

// page  <form action="page b" id="some id" name="some name" method="post">   <input type="hidden" name="mode" value="edit" />   <input type="submit" class="btn" value="edit" /> </form> 

my code in page b below (simplified).

// page b  <?php    // if form submitted , mode 'edit', go process form    if( isset( $_post['_go'] && isset( $_post['_mode'] ) ) {      $_go_mode = $_post['_mode'];      if( $_go_mode == 'edit' ) {       process_form();     }    }    function process_form() {      // function receives parameters form     // , needs      echo '<div class="process-result">';     echo 'successfully made changes';     echo '</div>';    }  ?>  <div>    <?php    // $_post['mode'] variable passed page    if( isset( $_post['mode'] ) ) {     $mode = $_post['mode'];   }    ?>    <form method="post" class="form-horizontal">      <div class="form-group">       <label class="control-label">some label</label>       <input type="text" class="form-control" name="_name" value="some value" />     </div>      <div class="form-group">       <input type="hidden" name="_mode" value="'.<?php echo $mode; ?>.'" />       <input type="submit" name="_go" class="btn" value="make changes" />     </div>    </form>  </div> 

as work-around, changed code below. works intended, don't feel nice way it...

before:         if( $_go_mode == 'edit' ) {           process_form();         }  after:         if( $_go_mode == 'edit' ) {           $_post['mode'] = $_go_mode;           process_form();         } 

i have modified code use sessions solve problem of loosing variable , can reuse in other pages. have set action of page form "" creating session in page redirecting page b .note "session_start();" must on top of each page code above can "

page a

<?php session_start();    if( isset( $_post['mode'] ) ) {       $mode = $_post['mode'];       //setting session       $_session['mode']=$mode;       //put own file name b in next line       header("location:pageb.php");       } ?  <form action="" id="some id" name="some name" method="post">   <input type="hidden" name="mode" value="edit" />   <input type="submit" class="btn" value="edit" /> </form> 

page b

<?php session_start(); //checking session , assigning variable name  if (isset($_session['mode'])) {     $mode = $_session['mode'];     } // if form submitted , mode 'edit', go process form if( isset( $_post['_go'] && isset( $_post['_mode'] ) ) {     $_go_mode = $_post['_mode'];     if( $_go_mode == 'edit' ) {        process_form();        }   }    function process_form() {      // function receives parameters form     // , needs      echo '<div class="process-result">';     echo 'successfully made changes';     echo '</div>';    }  ?>  <div>     <form action "" method="post" class="form-horizontal">      <div class="form-group">       <label class="control-label">some label</label>       <input type="text" class="form-control" name="_name" value="some value" />     </div>      <div class="form-group">       <input type="hidden" name="_mode" value="<?php echo $mode; ?>" />       <input type="submit" name="_go" class="btn" value="make changes" />     </div>    </form>  </div> 

check out posting same page

<html>     <head>     </head>     <body>         <form name="anyname" action="" method="post">             <input type="radio" name="choice" value="asia"/>asia             <input type="radio" name="choice" value="africa"/>africa             <input type="submit" name="submit" value="my radio choice">         </form> <?php        function choose(){         //declare global variable outside function         global $choice;           if ($choice =='africa') {         $test='you choose africa';         }            elseif ($choice =='asia'){         $test='you choose asia';         }         //returning output of function         return $test;         }        //here proccess data after page reloads      if (!empty($_post['choice'])){           $choice=$_post["choice"];         //running function         choose();         //getting output function         $testafter=choose();         echo 'this data after page reloads :',$aftertest;         }        //here process data before page reloads     if ((isset($_post["submit"])) && ($_post["submit"]!="")){         $choice=$_post["choice"];         //running function         choose();         //getting output function         $testbefore=choose();          } ?>       <body> </html> 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -