Receiving Dynamic Textbox Data

You should not use this code on a production website.

Warning: This tutorial uses old techniques. It is insecure and will leave your server vulnerable to SQL Injection attacks.This tutorials also uses mysql_ functions that are no longer support. For updated tutorials look for a PDO or MySQLi tutorial.This post will be delete or revised in the future.

In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial. We will collect the data and insert the values into a database. To start we need to make sure our file that we are using the make this data is called insert.php (as we named it in the last tutorial.) This file will be called when you click the “Add Invoice” button.


$conn = mysql_connect ("localhost","username","password") or die 
('cannot connect to database error: '.mysql_error());
mysql_select_db ('test');

This starts the php and makes the connection to the database so that we can insert the values.

$quantity = $_POST['quantity'];
$desc = $_POST['desc'];
$itemno = $_POST['itemno'];
$ourcost = $_POST['ourcost'];
$cost = $_POST['cost'];
$distid = $_POST['distid'];
$invoice = $_POST['invoice'];
$integer = 0;

This declares all of our POST variables (the names of the textboxes in the form from the last page). Notice the additioni variable that we called integer. We are going to use this to go through all the results. This sets each php variable equal to the array of values we set on the last page. We can set which variable we are trying to use by defining the number of the array we would like to grab. For example:

$quantity[0] – Quantity of first item.
$quantity[1] – Quantity of second item.

while (count($quantity)>$integer) {
if (($distid[$integer] <> "") && ($desc[$integer] <> "") && ($quantity[$integer] <> "")&& ($cost[$integer] <> "") && ($itemno[$integer] <> "") && ($ourcost[$integer] <> "")){
$sql =  "INSERT INTO `test`.`item` (`InvoiceNo`, `DistID`, `ItemDesc`, `ItemQuantity`, `ItemCost`, `ItemNo`, `OurCost`) 
	VALUES ('".$invoice."', '".$distid[$integer]."', '".$desc[$integer]."', '".$quantity[$integer]."', '".$cost[$integer]."', '".$itemno[$integer]."', '".$ourcost[$integer]."')";

This section of code looks worse than it actually is. The function it is doing is actually relatively simple. The first line means that the system will do every thing after it until the amount of values in the array $quantity is greater than the value we set ($integer). The count function simply counts the number of values in an array. So if we insert 2 items this section will run until the value of $integer passes the count of $quantity. The second line checks all the variables for values and runs the insert query if none are blank. Notice that we are inserting $integer instead of a number so that this number can be incremented every time it is run. So on the first pass of the code:

$quantity[$integer] = $quantity[0]

echo "$quantity[$integer] $desc[$integer](s) bought from Distribution Center $distid[$integer] 
have/has been added to the database with an Item Number of $itemno[$integer] 
and a customer cost of $cost[$integer] and a company cost of $ourcost[$integer]<br />";

This line simply echoes the values of all the variables into a sentence so we can see it was inserted into the database.

echo "Item number ".($integer+1)." is missing values and cannot be inserted.";

The first bracket end the if statement. Everything up until that point would have been run if the statement were true. The next line starts the else statement. This will be run if one of the fields on the last page was blank. It will echo the statement above. Notice that we echoes the value of integer plus one since the integer starts on 0 instead of 1.

$integer = ($integer + 1);

Finally we increment our variable and then end the while statement. Once the variable is incremented it will try the while statement again. If it passes, the section of code will be run again but this time the value of our integer will be higher by one number. Finally we end our PHP.

This concludes writing the receiving file.


Let’s open our original file and insert two items with the above information. Notice the blank item number.


Your screen should look like this if you did it exactly the way I did. This means that the first item we inserted was successfully put into the database, but the second one was not since there was no item number.

This concludes this tutorial. I hope it is helpful and you were able to follow it. Thanks for reading.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join the TeamTutorials mailing list and get the latest tips, tricks, and special discounts for members only.

Thanks! Please click the link in the email we just sent you to confirm.

  • Pingback: Receiving Dynamic Textbox Data tutorial description - Blue Box Sols()

  • Thank you a lot! Really helpful.

  • SaraDob

    thanks for the Previous tutorial on dynamically adding was very helpful.but if it not too much for you,
    can you post this code of inserting to db using and SQL
    I tried to do this..but still confusing
    Please post the and SQl

  • sumathy

    i have benefited from your previous tutorial on dynamically generating textboxes using js.But while trying to pass the values to my jsp using request.getParameter i am getting a null.

  • mamaw


  • can you send the link for download this

  • muchas gracias por el tutorial!!
    tá bien chido, ca. de poca!

  • coi

    Hi, just a small little question. Now that we are able to add the rows.. Is there any way we can delete the rows as well??


  • satish

    ya nice code

  • Samkinikar

    How to validate the text boxes ? i.e numeric and alpha numeric ?