[英]how to get customer id from mysql and then to store it in a session variable to use the session variable in another file to retrieve information
[英]Get the ID of a button on click and store it as a session variable
我正在尝试制作博客的小版本,但是在单击附加到该按钮的按钮来从数据库编辑某个帖子时,我遇到了麻烦。 我已经为每个与数据库中的ID匹配的按钮设置了ID,但是我现在要做的是将其存储在会话变量中,这样我就可以使用该ID从数据库中获取信息,从而可以进行更新数据库中的信息。
这是帖子的样子:
$result = $db->query("SELECT * FROM posts");
while($row = $result->fetch()) {
echo "<li><h1>" . $row['headline'] . "</h1></li>";
echo "<li><b>" . $row['content'] . "</b></li>";
echo "<li><h4>" . $row['author'] . "</h4></li>";
echo "<li><h5>" . $row['date'] . "</h5></li>";
echo "<input type='submit' name='edit' value='Edit' id='" . $row['id'] . "'>";
echo "<br>";
echo "<input type='submit' name='delete' value='delete' id='" . $row['id'] . "'>";
}
if (isset($_POST['edit'])) {
//Here is where I need to get the ID of the clicked edit button
//But I can't figure out how to do that in PHP
}
HTML表单元素的id
属性在提交时不随表单一起发送。 您想要做的是在表单中使用<input type="hidden" name="post_id" value="" />
元素来检索帖子的ID。
将一个隐藏字段添加到您的窗体,称为postId。
然后,将一个类添加到您的按钮,然后在给定类中按下该按钮中的任何一个时,使用jQuery您可以更新隐藏字段的值。
...
<input type="submit" name="edit" value="Edit" class="postButton" id="<?php echo $row["id"]; ?>" />
...
<input type="submit" name="edit" value="Edit" class="postButton" id="<?php echo $row["id"]; ?>" />
...
<input type="submit" name="edit" value="Edit" class="postButton" id="<?php echo $row["id"]; ?>" />
...
<input type="hidden" name="postId" value="" />
然后,您可以使用jQuery将ID传递给hiddenfield:
$('.postButton').click(function() {
$('#postId').val($(this).attr('id'));
});
或者使用链接来实现这一点,然后将其从css格式化为一个按钮。
我建议使用带有链接的编辑页面:
<a href="/posts/12941/edit">edit post</a>
我不认为您只能在PHP中做到这一点。 您可能会为每个按钮使用不同的name
属性,并以此方式进行标识,尽管我不确定如何在不使用可怕的if/else
语句的情况下有效地检测单击了哪个按钮。
做到这一点的另一种方法是使用带有AJAX请求的jQuery
(或纯Javascript,尽管我会建议jQuery)将单击按钮的id
发送到PHP脚本。
像这样的东西:
jQuery的
$('.button').on('click', function(e){
e.preventDefault();
$.post("php_page.php", {id: $(this).attr('id')}, function(data){
//This is the success function, you can display a message or something
});
});
然后,在PHP中
if(!empty($_POST['id']))
$_SESSION['button_clicked'] = $_POST['id']
这是非常基本的,您需要更多的代码才能使所有功能正常工作(例如:session_start(),jQuery中的文档就绪函数),但这就是我要做的。
您也可以使用AJAX进行GET
请求,具体取决于您的代码。
您可以使用JQUERY
获取id
并可以在php
中将其设置为会话。
while($row = $result->fetch()) {
echo "<li><h1>" . $row['headline'] . "</h1></li>";
echo "<li><b>" . $row['content'] . "</b></li>";
echo "<li><h4>" . $row['author'] . "</h4></li>";
echo "<li><h5>" . $row['date'] . "</h5></li>";
echo "<input type='submit' class='edit' name='edit' value='Edit' data-id='your id goes here' id='" . $row['id'] . "'>";
echo "<br>";
echo "<input type='submit' name='delete' value='delete' id='" . $row['id'] . "'>";
}
JQUERY
$('.edit').on('click', function(){
var id = $(this).attr('data-id);
$.ajax({
type: 'post',
url: '/path/where /you want to set session',
data: {id : id},
success: function (res){
alert(res);
});
});
您可以通过以下方式实现
$result = $db->query("SELECT * FROM posts");
?>
<?php
echo "<li><h1>" . $row['headline'] . "</h1></li>";
echo "<li><b>" . $row['content'] . "</b></li>";
echo "<li><h4>" . $row['author'] . "</h4></li>";
echo "<li><h5>" . $row['date'] . "</h5></li>";
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><input type='hidden' name='id' value='".$row['id']."'>
<input type='submit' name='edit' value='Edit' id='" . $row['id'] . "'></form>";
echo "<br>";
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><input type='hidden' name='id' value='".$row['id']."'><input type='submit' name='delete' value='delete' id='" . $row['id'] . "'></form>";
}
if (isset($_POST['edit'])) {
$id=$_POST['id'];
$_SESSION['id']=$id;
}
<script>
function submitForm(idButton) {
document.getElementById('storedClickedButtonId').value = idButton;
document.forms["MyForm"].submit();
}
</script>
<form name="MyForm" action="tt.php" method="post">
<?php
while($row = $result->fetch()) {
echo "<li><h1>" . $row['headline'] . "</h1></li>";
echo "<li><b>" . $row['content'] . "</b></li>";
echo "<li><h4>" . $row['author'] . "</h4></li>";
echo "<li><h5>" . $row['date'] . "</h5></li>";
echo "<input type='button' name='edit' onClick='submitForm(this.id)' value='Edit' id='" . $row['id'] . "'>";
echo "<br>";
echo "<input type='button' name='delete' onClick='submitForm(this.id)' value='delete' id='" . $row['id'] . "'>";
echo "<input type='hidden' id='storedClickedButtonId' name='clickedButtonId' value='' />";
}
?>
</form>
您将需要将每个部分包装在自己的表单标签中。 然后将ID设置为隐藏字段,然后将您的提交按钮更改为具有相同的ame但值不同(在本例中为操作-编辑/删除)。
然后,您可以从隐藏字段中检索值,并根据所按下的提交按钮的值来忽略该值:
while($row = $result->fetch()) {
echo "<li><h1>" . $row['headline'] . "</h1></li>";
echo "<li><b>" . $row['content'] . "</b></li>";
echo "<li><h4>" . $row['author'] . "</h4></li>";
echo "<li><h5>" . $row['date'] . "</h5></li>";
echo "<form method='post'>"; //form tag
echo "<input type='hidden' name='deleteid' value=" . $row['id'] . ">"; //get id
echo "<input type='submit' name='action' value='edit' id='" . $row['id'] . "'>";//pick action
echo "<br>";
echo "<input type='submit' name='action' value='delete' id='" . $row['id'] . "'>";//pick action
echo "/form>"
}
if (isset($_POST['action'])) {
$id=$_POST['deleteid'];//get id
if($_POST['action']=='edit'){//do edit}
else if($_POST['action']=='delete'){//do delete}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.