Chrillewoodz
Chrillewoodz

Reputation: 28348

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.

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
   }

Upvotes: 1

Views: 8057

Answers (8)

Steve
Steve

Reputation: 20469

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).

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

Upvotes: 0

Halayem Anis
Halayem Anis

Reputation: 7785

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

Upvotes: 0

vaso123
vaso123

Reputation: 12391

Add a hidden field to your form, called 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.

...
<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:

$('.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.

Upvotes: 2

srinath madusanka
srinath madusanka

Reputation: 592

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

Upvotes: 0

RNK
RNK

Reputation: 5792

You can use JQUERY to get an id and in php you can set as session..

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

Upvotes: 0

Drown
Drown

Reputation: 5982

I don't think that you can do that in PHP only. 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/elsestatement.

Another way to do that would be using jQuery(or pure Javascript, although I'd recommand jQuery) with an AJAX request to send the idof the clicked button to a PHP script.

Something like this :

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

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.

You can also do a GET request with AJAX, which is more appropriate depends on your code.

Upvotes: 0

jgroenen
jgroenen

Reputation: 1326

I would suggest using an edit page, with a link:

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

Upvotes: 0

AlexL
AlexL

Reputation: 1707

The id attribute of an HTML form element is not sent with the form on submission. 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.

Upvotes: 2

Related Questions