I'm trying to create form that send data to servlet.
Here im adding some input box dynamically on Click of button. But whenever i enter data and click new "add data" , previous data get lost and it does not appear in input box.
Screen shot of form after clicking Add Data
I'll have to create it dynamically. But when I click on add data of add content the data fields values the i have already filled diappears as shown in the screenshot. How to save the data there? And then how to send the whole form data to the servlet on click of submit button? code of html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
function add_fields() {
console.log(document.getElementById('wrapper').innerHTML);
document.getElementById('wrapper').innerHTML += "<br /><span>Step Number: <input name='step' type='number'style='width:75px;'value='' /></span> <span> Add Content: <input type='button' onClick='addContent()' value='Add Content' /></span>";
console.log(document.getElementById('wrapper').innerHTML);
}
function addContent(){
console.log(document.getElementById('wrapper').innerHTML);
document.getElementById('wrapper').innerHTML +="<br /><span><input name='contentimage' type='text' placeholder='enter image url' /></span><span><input name='contentmessage' type='text' placeholder='enter message' /></span><span><input name='alert' type='text' placeholder='enter alert' /></span>";
console.log(document.getElementById('wrapper').innerHTML);
}
</script>
<style>
.modal-header, h4, .close {
background-color: #5cb85c;
color:white !important;
text-align: center;
font-size: 30px;
}
.modal-footer {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-default btn-lg" id="myBtn">Share Solutions</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4>Add Solutions</h4>
</div>
<div class="modal-body" style="padding:40px 50px;">
<form role="form">
<div class="form-group">
<label for="cost">Cost</label>
<input type="text" class="form-control" name="cost" id="cost" placeholder="Enter cost">
</div>
<div class="form-group">
<label for="difficulty">Difficulty</label>
<input type="text" class="form-control" name="difficulty" id="difficulty" placeholder="Enter Difficulty">
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="text" class="form-control" name="image" id="image" placeholder="Enter Image Url">
</div>
<div class="form-group">
<label for="message">Message</label>
<input type="text" class="form-control" name="message" id="message" placeholder="Enter Message">
</div>
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" id="title" placeholder="Enter Title">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" id="description" placeholder="Enter Description">
</div>
<div class="form-group" id="wrapper">
<label for="description">Data</label> <button class="btn btn-success btn-block" type="submit" onClick="add_fields()"> Add Data</button>
</div>
<div>
</div>
<button type="submit" class="btn btn-success btn-block">Submit</button>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal" onClick="window.location.reload()"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$("#myBtn").click(function(){
$("#myModal").modal();
});
});
</script>
</body>
</html>