Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add additional rows then add these to database

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
Hi

Having a bit of trouble here, I saw a UI a while ago where a form started with one row, with the ability to add more on demand. It went a little like this

fullname | address | telephone | dropdown options | something else

add another row

The 'add another row' button created an exact row as above to enter the details of another person.

If I added 4 people, next time I loaded that page it would show the four rows.

I can use php to add one row at a time but how do I code it up so that the script works out how many rows have been created and completed then create new rows in the database?

Thank you for any help
 
Use javascript to add the form elements and append a incrementing number to the element names the loop through the form elements.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
There's quite a lot of coding involved in an interface like that. But the important thing is to understand what you need to do, and how each part of the interface needs to function to do what you want.

The basic structure would be composed of 3 distinct parts.

1. Create a new Row in the html page dynamically when a button is pressed.

2. Store new row data. Either when all fields have been filled or maybe when save button is clicked.

3. Load Previously saved data


Each one is an entity unto itself and does not depend on the others to function.

So the first thing is to build the new Row when requested. This would be done client side using Javascript or jQuery, so falls out of the scope of PHP. Basically you can use the JS functions createElement() and appendElement() to build the row elements you need.


Second would be to create an Ajax script that can save the new row's data when a new Row is created or when all rows have been filled in or something similar. The Ajax script would need to connect to a PHP script that would store the sent data in the DB.

And Lastly, you would need to make your PHP script output all rows it finds when the page is loaded.

Start with number 3, and work your way back.

The PHP side, is quite basic, as you would only be saving values to a DB, and reading from it when the page loads.

Remember PHP runs on the server, so all those interactive features rarely involve PHP directly.


1. JS/Jquery to build new ROW
2. AJAX script to get new ROW Data and send to PHP script to save to DB
3. PHP script to load DB data on page load.











----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you for the replies and pointers, I'm getting there.

With jquery I'm creating new rows, each input has the row number appended so after adding a couple of rows I have

fullname1 | address1 | telephone1 | dropdown-options1 | something-else1
fullname2 | address2 | telephone2 | dropdown-options2 | something-else2
fullname3 | address3 | telephone3 | dropdown-options3 | something-else3

When submitting form, I loop through number of submitted rows to insert.

for( $i = 1; $i <= $numberitems; $i++ )
{
$field = $_POST[field.$i];
$query = "insert INTO table (field) VALUES ($field)”;
mysql_query($query);
}

Problem is when updating say two rows and inserting another two. How does it know to update 2 and insert 2?
 
It doesn't. You need to check whether the rows existed already, or where added, and if they did exist, if anything was changed in them.

So basically you would have to compare the rows you have in your DB with the ones you are getting from the post.

Usually this would be accomplished by having a hidden value for each row be its ID. if it has an ID then you can run a query to get that row based on its id, and compare values. If anything changed perform an update.

For those without a valid ID, then perform an insert.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top