Okay so I am working on a simple social network. I am building it with PHP and MySQL. Users can post status updates just like on Facebook. I know how to use the INSERT INTO and SELECT FROM statements to input/output user statuses, but how would I go about having a button (an X) that when clicked it calls a function that holds a MySQL query that DELETES the post? Or one that edits the post? Is the way to do it a call to a function()?
You need to learn AJAX to do that. With an Ajax call, you would capture the click
event with JavaScript, and will post an AJAX request to a PHP script that will issue the corresponding delete
sql statement to drop the post.
PHP runs on the server. The button exists on the client. So you can't call a function directly.
You need to have a script that accepts some input over HTTP (since you want to do something, this should be an HTTP POST request, in PHP you can read that data via $_POST[]
). The script should sanity check the input, do any appropriate actions based on that input and make a response indicating success or failure.
The simplest way to have the browser make the request is to use a <form>
. This also allows you to collect information you may need from the user.
To do it "just like on Facebook" (while following best practises) you should use JavaScript to bind a submit event listener to the form, then stop the default event and use XMLHttpRequest to make the HTTP POST request instead (this is Ajax). Then you use DOM manipulation to update the information displayed to the user.