2

我想编辑一个动态生成的表单(意思是:我不知道会生成多少行)。此内容是在while循环中生成的,并且生成的 HTML 会创建 的按钮input-type=submit,生成与循环中的迭代一样多的同名按钮。

在生成的按钮中,我想知道单击了哪个提交按钮,以便为用户提供单击它的相同表单。忽略连接数据库的名称和密码;连接性很好。

随意提出任何新方法来实现所需的功能。

代码如下:

    echo "you have reached your travel details page. your recent travelling details are as follows".'</br>';
$dbc=mysqli_connect('localhost','xyz','xyz','abc') or die("connection to DB failed");
$query="SELECT * FROM travel_details WHERE emailid='{$_SESSION['username']}' ORDER BY dep_date DESC";
$result=mysqli_query($dbc,$query) or die("error in querying the DB");
?>
<h1>Your travel details are:-</h1>
<form name="showtraveldet" METHOD="POST" action="edittraveldet.php">
    <table border="1">
    <tr>
    <th>Starting point</th><th>Ending point</th><th>No of passengers</th><th>Expected fare</th><th>Departure date</th>
    <th>Departure time</th><th>Arrival Date</th><th>Arrival Time</th><th>Car Model</th><th>Car number</th>
    <th>Who is driving</th><th>Driver's license number</th>
    </tr>
<?php
while ($row=mysqli_fetch_array($result)) 
{
    $tid=$row['travel_id'];
    echo "the value of tid is '{$tid}'";
    echo'<tr><td>'.$row['start_point'].'</td><td>'.$row['end_point'].'</td><td>'.$row['no_of_pass'].'</td><td>'.
    $row['exp_fare'].'</td><td>'.$row['dep_date'].'</td><td>'.$row['dep_time'].'</td><td>'.$row['arr_date'].'</td><td>'.$row['arr_time'].'
    </td><td>'.$row['car_model'].'</td><td>'.$row['car_no'].'</td><td>'.$row['who_is_driving'].'</td><td>'.$row['driver_license_no'].'</td>
    <td><input type="submit" name="edit" value="Edit"></td></tr><input type="hidden" name="travelid" value="'.$row['travel_id'].' ;?>">';

}

编辑traveldet.php:-

    $travelid=$_POST['travelid'];
echo "the travel id in the variable is $travelid and got the value from '{$_POST['travelid']}'";
$dbc=mysqli_connect('localhost','xyz','xyz','abc') or die("connection to DB failed");
$query="SELECT * FROM travel_details WHERE travel_id='{$travelid}'";
$result=mysqli_query($dbc,$query) or die("error in querying the DB");
mysqli_close($dbc);
$row=mysqli_fetch_array($result);
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validatewheregoing()" name="wheregoing">
        <h1> Enter your travelling details so that other travellers can join you</h1>
        <fieldset>
            <legend> Travelling details </legend>
            Start Point: <input type="text" name="start" value="<?php echo $row['start_point']; ?>"/><br />
            End point: <input type="text" name="end" value="<?php echo $row['end_point']; ?>"/><br />
            Passengers allowed: <input type="number" name="noofpass" value="<?php echo $row['no_of_pass']; ?>"/><br />
            Expected Fare per passengers in rupees:<input type="number" name="fare" value="<?php echo $row['exp_fare']; ?>"/><br />
            Departure Date:<input type="date" name="depdate" value="<?php echo $row['dep_date']; ?>"/><br/>
            Departure time:<input type="time" name="deptime" value="<?php echo $row['dep_time'] ;?>"/><br/>
            Arrival Date:<input type="date" name="arrdate" value="<?php echo $row['arr_date']; ?>"/><br/>
            Arrival time at destination:<input type="time" name="arrtime" value="<?php echo $row['arr_time']; ?>"/><br/>
            Car Model and name:<input type="text" name="cardet" value="<?php echo $row['car_det']; ?>"/><br/> <!--make this as a dropdown box for better database matching-->
            Car Number:<input type="text" name="carno" /><br/><input type="checkbox" name="taxi" value="check this box if pooling a taxi">
            Is the car self driven or driven by driver?<input type="radio" name="drivedet" value="Selfdriven" checked=""/>Self Driven<input type="radio" name="drivedet" value="driverdriven" />Driver driven<br />
            Driver's License number<input type="text" name="licence_no"/></br>
            <input type="checkbox" name="taxi" value="check this box if pooling a taxi"></br>
            <input type="hidden" name="travelid" value="<?php echo $travelid ;?>" />
            <input type="submit" value="invite travellers" name="editwheregoing"/>
        </fieldset>
     </form>
4

2 回答 2

1

如果只有您可以更改代码,我建议您将表单标签本身放在 while 循环中,每个标签都有相同的操作指向相同的 url,但向目标页面提交不同的信息。这样您就不必担心单击的按钮

while($row=mysqli_fetch_array($result))
{
    //<form action="sameactionurl.php" name="form_1">
       //<input type="hidden" name="travelid" value="$row['travelid']" />
    //</form>
}

另一种解决方案,如果您不想更改代码,则在提交表单之前使用 JavaScript 将公共隐藏字段设置为当前 ID 的值

于 2013-01-16T16:28:48.050 回答
0

以标准方式命名您的提交按钮,并在末尾附加一个 3 位数字,“button_XXXX_###”,其中 ### 是数字,XXX 是按钮的原始名称。

提交后,检查所有以“button_XXXX”开头的变量的请求参数,并将实际名称“button_XXXX_####”用'_'字符分开,“###”后缀将显示按钮的编号按下。

不过,为每一行创建一个表单可能会更容易。

于 2013-01-16T16:25:19.067 回答