Add value on textbox upon clicking submit
Is it possible to create multiple html text input
s o开发者_运维知识库n 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.
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>
@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>
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.
精彩评论