简体   繁体   English

获取单击按钮的ID,并将其存储为会话变量

[英]Get the ID of a button on click and store it as a session variable

I'm trying to make a small version of a blog but I'm stuck when it comes to editing a certain post from the database using the click of a button attached to the post. 我正在尝试制作博客的小版本,但是在单击附加到该按钮的按钮来从数据库编辑某个帖子时,我遇到了麻烦。 I've set an id to each button that matches the id in the database, but what I need to do now is store it in a session variable so that I can get the information from the database using this id and therefore allowing me to update the info in the database. 我已经为每个与数据库中的ID匹配的按钮设置了ID,但是我现在要做的是将其存储在会话变量中,这样我就可以使用该ID从数据库中获取信息,从而可以进行更新数据库中的信息。

Here's how a post looks like: 这是帖子的样子:

$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
   }

The id attribute of an HTML form element is not sent with the form on submission. HTML表单元素的id属性在提交时不随表单一起发送。 What you want to do is use a <input type="hidden" name="post_id" value="" /> element in your form to retrieve the ID of the post. 您想要做的是在表单中使用<input type="hidden" name="post_id" value="" />元素来检索帖子的ID。

Add a hidden field to your form, called postId. 将一个隐藏字段添加到您的窗体,称为postId。

Then, add a class to your buttons, and when any of that button pressed with the given class, with jQuery you can update the value of the hidden field. 然后,将一个类添加到您的按钮,然后在给定类中按下该按钮中的任何一个时,使用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="" />

Then you can pass the id to the hiddenfield with jQuery: 然后,您可以使用jQuery将ID传递给hiddenfield:

$('.postButton').click(function() {
    $('#postId').val($(this).attr('id'));
});

Or use a link to achive this, and format that from css to seems like a button. 或者使用链接来实现这一点,然后将其从css格式化为一个按钮。

我建议使用带有链接的编辑页面:

<a href="/posts/12941/edit">edit post</a>

I don't think that you can do that in PHP only. 我不认为您只能在PHP中做到这一点。 You could have a different name attribute for each button and identify it that way, although I'm not sure of how you can effectively detect which was clicked without using a monstreous if/else statement. 您可能会为每个按钮使用不同的name属性,并以此方式进行标识,尽管我不确定如何在不使用可怕的if/else语句的情况下有效地检测单击了哪个按钮。

Another way to do that would be using jQuery (or pure Javascript, although I'd recommand jQuery) with an AJAX request to send the id of the clicked button to a PHP script. 做到这一点的另一种方法是使用带有AJAX请求的jQuery (或纯Javascript,尽管我会建议jQuery)将单击按钮的id发送到PHP脚本。

Something like this : 像这样的东西:

jQuery 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
    });
});

Then, in PHP 然后,在PHP中

if(!empty($_POST['id']))
    $_SESSION['button_clicked'] = $_POST['id']

This is very basic, you need more code than that to make everything work (ex: session_start(), document ready function in jQuery), but that's what I would do. 这是非常基本的,您需​​要更多的代码才能使所有功能正常工作(例如:session_start(),jQuery中的文档就绪函数),但这就是我要做的。

You can also do a GET request with AJAX, which is more appropriate depends on your code. 您也可以使用AJAX进行GET请求,具体取决于您的代码。

You can use JQUERY to get an id and in php you can set as session.. 您可以使用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 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);
 });
});

you can achieve this following way 您可以通过以下方式实现

    $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>

You will need to wrap each section in its own form tag. 您将需要将每个部分包装在自己的表单标签中。 Then set the id to a hidden field, and change your submit buttons to have the same ame but different values (in this case action - edit/delete). 然后将ID设置为隐藏字段,然后将您的提交按钮更改为具有相同的ame但值不同(在本例中为操作-编辑/删除)。

Then you can retrieve the value from the hidden field and deside what to do with it based on the value of the submit button that was pressed: 然后,您可以从隐藏字段中检索值,并根据所按下的提交按钮的值来忽略该值:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM