Jump to content

How to Read Values from $POST[]


karthikjayraj

Recommended Posts

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.. :shrug:

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.