MikeS
MikeS

Reputation: 383

Make a button perform an SQL statement

I'm trying my luck here. I am currently developing a website wherein people should be able to send a message and add a user as a friend from that person's profile page. I have a button, namely [Add as friend] using the basic <button></button> clauses. What would be the best workaround to do this?

For example, I'd like the SQL statment: INSERT into friends (uid, fid, isapproved) VALUES ('1', '2', '1') when the [Add this friend] button is clicked.

I'm working with just php and MySQL for now. Any suggestion would be more than welcome. Thanks, guys.

Upvotes: 0

Views: 2693

Answers (4)

devasia2112
devasia2112

Reputation: 6016

If you are not using AJAX, something like this should be enough.

addfriend.tpl.php file

<form method="post" action="action_addfriend.php">
 <input type="hidden" value="<?=$userid;?>" name="userid" />
 <input type="submit" value="Add as Friend" />
</form>

action_addfriend.php file

<?php

if ($_POST)
{  
  $fid = htmlspecialchars(stripslashes($_POST['userid']));
  $fid = mysql_real_escape_string($fid);
  include("your_connectdb_file.php");
  mysql_query("INSERT into friends (uid, fid, isapproved) VALUES ('1', '$fid', '1')");
  mysql_free_result();
}
?>

Upvotes: 1

grisevg
grisevg

Reputation: 250

send AJAX request with POST and/or GET data to url responsible for adding friends, eg: mysupersocialnetwork.org/friend/add/ this page request will trigger php and php will execute script.

Upvotes: 0

Kypros
Kypros

Reputation: 2986

If you are only using PHP as a scripting language without any javascript, then you should make that button direct to a page which handles the add friend request for the given user_id.

For a friend addition to work on the same page you would need to use javascript and Ajax to make a background call to a php file which will handle the request silently and return a response if it were successful.

Upvotes: 0

Nanne
Nanne

Reputation: 64399

  • Button calls javascript function.
  • Javascript function posts (AJAX,possibly using jquery) to a php file 'addfriend'
  • PHP file executes mysql, returns JSON
  • Javascript function calls callback and shows return value

Upvotes: 3

Related Questions