I am trying to create a form dynamically via jquery and submit it to a PHP file. This creates the form but nothing happens when i click the submit button. What is going wrong here ?
Method i am using to create a form dynamically is :-
$('#share').append("<form action='sharer.php' method='POST'/>");
$('#share').append("<div class= 'appm'>Save this/div/>");
$('#share').append("<input type='text' placeholder='Name' name='routename' id='rname'/>");
$('#share').append("<input type='text' placeholder='description' id='rdescription' name='routedescription' class= 'address'/>");
$('#share').append("<input type='text' placeholder='tags' id='tags' name='routetags' />");
$('#share').append("<br><input type='submit' id='savebutton' value = 'Save' />");
$('#share').append("</form>");
Answer: Use the jQuery submit() Method You can use the submit() method to submit an HTML form (i.e. ) using jQuery. The jQuery code in the following example will submit the form on click of the button (i.e. the element) which has the type attribute set to button (i.e. type=”button” ).
Create Dynamic Controlsappend() event with the control in which you want to add some dynamic controls and in that append event you need to write the HTML code similar to what you might have written in the body section and this will work for you, for example: <script> $(document). ready(function () {
jQuery submit() MethodThe submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.
You are appending all the content that you add to the parent element, so they won't get inside the form itself. Way to fix this:
$("#share").append('<form action="sharer.php" method="POST">');
$("#share form").append('<div class="appm">Save this</div>');
$("#share form").append('<input type="text" placeholder="Name" name="routename" id="rname"/>');
$("#share form").append('<input type="text" placeholder="description" id="rdescription" name="routedescription" class="address"/>');
$("#share form").append('<input type="text" placeholder="tags" id="tags" name="routetags"/>');
$("#share form").append('<br><input type="submit" id="savebutton" value="Save" />');
You don't need to append a closing tag for the form after that.
(jsperf link up there)
If you really want a good performance solution, go for pure JavaScript code:
var div = document.getElementById('share');
var form = document.createElement('form');
form.setAttribute('action', 'sharer.php');
form.setAttribute('method', 'POST');
/*-----------*/
var appm = document.createElement('div');
appm.appendChild(document.createTextNode('Save This'));
appm.setAttribute('class', 'appm');
/*-----------*/
var input1 = document.createElement('input');
input1.setAttribute('type', 'text');
input1.setAttribute('placeholder', 'Name');
input1.setAttribute('name', 'routename');
input1.setAttribute('id', 'rname');
var input2 = document.createElement('input');
input2.setAttribute('type', 'text');
input2.setAttribute('placeholder', 'description');
input2.setAttribute('name', 'routedescription');
input2.setAttribute('id', 'rdescription');
input2.setAttribute('class', 'address');
var tags = document.createElement('input');
tags.setAttribute('type', 'text');
tags.setAttribute('placeholder', 'tags');
tags.setAttribute('name', 'routetags');
tags.setAttribute('id', 'tags');
var submit = document.createElement('input');
submit.setAttribute('type', 'submit');
submit.setAttribute("value", "Save");
submit.setAttribute('id', 'savebutton');
form.appendChild(input1);
form.appendChild(input2);
form.appendChild(tags);
form.appendChild(submit);
div.appendChild(form);
JQuery will never allow you to put a tag without closing it. So now, your code create a form and appen elements after the form (not inside).
But you can create a form, save it in a var and append your things into the var.
var form = $('<form/>', {action : 'sharer.php', method : 'POST'}).appendTo('#share');
form.append("<div class= 'appm'>Save this</div>");
form.append("<input type='text' placeholder='Name' name='routename' id='rname'/>");
form.append("<input type='text' placeholder='description' id='rdescription' name='routedescription' class= 'address'/>");
form.append("<input type='text' placeholder='tags' id='tags' name='routetags' />");
form.append("<br/><input type='submit' id='savebutton' value='Save' />");
This is also optimal since you are caching the form, not creating a jQuery object for each append!
Working fiddle : http://jsfiddle.net/Lb8BY/ (append to body)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With