The question is published on by Tutorial Guruji team.
<html> <head> <script type='text/javascript'> j=1; function addFields(){ // Number of inputs to create var number = document.getElementById("member").value; // Container <div> where dynamic content will be placed var container = document.getElementById("container"); // Clear previous contents of the container while (container.hasChildNodes()) { container.removeChild(container.lastChild); } for (i=0;i<number;i++){ // Append a node with a random text container.appendChild(document.createTextNode("Member name ")); // Create an <input> element, set its type and name attributes var input = document.createElement("input"); input.type = "text"; input.name = "member_name" + i; container.appendChild(input); // Append a line break container.appendChild(document.createTextNode("Member age ")); var input = document.createElement("input"); input.type = "text"; input.name = "member_age" + i; container.appendChild(input); // Append a line break container.appendChild(document.createTextNode("Member occupation")); var input = document.createElement("input"); input.type = "text"; input.name = "member_occupation" + i; container.appendChild(input); // Append a line break container.appendChild(document.createTextNode("Member education")); var input = document.createElement("input"); input.type = "text"; input.name ="member_education" + i; container.appendChild(input); // Append a line break container.appendChild(document.createTextNode("Member place")); var input = document.createElement("input"); input.type = "text"; input.name = "member_place" + i; container.appendChild(input); // Append a line break container.appendChild(document.createElement("br")); } } </script> </head> <body> <a href="#" id="filldetails" onfocusout="addFields()"><input type="text" id="member" name="member" value="">Number of members: (max. 10)<br /> Fill Details</a> <form class="contact-form row" method="post" action="test.php" enctype="multipart/form-data"> <div id="container"></div> <div class="form-field col-lg-12"> <input class="submit-btn" type="submit" value="Submit" name="submit"> </div> </form> </body> <?php include('config.php'); $i=0; if(isset($_POST['submit'])) { $name = $_POST['member_name'.$i.'']; $sql="INSERT INTO `ssk`.`members` (`member_name`) VALUES ('$name')"; $query=mysql_query($sql,$con); if($query) { echo '<div class="alert alert-success"> <strong>form submitted successfully your member-id is '.$member_id.' </strong> </div>';?> <script> alert('Thank you! Your form has been submitted and is being verified please note-down the member-id.'); </script> <?php }else{ echo '<div class="alert alert-success"> <strong>unsuccessful please read the above data and fill the form accordingly.</strong> </div>'; ?> <script> alert('ERROR:Request unprocessed'); </script> <?php } } ?> </html> what i want is to have a form which accepts the number of fields to be generated after which the user has to fill the generated input fields and the data has to be stored in the database is their any alternate way of doing this using php & mysql its taking entry of first data bur not working dynamica please help me with this thank you:)
what i want is to have a form which accepts the number of fields to be generated after which the user has to fill the generated input fields and the data has to be stored in the database is their any alternate way of doing this using php & mysql its taking entry of first data bur not working dynamica please help me with this
Answer
Using the structure you already have you could simply put a while loop in the php:
if(isset($_POST['submit'])) { while(isset($_POST['member_name'.$i])) { $name = $_POST['member_name'.$i.'']; $sql = "INSERT INTO `ssk`.`members` (`member_name`) VALUES ('$name')"; //rest of the code here //increment $i $i++; } }
You could also have used arrays for the form inputs e.g. input.name = "member_name[]"
and then the loop in php could have have been foreach($_POST['member_name'] as $i=>$name)
.
Please also look into validating the post variables and properly preparing sql statements so you are not open to sql injection or problems if the name contained a quote.