0

I am wondering if there is a way I can pass an updated PHP counting variable to an HTML form before submission. My PHP reads in a CSV file and generates an HTML table based on the number of rows in the CSV file. The counting variable then reflects the number of rows in the HTML table. Is there anyway I can update the HTML form "request form" with the PHP variable $formvar ?

   <?php
    $csvFile = $_POST['myfile'];
    $formvar =1;
    $row = 0;
    echo "<form id=\"requestform\" action=\"picklist-submit.php\" method=\"post\">";
    echo "<table>";
    echo "<input value=\"Submit\" type=\"submit\">";
    echo "<input type=\"hidden\" name=\"formvar\" value=\"$formvar\">";
    echo "<td>ISBN</td>";
    echo "<td>Quantity</td>";
    echo "<td>Comments</td>";
    echo "<td>Initials</td>";
    echo "</tr>";
if (($handle = fopen($csvFile, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
          $var = $data[0];
          $var2 = $data[1]; 
          $var3 = $data[2];
          $var4 = $data[3];  
          }
 echo "<tr>";
 echo "<td><input type=\"text\" name=\"upc1\" value=\"$var\"></td>";
 echo "<td><input type=\"text\" name=\"quantity1\" value=\"$var2\"></td>";
 echo "<td><input type=\"text\" name=\"comment1\" value=\"$var3\"></td>";
 echo "<td><input type=\"text\" name=\"initials1\" value=\"$var4\"></td>";
 $formvar++;
    } 
 echo "</tr></table>";
 echo "</form>";
 fclose($handle);
}

?>

2 Answers 2

3

After echo "</tr></table>";, add:

echo "<input type=\"hidden\" name=\"formvar\" value=\"$formvar\">";

Then when your form is submitted, you can retrieve the value from $_POST['formvar']

Sign up to request clarification or add additional context in comments.

Comments

0

You can add a field outside of the loop.

    $formvar++;
    } 
echo "<input type=\"hidden\" name=\"formvar\" value=\"".$formvar."\" />"; 
echo "</tr></table>";

You'll also want to remove the hidden field above the loop.

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.