Ogdan Posted February 3, 2010 Share Posted February 3, 2010 Hey everyone I was just wondering if it were possible to make dynamic forms that were either self posting or posted to another page. For example could i use the $_GET array to make a form on another page that added elements based on input from that form? like so <form action="Question7_process.php" method="get"> please enter a number: <input type="text" name="number" value="" /> <input type="submit" name="submit" value="Submit" /> </form> then use that number to make a form with how ever many input fields and have it SELF POST and pull the values off of the $_POST array? I'm having a problem and would love some input. Thank You Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted February 3, 2010 Share Posted February 3, 2010 Can you explain what you mean by self posting as you can easily make a form with the number of inputs put in the first form with a loop. But why would you want that to self post as you don't have anything inputted till the user adds something? Quote Link to comment Share on other sites More sharing options...
Ogdan Posted February 3, 2010 Author Share Posted February 3, 2010 ok so here might be a better explanation so i want a user to enter how many numbers they would like to add, lets say the user inputs 4 then the form would either SELF POST or POST to another page with the 4 input fields, where the user would enter 4 numbers then the form would add them all up. The problem I am having is on the second page the only thing that is getting input to the $_POST array is the last number. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 3, 2010 Share Posted February 3, 2010 You can do this with a form that consistently reposts to itself. You also need to be familiar with the concept of a state machine. You can find plenty of material online about FSM (Finite State Machines). The short of it is you create a machine (aka program) that has several states. What it does depends on which state it is currently in! <?php define( 'STATE_INIT', 0 ); define( 'STATE_HAVENUM', 1 ); define( 'STATE_ENTERNUMS', 2 ); $state = array_key_exists( 'moveto', $_POST ) ? $_POST['moveto'] : STATE_INIT; $state = in_array( $state, array( STATE_INIT, STATE_HAVENUM, STATE_ENTERNUMS, STATE_FINAL ) ) ? $state : STATE_INIT; // By this point, $state is one of the three states $errors = array(); // We might have errors if( $state == STATE_HAVENUM ) { // Must confirm that the number is a number! $num = array_key_exists( 'num', $_POST ) ? $_POST['num'] : false; $num = is_numeric( $num ) ? ((int)$num) : false; if( $num === false ) { $errors[] = 'You did not enter a number!'; $state -= 1; // Decrease state by 1 } }else if( $state == STATE_ENTERNUMS ) { // Must confirm all entered numbers are numbers! $had_err = false; $total = 0; if( array_key_exists( 'nums', $_POST ) && is_array( $_POST['nums'] ) ) { foreach( $_POST['nums'] as $v ) { if( ! is_numeric( $v ) ) { $had_err = true; break; } $total += $v; } } if( $had_err ) { $errors[] = "Fill out all fields with numbers!"; $state -= 1; } } // Begin our form $form = <<<EOT <form method="post" action="{$_SERVER['REQUEST_URI']}" /> EOT; if( $state == STATE_INIT ) { $moveto = STATE_HAVENUM; $form .= <<<EOT <p> <input type="hidden" name="moveto" value="{$moveto}" /> How many inputs? <input type="text" name="num" value="" size="3" /> </p> <p><input type="submit" name="submit" value="submit" /> EOT; }else if( $state == STATE_HAVENUM ) { $moveto = STATE_ENTERNUMS; $form .= <<<EOT <div> <input type="hidden" name="moveto" value="{$moveto}" /> <input type="hidden" name="num" value="{$_POST['num']}" /> </div> EOT; for( $i = 0; $i < $_POST['num']; $i += 1 ) { $form .= <<<EOT <p><input type="text" name="nums[]" value="" size="3" /></p> EOT; } $form .= <<<EOT <p><input type="submit" name="submit" value="submit" /> EOT; }else if( $state == STATE_ENTERNUMS ) { echo "<p>The total is {$total}</p>"; } $form .= "</form>"; if( count( $errors ) ) { echo '<p>You had errors:</p>'; echo '<ul><li>' . implode( '</li><li>', $errors ) . '</li></ul>'; } echo $form; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.