Home:ALL Converter>Fill second form hidden fields with first form values (HTML, Javascript)

Fill second form hidden fields with first form values (HTML, Javascript)

Ask Time:2016-04-14T11:14:19         Author:SocialenVy

Json Formatter

I'm trying to add hidden fields in the second form and fill these fields (using java script) with the first form values before submission.

First form:

                    <form id="contactform" name="cform" class="contact-form clearfix form oswald normal antialiased" method="post" action="./contact/includes/contact-processs.php">
                    <div class="fullwidth clearfix">
                        <div class="f-left">
                            <input type="text" name="name" id="name" placeholder="Name">
                        </div>
                        <div class="f-right">
                            <input type="email" name="email" id="email" placeholder="E-Mail">
                        </div>
                    </div>
                    <textarea name="message" id="message" placeholder="Hashtags / Target Audience"></textarea>
                    <div class="note"></div>
                    <button type="submit" id="submit" class="darkertext" name="submit" onclick="loading()">GET STARTED</button>
                </form>

Second Form:

Author:SocialenVy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36613227/fill-second-form-hidden-fields-with-first-form-values-html-javascript
Girdhari Agrawal :

May be this is not the best solution but you can try this.\n\n<!--Form 1-->\n<form onsubmit=\"fill()\">\n <input type=\"text\" id=\"name\" name=\"name\">\n <input type=\"submit\">\n</form>\n\n<!-- Form 2-->\n<form>\n <input type=\"hidden\" id=\"nameHidden\">\n</form>\n\n//Javascript code\nfunction fill() {\n var element1 = document.getElementById(\"nameHidden\");\n var element2 = document.getElementById(\"name\");\n element1.value = element2.value;\n}\n",
2016-04-14T03:27:23
Munawir :

Use addEventListener(\"change\", function(){...} to listen to input change and fill the other form on input value change.\n\nDemo :\n\n\r\n\r\npaypalform.name.addEventListener(\"change\", function(){\r\n contactform.name.value = paypalform.name.value;\r\n});\r\npaypalform.email.addEventListener(\"change\", function(){\r\n contactform.email.value = paypalform.email.value;\r\n});\r\npaypalform.message.addEventListener(\"change\", function(){\r\n contactform.message.value = paypalform.message.value;\r\n});\r\n<form id=\"paypalform\" name=\"cform\" class=\"contact-form clearfix form oswald normal antialiased\" method=\"post\" action=\"./contact/includes/contact-processs.php\">\r\n <div class=\"fullwidth clearfix\">\r\n <div class=\"f-left\">\r\n <input type=\"text\" name=\"name\" id=\"name\" placeholder=\"Name\">\r\n </div>\r\n <div class=\"f-right\">\r\n <input type=\"email\" name=\"email\" id=\"email\" placeholder=\"E-Mail\">\r\n </div>\r\n </div>\r\n <textarea name=\"message\" id=\"message\" placeholder=\"Hashtags / Target Audience\"></textarea>\r\n <div class=\"note\"></div>\r\n <button type=\"submit\" id=\"submit\" class=\"darkertext\" name=\"submit\" onclick=\"loading()\">GET STARTED</button>\r\n </form>\r\n\r\n<!---------Second Form---------->\r\n<form id=\"contactform\" name=\"cform\" class=\"contact-form clearfix form oswald normal antialiased\" method=\"post\" action=\"./contact/includes/contact-processs.php\">\r\n <div class=\"fullwidth clearfix\">\r\n <div class=\"f-left\">\r\n <input type=\"text\" name=\"name\" id=\"name\" placeholder=\"Name\">\r\n </div>\r\n <div class=\"f-right\">\r\n <input type=\"email\" name=\"email\" id=\"email\" placeholder=\"E-Mail\">\r\n </div>\r\n </div>\r\n <textarea name=\"message\" id=\"message\" placeholder=\"Hashtags / Target Audience\"></textarea>\r\n <div class=\"note\"></div>\r\n <button type=\"submit\" id=\"submit\" class=\"darkertext\" name=\"submit\" onclick=\"loading()\">GET STARTED</button>\r\n </form>",
2016-04-14T03:39:13
yy