Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:57:45+00:00 2026-06-17T16:57:45+00:00

hi i am working on a php form which has to add a table

  • 0

hi i am working on a php form which has to add a table row dynamically when add button is pressed and i am using for loop to save the values the problem is that it is not saving the data into my database and gives the error that the loop values of my textbox is undefined
can anyone help me
here is my script

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript" type="text/javascript">
var i=1;
function addRow()
{
          var tbl = document.getElementById('table1');
          var lastRow = tbl.rows.length;
          var iteration = lastRow - 1;
          var row = tbl.insertRow(lastRow);

          var firstCell = row.insertCell(0);
          var el = document.createElement('input');
          el.type = 'text';
          el.name = 'name' + i;
          el.id = 'name' + i;
          el.size = 20;
          el.maxlength = 20;
          firstCell.appendChild(el);

          var secondCell = row.insertCell(1);
          var el2 = document.createElement('input');
          el2.type = 'text';
          el2.name = 'address' + i;
          el2.id = 'address' + i;
          el2.size = 20;
          el2.maxlength = 20;
          secondCell.appendChild(el2);

          var thirdCell = row.insertCell(2);
          var el3 = document.createElement('input');
          el3.type = 'text';
          el3.name = 'contactNum' + i;
          el3.id = 'contactNum' + i;
          el3.size = 20;
          el3.maxlength = 20;
          thirdCell.appendChild(el3);
          alert(i);
          i++;
          frm.h.value=i;
          alert(i);

}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title></head>

<body>
<form action="submit.php" method="post" name="frm" id="frm">
<table width="40%" border="2" cellpadding="0" cellspacing="0" id="table1">
  <tr>
    <td><strong>Name</strong></td>
    <td><strong>Address</strong> </td>
    <td><strong>Contact Num</strong> </td>
  </tr>
  <tr>
    <td><input name="name" type="text" id="name" size="20" maxlength="20" /></td>
    <td><input name="address" type="text" id="address" size="20" maxlength="20" /></td>
    <td><input name="contactNum" type="text" id="contactNum" size="20" maxlength="12" /></td>
  </tr>

</table>
<input type="button" value="Add" onclick="addRow();" />
<input name="Submit" type="submit" value="Submit" />
<label>
<input name="h" type="hidden" id="h" value="0" />
</label>
</form>
</body>
</html>

hereis my submit.php code

<?php
mysql_connect("localhost", "root", '')or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$num =  $_POST['h'];
for($i=0;$i<=$num;$i++)
{

        $name       = $_POST["name_$i"];
        $address    = $_POST["address_$i"];
        $contactNum = $_POST["contactNum_$i"];

mysql_query("INSERT INTO `com`(`name`, `add`, contact) Values('$name', '$address', '$contactNum')") or die(mysql_error());
}
echo "<h1>Done!</h1>";

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:57:46+00:00Added an answer on June 17, 2026 at 4:57 pm

    Below code will solve your problem and insert data in database.

    <script language="javascript" type="text/javascript">
    var i=1;
    function addRow()
    {
          var tbl = document.getElementById('table1');
          var lastRow = tbl.rows.length;
          var iteration = lastRow - 1;
          var row = tbl.insertRow(lastRow);
    
          var firstCell = row.insertCell(0);
          var el = document.createElement('input');
          el.type = 'text';
          el.name = 'name_' + i;
          el.id = 'name_' + i;
          el.size = 20;
          el.maxlength = 20;
          firstCell.appendChild(el);
    
          var secondCell = row.insertCell(1);
          var el2 = document.createElement('input');
          el2.type = 'text';
          el2.name = 'address_' + i;
          el2.id = 'address_' + i;
          el2.size = 20;
          el2.maxlength = 20;
          secondCell.appendChild(el2);
    
          var thirdCell = row.insertCell(2);
          var el3 = document.createElement('input');
          el3.type = 'text';
          el3.name = 'contactNum_' + i;
          el3.id = 'contactNum_' + i;
          el3.size = 20;
          el3.maxlength = 20;
          thirdCell.appendChild(el3);
         // alert(i);
          i++;
          frm.h.value=i;
        //  alert(i);
    }
    </script>
    

    In make change in according to below :

    <tr>
        <td><input name="name_0" type="text" id="name_0" size="20" maxlength="20" /></td>
        <td><input name="address_0" type="text" id="address_0" size="20" maxlength="20" /></td>
        <td><input name="contactNum_0" type="text" id="contactNum_0" size="20" maxlength="12" /></td>
    </tr>
    

    In submit.php made change like below.

    for($i=0;$i<$num;$i++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a PHP form that allows a user to add rows
I am working on a signup form, I am using PHP and on my
I have a PHP form for discussions. Each message has its own response button,
I'm working with PHP, and I'm making an action page which a form posts
I'm trying to add ajax form submission to a PHP web app I'm working
I am working on a webpage which has a comment box using which a
I have a table in which every row has an option to upload a
I have simple php validation form that is halfway working. If you leave the
I am working on a php program that requires javascript on every form control.
I have an email attachment form that is working fine when the <form action=processingtheformfile.php>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.