0

i am trying to get the data from my form using loop. The following code does not work

HTML code :-

<tr  name="row_1" id="row_1">
    <td align="center">1</td>
    <td><input type="text" class="SText" contenteditable="true" value="Basic" name="SText_1" id="SText_1" /></td>
    <td><input type="text" class="SAmount" contenteditable="true" value="6000.00" name="SAmount_1" id="SAmount_1" /></td>
</tr>
<tr  name="row_2" id="row_2">
    <td align="center">2</td>
    <td><input type="text" class="SText" contenteditable="true" value="Housing" name="SText_2" id="SText_2" /></td>
    <td><input type="text" class="SAmount" contenteditable="true" value="5000.00" name="SAmount_2" id="SAmount_2" /></td>
</tr>

PHP code :-

<?php
$i=1;
while ($i<=10) {
   $sText_$i = $_POST['SText_$i'];
   $sAmount_$i =  $_POST['SAmount_$i'];

   //$sText_$i = $_POST['SText_'] . $i ; // dont work either
   //$sAmount_$i =  $_POST['SAmount_'] .$i;
$i++;
}
?>
1
  • 1
    $length = sizeof($_POST["SText"]); for($i=0; $i<$length; $i++) { echo $_POST['SText'][$i]; echo $_POST['SAmount'][$i]; } Commented Mar 14, 2017 at 7:57

1 Answer 1

4

PHP uses the square bracket syntax to convert form inputs into an array

HTML code :-

<tr  name="row_1" id="row_1">
    <td align="center">1</td>
    <td><input type="text" class="SText" contenteditable="true" value="Basic" name="SText[]" id="SText_1" /></td>
    <td><input type="text" class="SAmount" contenteditable="true" value="6000.00" name="SAmount[]" id="SAmount_1" /></td>
</tr>
<tr  name="row_2" id="row_2">
    <td align="center">2</td>
    <td><input type="text" class="SText" contenteditable="true" value="Housing" name="SText[]" id="SText_2" /></td>
    <td><input type="text" class="SAmount" contenteditable="true" value="5000.00" name="SAmount[]" id="SAmount_2" /></td>
</tr>

PHP Code :-

<?php
echo "<pre>";
print_r($_POST["SText"]);
print_r($_POST["SAmount"]);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

how will i get this with php loop ?
This print_r will gives you all SText and SAmount into a array , so you can use simple foreach loop to get single value ..
First add form before start table then lastly submit it .. and you get values .

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.