How To

How to Add Unlimited Fields in Form Using JavaScript and Store Into Database With PHP

Sometimes we need to add unlimited fields into our form for a better user experience. At that time, we can put an ADD FIELD button into the page for dynamic field generation on a webpage.

Creating unlimited fields in the form is effortless using JavaScript and jQuery. Also, saving the data into a database is easy to handle with PHP.

Adding Unlimited Fields Using JS and Saving Using PHP

Here we will use JavaScript to add new fields and PHP to save data into the database.

Step 1: Create a form and add a function to add new dynamic fields

<!DOCTYPE html>
<html>
 <body>
  <form id="form" method="POST" action="save_data.php">
   <input type="text" name="text_field[]">
   <button type="submit">SUBMIT</button>
  </form>
  <button >

Explanation

Here we created a function named add_field to append a text field at the second last position in the form.

Note: Here, we set every field name as text_field[], which submits all text field data as an array.

Read Also: How to Remove On-Page Errors in PHP

Step 2: Create a save_data.php file to store the data in the database

<?php
    $conn = mysqli_connect("localhost","USER_NAME","PASSWORD","DATABASE_NAME");
    
    $data = $_POST['text_field'];
    $data = implode(",",$data);
    
    $query = "INSERT INTO `test`(`data`) VALUES ('$data')";
    if(mysqli_query($conn,$query)){
        echo "Success: Data successfully inserted";
    }else{
        echo "Error: Could not insert data into table";
    }
?>

Explanation

  • Here we first stored the submitted array data in a PHP array variable named $data.
  • After that, we used an inbuilt PHP function implode(), which converts an array into a PHP string.
  • Finally, we saved our string data to our data table.

Conclusion

Creating unlimited dynamic fields is very easy using JavaScript. Even more, we learned a way to store that dynamic data into a database using some simple functions of PHP.

I hope now you have a complete understanding of dynamic field insertion and data storing techniques.

Enjoy Coding 🙂

Nachiket Panchal

Founder & Administrator of `errorsea` Having interest in Programming & Technology.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

5 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago