0

我正在从 mysql 填充一个表单。代码是。.

task.php

<div data-role="content">
               <h2> Please select cars </h2>
<form method="post" action="cars.php">
            <?php 
            $carq = "select * from cars";
            $executecars = mysql_query($carq);
            while($row=mysql_fetch_assoc($executecars)){
                $cname = $row['name'];  
                ?>

               <label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
               <input type="checkbox" name="car" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>

            <?php }     

            ?>
           <input type="submit" name="submitcars" id="submitcars" value="View Details"/>
           </form>
         </div>

现在在cars.php中我想查询以显示所选汽车的详细信息,

<div data-role="content">
        <?php 
            if(isset($_POST['submitcars'])){

                echo $_POST[$cname];?????????????


                }
        ?>

        </div>

现在如何在 cars.php 中处理表格?

谢谢

4

3 回答 3

2

制作汽车属性数组::

<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>

并让他们在下一页

if(isset($_POST['submitcars'])){
   foreach($_POST['car'] as $car){
       // do something with $car
   }
}
于 2012-10-05T10:53:27.980 回答
0

尝试这个

<form method="post" action="cars.php">
<?php 
            $carq = "select * from cars";
            $executecars = mysql_query($carq);
            while($row=mysql_fetch_assoc($executecars)){
                $cname = $row['name'];  
                ?>

               <label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
               <input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>

            <?php }     

            ?>
于 2012-10-05T10:55:01.847 回答
0

首先,让我先说你不应该使用 mysql 函数。它们已贬值,不再推荐。

在 task.php 上,您的开始表单标签被打印在您的循环中。其次,您可能应该使用 [] 使用一组复选框。

     <div data-role="content">
        <h2> Please select cars </h2>
        <form method="post" action="cars.php">
        <?php 
        $carq = "select * from cars";
        $executecars = mysql_query($carq);
        while($row=mysql_fetch_assoc($executecars)){
            $cname = $row['name'];  
            ?>

           <label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
           <input type="checkbox" name="cars[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>

        <?php }  ?>
       <input type="submit" name="submitcars" id="submitcars" value="View Details"/>
       </form>
    </div>

然后,在 cars.php 上:

<div data-role="content">
    <?php 
        if(isset($_POST['submitcars'])){

            if(isset($_POST['cars'] && count($_POST['cars']) > 0){

                 foreach($_POST['cars'] as $key => $car){

                     echo $car . '<br /'>;

                 }                    

            }
            else{
                echo 'No cars found!';
            }
        }
    ?>
</div>
于 2012-10-05T10:56:23.163 回答