I appreciate some help if anyone can tell me how we can generate HTML form dynamically based on any JSON object using javascript?
-
1This question is too broad to even answer. Definitely need more information than thisCoder– Coder2017-07-14 17:10:53 +00:00Commented Jul 14, 2017 at 17:10
-
may i know what more info you require?naveen kumar– naveen kumar2017-07-14 17:16:19 +00:00Commented Jul 14, 2017 at 17:16
-
what will be the contents of the JSON? what goes into the body? which value from JSON goes to which tag? These are the questions to start withCoder– Coder2017-07-14 17:17:53 +00:00Commented Jul 14, 2017 at 17:17
-
the contents will be multiple objects containing data type,data name and data itself.naveen kumar– naveen kumar2017-07-14 17:24:14 +00:00Commented Jul 14, 2017 at 17:24
-
That just explains the contents of any objects but not how it should be mapped to HTML contentCoder– Coder2017-07-14 18:36:11 +00:00Commented Jul 14, 2017 at 18:36
|
Show 1 more comment
2 Answers
try this seee
You can use an existing library for that.
A simple one is: Angular Dynamic Forms
More standard way is the JSON Schema with this nice implementation Angular Schema Form. See examples on how it works here DEMO
Comments
HTML:
<html>
<head></head>
<body>
<body>
</html>
javascript:
<script>
//create a form
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
//create input element
var i = document.createElement("input");
i.type = "text";
i.name = "user_name";
i.id = "user_name1";
//create a checkbox
var c = document.createElement("input");
c.type = "checkbox";
c.id = "checkbox1";
c.name = "check1";
//create a button
var s = document.createElement("input");
s.type = "submit";
s.value = "Submit";
// add all elements to the form
f.appendChild(i);
f.appendChild(c);
f.appendChild(s);
// add the form inside the body
$("body").append(f); //using jQuery or
document.getElementsByTagName('body')[0].appendChild(f); //pure javascript
</script>
you can create as many elements as you want dynamically.
1 Comment
naveen kumar
why a php file?