Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two forms share same input

Tags:

html

forms

input

How can two forms share the same inputs? I have two forms, one for two different paths... if i wanted to get the user to enter their name, but only once.... how could both forms get hold on this field?

like image 284
tarnfeld Avatar asked Nov 07 '09 09:11

tarnfeld


2 Answers

In HTML5 you could do it without JavaScript. Do one form with one set of inputs, and just make two submit buttons using a different formaction attributes found on Mozilla Developer Network like this:

<form>
    <input name="fname" type="text" />

    <input type="submit" value="button one" formaction="script_one.php">
    <input type="submit" value="button two" formaction="script_two.php">
</form>
like image 65
Ilia Gilmijarow Avatar answered Oct 11 '22 15:10

Ilia Gilmijarow


Using JavaScript you could set one field's value to another's.

Something like:

document.form2.input.value = this.value;

Put that code in the onblur event for your first form.

So:

<form name="form1">
<input type="text" name="input" onblur="document.form2.input.value = this.value;" />
</form>

<form name="form2">
<input type="text" name="input" onblur="document.form1.input.value = this.value;" />
</form>
like image 39
Waleed Amjad Avatar answered Oct 11 '22 13:10

Waleed Amjad