Send data to another page via a button

Asked

Viewed 824 times

-2

I would like to know how to resolve the following situation: I have a page that has a loop in which it shows all posts of a specific posttype, and in each post I put a button (no functionality for now).

I would like to somehow make that when the user clicked on this button, the post would be saved in some way and appear on another page called "my posts", and of course, if the person clicked on the button again this post would disappear from the page "my posts".

I’ve researched plugins for this, but it didn’t happen and now I’m trying to make some code even at hand, but I don’t have as much knowledge for it. If anyone can help, I’d be grateful.

  • Welcome to Stackoverflow EN! Show us some code you’ve already done. So we can help

  • You are using a database to save and control these posts?

  • am yes mysql workbanch

1 answer

1


I think the best way to do this is by using GET. You will create for each button a link that will contain 2 things:

1) The file that will do the processing of the data sent. 2) The data itself.

On each post button, put the address of a specific file printing right after the post id (or any other identification).

Below is an example:

<div class="post"> <div class="thumb"></div> <h3 class="post-title"><?php echo $post['name'];?></h3> <a href="save_post.php?id=<?php echo $post['id'];?>"></a> </div>

Notice that right after the link address (href) I print a "? id=" (indicating the name of the variable that will be passed and the post id (which in your case will be the value that contains the post information. Imagine that the post in question is the id 50 post. The address printed on href will be:

<a href="save_post.php?id=50"></a>

Now, create the file save_post.php that will handle this data. I don’t know exactly how you intend to create the logic of storing "my posts" but to get the variable with the post id, just do the following. in save_post.php put:

$post_id = $_GET['id'];

Done. Just use the $post_id variable the way you want because it will contain the id of the clicked post.

  • Wow, perfect I’ll do that yes, thank you!!!

  • And how would I ever click again?

  • This will depend on where you will store the data from "my posts".

  • I got it bro, thanks!!

  • As I said in the explanation, in the file save_post.php you will implement the way to store this data. When removing you just need to do the reverse process. If you made an insert in any table with the post ID, just delete this post by searching for the ID.

  • Hmmm, beauty ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.