简体   繁体   中英

How to get the value of a loop text field using post method in php

I created all the text field by loop and when I click button of the text field, I can post it to another page. But now I cannot retrieve value of the text field. How can I retrieve the value of the loop text field?

This is my code.

<?php 
                $MYSQLQuery1=mysql_query("select id,ccode,pdesc,cost from product2")or die(mysql_error());
                $i=1;
                while($row=mysql_fetch_array($MYSQLQuery1)){
                ?>
                    <tr>
                    <td><?php echo"<img src='img/".$row['ccode'].".jpg' width='150' height='200'/>","<br/>"; 
                              echo $row['pdesc'],"<br/>"; 
                              echo $row['cost']; ?>
                    </td>
                    <td><?php echo "<input type='text' name='product".$i."'>";  ?></td>
                    <td>
                        <form action="addvehicle.php" role="form" method="POST" class="form-inline">
                            <input type="hidden" name="ID" value="<?php echo $row['id']; ?>">
                            <input type="submit" name="SUBMITEDITVEHICLE"  value="Edit" class="btn btn-success">
                        </form>
                    </td>
                    </tr>
                <?php 
                    $i++;
                }
                ?>

This is where I post the value.

if(isset($_POST['SUBMITEDITVEHICLE']))
{
    if($_POST['SUBMITEDITVEHICLE']=='Edit')
    {   
            $i=0;
            $E_1 = clean($_POST['ID']);
            $gt = clean($_POST['product. $i']); 
            echo $gt,"<br/>";
            $query_editvehicle=mysql_query("SELECT ccode,pdesc,cost FROM product2 WHERE ID='$E_1' ")or die(mysql_error());
            while($roweditvehicle=mysql_fetch_array($query_editvehicle))
            {
                $txtid = $roweditvehicle['id'];
                $txtvehicleregno = $roweditvehicle['ccode'];
                $txtmakemodel = $roweditvehicle['pdesc'];
                $txtcost = $roweditvehicle['cost']; 
            }
        echo txtvehivleregno;
        echo txtmakemodel;
        echo txtcost;
        $i++;
    }
}

<td><?php echo "<input type='text' name='product".$i."'>"; // ?></td>

它是一个文本框,因此您必须通过

<td><?php echo "<input type='text' name='product".$i."' value=$row['cost']>"; // ?></td>

As far as I know, you can echo it inside the while like this:

<input type='text' name='product[]'> //don't forget the []

then it if you post it you can have an array value,

$products = $_POST['product'];
print_r($products);//to print array

try this code:

<?php 
            $MYSQLQuery1=mysql_query("select id,ccode,pdesc,cost from product2")or die(mysql_error());
            $i=1;
            echo '<form action="addvehicle.php" role="form" method="POST" class="form-inline">';
            while($row=mysql_fetch_array($MYSQLQuery1))
              {
                echo "
                     <tr>
                        <td>
                           <img src='img/".$row['ccode'].".jpg' width='150' height='200'/>
                           <br/>
                          ".$row['pdesc']."<br/>"
                           .$row['cost']. 
                      "</td>
                        <td>
                          <input type='text' name='product[]'>
                       </td>
                      <td>
                        <input type='hidden' name='ID' value='".$row['id']."'>
                        <input type='submit' name='SUBMITEDITVEHICLE'  value='Edit' class='btn btn-success'>

                     </td>
                    </tr>"; //end of one echo and end of tr
               }//end of while

 echo "</form>"; //end of form
            ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM