1
 <html>
<head>

   
               

</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 dynamic please help me with this.

3
  • What does your current code not do which you need it to do. What efforts have you made to get it to work and the specific problems you have with it? Commented Aug 15, 2020 at 9:31
  • now i have placed the php code help me with this @NigelRen Commented Aug 15, 2020 at 10:55
  • dcblog.dev/dynamic-form-elements-with-jquery Commented Aug 15, 2020 at 11:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer it was really helpful finally succeeded after lot of efforts :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.