我正在生成一个html表。每行都以编辑按钮开头。
网站A:
echo "<table border='1' align='center'>";
echo "<tr>"
. "<th style='font-weight:bold'></th>"
. "<th>".$nr.":</th>"
. "<th>Anschrift</th><th>Nachname</th>"
. "<th>Vorname</th>"
. " <th>PLZ</th>"
. "<th>Ort</th>"
. "</tr>";
echo "<form action='lxKundenEdit.php' method='POST'>";
$i=0;
foreach($arr as $key =>$value)
{
echo "<tr><td><input type='submit' name='btn'.$i.'\'' value='Bearbeiten'/></td>";
foreach($value as $subkey=>$subValue)
{
echo "<td>".$subValue."</td>";
}
echo "</tr>";
$i++;
}
echo "</form>";
echo "</table>";
然后我想知道哪个按钮被按下了。当vardump POST-Array时,它似乎没什么用。关于这个的任何提示?问候,Ismir
网站B:
var_dump($_POST['btn0']); //f.e.
答案 0 :(得分:3)
只需在隐藏的输入中传递发件人ID
即可<input type="hidden" name="sender" value="<?= $senderID; ?>">
在网站B上,您可以从$_POST
变量中获取值:
echo $_POST['sender'];
我看到我误解了你的问题。你可以做的是在提交按钮上传递你的子键,如下所示:
<input type="submit" name="submit[<?= $subkey; ?>]" value="send" />
答案 1 :(得分:3)
您应该为每个按钮指定一个唯一值。发布表单后,您可以检查该按钮的值。
echo '<input type="submit" name="btn'.$i.'" value="'.$i.'" />';
修改强>
正如彼得在评论中指出的那样,如果你想改变按钮的文字,你可以使用按钮元素:
<?php
echo '<form method="post">';
echo '<button type="submit" value="button 1 was used" name="button">Send</button>';
echo '<button type="submit" value="button 2 was used" name="button">Send</button>';
echo '</form>';
var_dump( $_POST[ 'button' ] );
答案 2 :(得分:2)
您需要做的就是:
制作所有提交按钮的数组,并将ID作为键。
protected $primaryKey = 'customer_id';
然后在PHP中,发布提交按钮,如:
echo '<tr><td><input type="submit" name="btn[<?php echo $i;?>]" value="Bearbeiten"/></td>';
答案 3 :(得分:1)
只需在表单中包含隐藏的输入,例如
<input type='hidden' name='submit'/>
因此,您可以使用它来检查表单是否已发送(if(isset($ _ POST ['submit']))等。然后检查任何其他$ _POST字段,查找任何['btn ...'],例如
foreach($_POST as $key => $value){ // check all the $_POST fields
if(substr($key,0,3) == 'btn'){ // if its name begins with 'btn'...
$button_id = substr($key,3,NULL); // then that's some button. Get its ID
}
}
认为这应该有效