1

I have a jQuery function to dynamically add rows containing input fields, this exists on a form, the function adds text boxes (distinct names) to each cell in the table

an example of the generated HTML would be:

<table width="400" border="0" cellspacing="0" cellpadding="2px" margin="0" >
    <tbody>
    <tr>
        <td><input type="text" name="name_1"></td>
        <td><input type="text" name="surname_1"></td>
        <td><input type="text" name="age_1"></td>
    </tr>
    <tr>
        <td><input type="text" name="name_2"></td>
        <td><input type="text" name="surname_2"></td>
        <td><input type="text" name="age_2"></td>
    </tr>
    <tr>
        <td><input type="text" name="name_3"></td>
        <td><input type="text" name="surname_3"></td>
        <td><input type="text" name="age_3"></td>
    </tr>
    </tbody>

I am reading the "$_POST['varName'] data with the following code:

<?php
    $cnt = 1 ;
    $fName = ( $name ."_" .$cnt) ;
    do {
        echo("$_POST[$fName]  <br>");
        $cnt = $cnt +1 ; 
        $fName = ( $fldName ."_" .$cnt) ;

        } while (isset($_POST[$fName]));
    ?>

however, i would like to simply loop through each row in the table and read the data sequentially in a loop (using PHP), my idea is to pass the table object to a php function, is this possible?

Basically i am looking for a solution to read the table data, where each row contains input boxes "name_X", "surname_x" and "age_x" and i will not know how many rows exist at design time... (i will never have more than 9 rows)

Hope this is clear! ... Any Suggestions?

3
  • You cannot access the HTML table itself from PHP if that's what you're suggesting. Only the form field values get posted to your PHP script. Commented Apr 18, 2012 at 19:27
  • 1
    Isn't the code that you posted doing exactly what you are asking? Commented Apr 18, 2012 at 19:29
  • "my idea is to pass the table object to a php function". The table is not a PHP object, it is HTML which is just markup. Commented Apr 18, 2012 at 19:34

3 Answers 3

4

i'm pretty sure you must use an array in the name="" values

<td><input type="text" name="name[]"></td>
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a hidden field and set there the number of rows as value using javascript. In the php script you can then use a simple for loop.

Simething like this:

$rowCount = $_POST['hiddenName'];
for($i=1; i<=$rowCount; $i++)
{

  $_POST['name_'.$i]
  ...
}

1 Comment

Great!, the answer above solves the problem in a different way, however i think this is a solution to the current question!. Thanks for your response!
0

I Used both the name array and the simple for loop to find a suitable answer. Thanks All!

HTML: I have a js function that adds the following to a table dynamically (Button on form)

<tr>
        <td><input type="text" name="name[]" ></td>
        <td><input type="text" name="surname[]" ></td>
        <td><input type="text" name="age[]"> </td>
    </tr>

PHP: and this is the way that i process the table within the form

<?php  
$rowCount =  count($_POST['name']); 
    echo "<table>";
    for($i=1; $i<=$rowCount; $i++)
        {
        echo "<tr>";
        echo "<td>".$_POST['name'][$i -1]."</td>";
        echo "<td>".$_POST['surname'][$i -1]."</td>";
        echo "<td>".$_POST['age'][$i -1]."</td>";
        echo "</tr>";
        }
    echo "</table>";?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.