Jump to content

Dynamic forms


Ogdan

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.