AghaKhan
AghaKhan

Reputation: 74

Posting variable values from inside HTML forms inside PHP loop

How can i post variable values from inside HTML form which is inside PHP loop.

A snippet of code I've written so far is below:

 while($row=oci_fetch_array($sql))
              {

        echo "
    <body>
        <fieldset style=\"  box-shadow: 1px 1px 10px 1px #C4C4C4; 
                            border:0; 
                            width:420px;
                            height:125px;\">
    <form action=\"buffer.php\" method=\"POST\">
    <strong>Code:</strong> <input type=\" text\" value=\" $row[0]\" name=\" code\" disabled ><br>
    <strong>Course Name:</strong> <input type=\" text\" value=\" $row[1]\" name=\" namec\" disabled><br>
    <strong>Credit:</strong> <input type=\" text\" value=\" $row[2]\" name=\" credit\" disabled ><br>
    <strong>Section:</strong> <input type=\" text\" value=\" $row[3]\" name=\" section\"disabled ><br>
    <input type=\"submit\" value=\"Add Assesment \" name=\"addasmnt\"><input type=\"submit\" value=\"Edit Attendance \" name=\"editasgmnt\">
    </br> </fieldset> </form> </body>
        ";
           $i=$i+1;          
            }

While in 'buffer.php';

<?php
session_start();
    $roll= $_SESSION['roll'];
    print_r($_SESSION);echo "<br>";
    print_r($_POST);echo "<br>";
    print_r($_GET);echo "<br>";
    print_r($_REQUEST);
?>

The output of buffer.php is

Array ( [roll] => hammad.hassan ) 
Array ( [addasmnt] => Add Assesment ) 
Array ( ) 
Array ( [addasmnt] => Add Assesment )

The $_POST is not showing any variables.

Upvotes: 0

Views: 838

Answers (1)

Rene Korss
Rene Korss

Reputation: 5484

They are not getting POSTed because inputs are disabled.

As W3C states in here:

Disabled controls cannot be successful.

First thing what user agent does in processing form data is:

Step one: Identify the successful controls

But as said above, disabled elements wont be on this list.

Three options:

  1. Remove disabled attribute
  2. Use readonly="readonly" instead of disabled
  3. Add <input type="hidden"> with same values and names. Then change original inputs names to something else.

Hidden inputs example

<?php
echo '
<body>
  <fieldset style="box-shadow: 1px 1px 10px 1px #C4C4C4; border:0; width:420px; height:125px;">
    <form action="buffer.php" method="POST">
      <strong>Code:</strong> <input type="text" value="'.$row[0].'" name="code_dummy" disabled><br>
      <strong>Course Name:</strong> <input type="text" value="'.$row[1].'" name="namec_dummy" disabled><br>
      <strong>Credit:</strong> <input type="text" value="'.$row[2].'" name="credit_dummy" disabled><br>
      <strong>Section:</strong> <input type="text" value="'.$row[3].'" name="section_dummy"disabled><br>
      <input type="submit" value="Add Assesment" name="addasmnt"><input type="submit" value="Edit Attendance" name="editasgmnt">
      </br>
      <input type="hidden" name="code" value="'.$row[0].'">
      <input type="hidden" name="namec" value="'.$row[1].'">
      <input type="hidden" name="credit" value="'.$row[2].'">
      <input type="hidden" name="section" value="'.$row[3].'">
    </form> 
  </fieldset> 
</body>
';

Read more about disabled controls.

In loop

Since you are using this with loop, which will result in multiple same name inputs. POST them as arrays. Add [] to input names.

For example:

echo '<input type="hidden" name="code[]" value="'.$row[0].'">';

This will give you array of all code input values. To set a key for value, add it inside of []. For example [$i].

But I presume your $i starts with 0, so this is not needed in this case, since keys will be automatically assigned (if empty) starting from 0.

Upvotes: 2

Related Questions