php - 如何检查提交了哪个按钮

标签 php html mysql forms

我有一个表格,像这样:

echo '<table align=center border=1>';
echo '<form method="post">';
echo '<tr>';
echo '<th>Lista Echipamente</th>';
echo '<th>Actiune</th>';
echo '</tr>';
while($row=mysql_fetch_array($sql)){
echo '<tr>';    
echo '<td><input class="search-logi" id="tip" type="text" name="tip" value="'.$row['tip'].'"></td>';
echo '<td><input type=submit name=modifica value=Modifica></td>';
echo '</tr>';
}    
echo '</form>';
echo '</table>';

因为循环,这个表单会有多行。对于每个输入,将是一个按钮。 假设我想在第二个输入中添加一个值,然后我将提交它(显然也是第二个按钮)。问题来了,在我的代码中我只有一个按钮,但用户看到的次数与循环次数一样多。如何检查提交了哪个按钮?

最佳答案

或者,您可以使用 <button>用于此目的的标签:

echo '<form method="post">'; // form tags are outside to be valid!!!
echo '<table align=center border=1>';
echo '<tr>';
    echo '<th>Lista Echipamente</th>';
    echo '<th>Actiune</th>';
echo '</tr>';

while($row = mysql_fetch_assoc($sql)){
    echo '<tr>';    
        echo '<td><input class="search-logi" type="text" name="tip['.$row['id'].']" value="'.$row['tip'].'"></td>';
        echo '<td><button type="submit" name="modifica" value="'.$row['id'].'">Modifica</button</td>';
    echo '</tr>';
}    

echo '</table>';
echo '</form>';

然后在处理表单上:

$modifica = $_POST['modifica']; // shoud return the index num
$tip = $_POST['tip'][$modifica]; // select which textbox

关于php - 如何检查提交了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26341621/

相关文章:

java - 在 Java 中将数据插入 SQL 数据库时出错

php - 无法更新 URL 内标记的类别

javascript - 创建没有缓动效果的垂直轮播

javascript - 将 CSS/Javascript 内联到 HTML 文件中

mysql - 使用 COUNT 语句选择超过 1 个 mysql 字段

php - MySQL数据库检查

php - PHP 中空函数的性能

PHP setcookie() 奇怪的行为

php - Laravel 5.4 firstOrCreate 或 firstOrNew 如果唯一只是 id

html - 在没有 photoshop 的情况下创建统一的提交、保存、取消按钮