0

Is it possible to create multiple html text inputs on my

<form method="POST" id="2"> 

using:

<input type="text" value="">

with an initial value of none and fill it with a value after clicking submit from a previous

<form method="POST" id="1">

Can anyone help me with this?

Thanks in advance! By the way, I'm using PHP.

zerey
  • 841
  • 8
  • 19
  • 37

3 Answers3

2

This question is a bit vague and tagged incorrectly.

PHP is executed server side. The only way to modify your form is by using Javascript.

The answer to your "Is it possible" question is simply yes.

<form name="myform" action="handle-data.php">
Input: <input type='text' name='name' value='none' />
<a href="javascript: submitform()">Submit</a>
</form>
<script type="text/javascript">
function submitform()
{
  // Do things here
  document.myform.submit();
}
</script>
vicTROLLA
  • 1,554
  • 12
  • 15
0

Do they have to be separate forms?

You could use something like the jQuery Form Wizard Plugin to split up the form in the UI and use the step_shown event to populate the fields you need.

Town
  • 14,143
  • 3
  • 47
  • 71
0

@zerey: I coded up this commented contrived example of what I think you might have been hoping to achieve using PHP only and I've combined it all into one form --

<?php
if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST")
{
    $foo = intval($_POST['foo']);
    $bar = $_POST['bar'];

    if (count($bar) > 0)
    {
        // the additional inputs that were created now contains data
        // and furter validation can take place here
    }
}

if (!isset($foo))
{
    $foo = 0; // default the # of inputs field to 0
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
    <div>
        <label for="foo"># inputs:</label>
        <input type="text" name="foo" size="3" maxlength="1" value="<?php echo $foo; ?>">

        <input type="submit" value="Submit">
    </div>
    <?php
    // a check to limit the amount of additional inputs that can be
    // created
    if ($foo < 10 && $foo > 0)
    {
    ?>
    <div>
        <?php
        // loop to output the # of inputs the user previously entered
        for ($i = 0; $i < $foo; $i++)
        {
        ?>
        <br>
        <label for="bar[<?php echo $i; ?>]"><?php echo $i + 1; ?></label>
        <input type="text" name="bar[<?php echo $i; ?>]" id="bar[<?php echo $i; ?>]" value="<?php echo $bar[$i]; ?>">
        <?php
        }
        ?>
    </div>
    <?php
    }
    ?>
</form>
stealthyninja
  • 9,960
  • 11
  • 45
  • 55