0

I want to add an input into form using JQuery, and then submit the form with pure HTML without using javascript.

<form method="post" action="" id="myform" >
<input type="hidden" name="example" value="done" />
<input type="submit" value="try" />
</form>

Here my JQuery line that add new input:

$('#myform').append("<input name="example2" value="done" type='hidden'/>");

My issue is when i submit the form, i can't get example2 input value ! help me to figure it out.

Brave Type
  • 165
  • 1
  • 2
  • 7

5 Answers5

2

Seems like the input box failed to append at the end of the form.

You could try inspect element and check if the HTML for the hidden field is generated. Check the quotes:

$('#myform').append('<input name="example2" value="done" type="hidden"/>');
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
2

What you need to do is download jquery library or use the external google cdn library as the one i have used in my case in the head of your html file.

Ensure that you append the input on the form as the document loads. If you intend to submit your form to Php as the server side check if the form is posted and print the output. You should save your file with .php extension.

Try the solution below. Thanks

$(document).ready(function() {
        $("form#myform").append("<input name='example2' value='done' type='hidden'/>")
      })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <form method="post" action="" id="myform">
      <input type="hidden" name="example" value="done" />
      <input type="submit" value="try" />
    </form>
0

Your JQuery appears to use double quotes when single should be used:

$('#myform').append("<input name='example2' value='done' type='hidden'/>");
Nick
  • 16,066
  • 3
  • 16
  • 32
0

if you use "" at the start and at the end of the () , inside use '' . same if you use '' at the start and at the end of the () ,inside use ""

see more here : When to use double or single quotes in JavaScript?

or here : Single or Double quotes in jQuery

$('#myform').append("<input name='example2' value='done' type='hidden'>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="" id="myform" >
<input type="hidden" name="example" value="done" />
<input type="submit" value="try" />
</form>
Community
  • 1
  • 1
Mihai T
  • 17,254
  • 2
  • 23
  • 32
-1

What you need to do is download jQuery library or use the external google CDN library as the one I have used in my case in the head of your html file.

Ensure that you append the input on the form as the document loads. If you intend to submit your form to PHP as the server side check if the form is posted and print the output. You should save your file with .php extension.

Try the solution below.

$(document).ready(function() {
        $("form#myform").append("<input name='example2' value='done' type='hidden'/>")
      })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <form method="post" action="" id="myform">
      <input type="hidden" name="example" value="done" />
      <input type="submit" value="try" />
    </form>
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114