通过 Button 将值传递给 Php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19814082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a value through Button to Php function
提问by M.chaudhry
I am beginner in php and I am currently working on admin panel (you can see my admin panel page). The thing is that I want to pass serial number through these two buttons to perform further. But I can't find how to send $value
to edit and delete a particular line.
我是 php 初学者,目前正在管理面板上工作(您可以看到我的管理面板页面)。问题是我想通过这两个按钮传递序列号以进一步执行。但我找不到如何发送$value
以编辑和删除特定行。
<div id="headin2"><strong> <h3>Admin page </h3></strong></div>
<?php
echo "<table width=\"100%\" border=\"0\" id=\"tab\">";
echo "<tr>";
echo "<th id=\"td1\">Serial No</th><th id=\"td2\">Account Title</th>
<th id=\"td3\">Email</th><th id=\"td4\">Gender</th><th id=\"td5\">city</th>
<th id=\"td6\">Course</th><th id=\"td7\">status</th><th id=\"td8\" colspan=\"3\">Action</th>";
echo "</tr>";
while ( $row = mysql_fetch_array($query))
{
$SN = $row['SN'];
$actitle = $row['ac_title'];
$email = $row['email'];
$gender = $row['sex'];
$cite = $row['city'];
$course = $row['CRS'];
$status = $row['status'];
echo "<tr>";
echo "<td>".$SN."</td><td>".$actitle."</td><td>".$email."</td>
<td>".$gender."</td><td>".$cite."</td><td>".$course."</td><td>".$status."</td>
<td>"."<input type=\"button\" name=\"edit\" value=\"Edit\"/>
<input type=\"button\" value=\"Delete\" name=\"delete\" >"."</td>";
echo "</tr>";
}
?>
</table>
回答by David
You can do it one of two ways.
您可以通过以下两种方式之一进行操作。
jQuery and AJAX
jQuery 和 AJAX
For each <tr>
, everywhere there is a delete button,
对于每个<tr>
,到处都有一个删除按钮,
<a href="#" data-id="<?php echo $value; ?>" class="delete-row">Delete</a>
Script at the bottom:
底部脚本:
//Include jQuery here
<script language="javascript">
$('.delete-row').click(function (event) {
event.preventDefault();
var id = $(this).data('id');
$.ajax({
url: "url/to/delete",
method: "POST",
cache: false,
data: { id: id },
success: function (html) {
$(this).parent().parent().remove();
});
});
});
</script>
This puts the ID of the row into the <a href>
itself using data
and uses jQuery to send out an AJAX call to delete the record. If the delete is successful, it removes the row from the table.
这将<a href>
使用行的 ID 放入自身,data
并使用 jQuery 发出 AJAX 调用以删除记录。如果删除成功,它将从表中删除该行。
Old-School Button
老式按钮
For each <tr>
, everywhere there is a Delete button,
对于每个<tr>
,到处都有一个删除按钮,
<form method="POST" action="url/to/delete">
<input type="hidden" name="id" value="<?php echo $value; ?>" />
<input type="submit" value="Delete" />
</form>
This is the old-school way to do it, where the hidden field is how the backend knows which row to delete.
这是老派的做法,隐藏字段是后端如何知道要删除哪一行。
On the backend, you still use $_POST['id'];
to get the ID of the record to remove. In the above examples, $value
is the ID for each row, and is most likely something like $row['id']
when it is coming from a foreach()
.
在后端,您仍然使用$_POST['id'];
获取要删除的记录的 ID。在上面的例子中,$value
是每一行的 ID,很可能类似于$row['id']
它来自foreach()
.
回答by AlexP
You need both the action
(edit/delete) andthe row id
. Unfortunately, without some JS the button will only post one value.
您需要action
(edit/delete)和row id
。不幸的是,如果没有一些 JS,按钮只会发布一个值。
You can create a new form for each row, add in a hidden element. For example:
您可以为每一行创建一个新表单,添加一个隐藏元素。例如:
<?php while ($row = mysql_fetch_array($query)) : ?>
<tr>
<!-- other cells -->
<td>
<form method="post" action="">
<input type="submit" name="action" value="Edit"/>
<input type="submit" name="action" value="Update"/>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"/>
</form>
</td>
</tr>
<?php endwhile; ?>
Then after posting it you can just check for the action
and id
然后在发布后,您可以检查action
和id
if ($_POST['action'] && $_POST['id']) {
if ($_POST['action'] == 'Edit') {
// edit the post with $_POST['id']
}
}
回答by Zahidul Hossein Ripon
when you click Edit it open a page with corresponding record. You can do it by a Edit link or a image or a button.
当您单击编辑时,它会打开一个带有相应记录的页面。您可以通过编辑链接或图像或按钮来完成。
<?php echo "<a href='".BASE_URL."admin/edit.php?id=$id' target='_blank'> <img src=".BASE_URL."/images/edit.png width=16 height=16 alt=Edit /> </a>";?>
for delete you can use jquery. here is a example
对于删除,您可以使用 jquery。这是一个例子
echo "<a href='#' onclick='deletePrescription($id)' ><img src=images/document_delete.png width=16 height=16 title='Delete Prescription' alt=Delete /> </a>";
<script type="text/javascript">
function deletePrescription(checkup_id) {
//alert(checkup_id);
var agree=confirm("Do you really want to delete the prescription?");
if (agree)
{
$.ajax
({
type: "POST",
url: "delete_prescription_admin.php",
data: "checkup_id="+checkup_id,
success: function(msg)
{
//
//alert( "array Updated: " + msg );
location.reload(true);
}
});
}
else
{
return false ;
}
}
</script>
回答by Gal Samuel
PHP is on the server side, so you need to send parameters, parse it and have your php act. i.e.
PHP 在服务器端,所以你需要发送参数,解析它并让你的 php 动作。IE
The edit button will make POST or GET request with parameter: id=123&action=edit
编辑按钮将使用参数发出 POST 或 GET 请求:id=123&action=edit
and your PHP will parse the Query String:
并且您的 PHP 将解析查询字符串:
$strID = $_GET['id'];
$strAction = $_GET['action'];
then act on it..
然后采取行动..
if ($strAction=='edit'){
// do something...
}
Your buttons should be wrappen in a form like:
您的按钮应该以如下形式包装:
<form action="yourFile.php?id=<?=$SN ?>&action=edit" method="GET" id="formEdit<?=$SN ?>">
<button type="submit" value="Edit">
</form>
回答by Heinrich
Use a hidden input (e.g.<input type="hidden" value="your_value_here">
)
使用隐藏输入(例如<input type="hidden" value="your_value_here">
)
回答by Abdullah
<a href=register_course.php?id=$roll&code=".$row['course_code']."&name=".$row['course_name']."><input type=submit value=Register>
*here register_course.php is the file where you are sending some variable having some data it will be delivered when button is clicked whose value is register //course code is the variable you want to send ... im assigning its value as $ code... and assigning id $roll as id and assigning course_name as $name....
*这里 register_course.php 是您发送一些变量的文件,其中包含一些数据,单击按钮时将传递一些数据,其值为 register //课程代码是您要发送的变量...我将其值分配为 $ code ... 并将 id $roll 分配为 id 并将 course_name 分配为 $name ....
the other file will get the variables having some data
另一个文件将获得具有一些数据的变量