karthikjayraj Posted October 13, 2010 Share Posted October 13, 2010 Hi, I am fairly new to PHP and I have built a page which accepts values from dynamically added rows so there is a combination of HTML,Javascript and PHP in the process. I am caught up with a problem HTML section In my form I have a table with a column as below and a dynamic "Add Row" which would increment the row in javascript and assign a new value to the input name as txtRow2,txtRow3,txtRow4 and so on based on the number of iteration " <tr><td><input type="text" name="txtRow1" id="txtRow1" size="40" /></td></tr> <input type="button" value="Add Row" onclick="addRow();" />" Javascript section The below code generates the new row in js var cellRight2 = row.insertCell(1); var e1 = document.createElement('input'); e1.type = 'text'; e1.name = 'txtRow1' + iteration; e1.id = 'txtRow1' + iteration; e1.size = 20; cellRight1.appendChild(e1); I hope you have understood what I have done till above.. Now comes the problematic part PHP section( I love PHP ) I have to insert the column values into mysql and i will do this using a for loop within which I will have my insert statement INSERT INTO test values($txtRow) Lets say I added 4 rows dynamically with values held in $txtRow1,$txtRow2,$txtRow3,$txtRow4(See the java script section) how would I use these values in my PHP insert statement as I don't want to write 4 different insert scripts and the following "$txtRow.i" won't work for ($i = 1; $i <= 10; $i++) { INSERT INTO test values($txtRow.i) } Any suggestions are welcome...If you think you still need to see the whole code i can post it too. Jay Quote Link to comment https://forums.phpfreaks.com/topic/215745-how-to-read-values-from-post/ Share on other sites More sharing options...
litebearer Posted October 13, 2010 Share Posted October 13, 2010 yes the whole code will help Quote Link to comment https://forums.phpfreaks.com/topic/215745-how-to-read-values-from-post/#findComment-1121731 Share on other sites More sharing options...
Zane Posted October 13, 2010 Share Posted October 13, 2010 Instead of having incrementing dynamic numbers for a name, you can use an array instead. This way, on the PHP side you can use the implode() function to create your columns list for your SQL. For visual help, ... look below Change this to this And change your Javascript, accordingly... like this var cellRight2 = row.insertCell(1); var e1 = document.createElement('input'); e1.type = 'text'; e1.name = 'txtRow[]'; e1.class = 'txtRow' e1.size = 20; cellRight1.appendChild(e1); But as far as adding multitudes of rows in your SQL, you're probably better off doing multiple INSERT statements. For instance, lets say someone adds 30+ new inputs. If you use only one INSERT statement for that, this assumes you have columns reserved for every permutation of added inputs; which is a horrible table layout btw. Don't fret either, multiple INSERT statements doesn't mean multiple mysql_query calls. You can append each new INSERT statement to one single variable and use that on one query.. as INSERTs, UPDATEs and DELETEs are designed this way. Quote Link to comment https://forums.phpfreaks.com/topic/215745-how-to-read-values-from-post/#findComment-1121735 Share on other sites More sharing options...
karthikjayraj Posted October 13, 2010 Author Share Posted October 13, 2010 Thanks Zanus..That worked Quote Link to comment https://forums.phpfreaks.com/topic/215745-how-to-read-values-from-post/#findComment-1121847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.