如何确定在PHP表单中单击了哪个动态生成的提交按钮?

时间:2013-01-16 16:18:51

标签: php mysql while-loop submit-button

我想编辑动态生成的表单(意思是:我不知道将生成多少行)。此内容在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'].' ;?>">';

}

edittraveldet.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>

2 个答案:

答案 0 :(得分: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的值

   

答案 1 :(得分:0)

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

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

但是,为每行创建表单可能更容易。