Hello

I am working on a web page(php based). I want to put a `like` button, so that when a user clicks it, he is added to the likers list in my database.(like in the facebook). I wrote a php funtion to execute this database insertion, but since the event should be fired with button click, i cannot execute the php function without redirecting or refreshing the page.

Can you give me assistance on how to make a database insertion without refreshing or redirecting the page.

Thank you very much

Recommended Answers

All 8 Replies

You need to use ajax.

Simple example... firstly create addlike.php file

<?php
// connect with the database
// sql query for "like"
// close the connection
?>

...and with ajax call the addlike.php

Ajax tutorial, simple ajax library. :)

Member Avatar for rajarajan2017

To execute PHP code:

<form name="frm1" action="Like.php" method="POST">
<input type="text" name="username" value=""/>
<input type="submit" name="commit" value="Submit"/>
</form>

Like.php:

<?php
$uname=$_POST['username'];
//Establish the connection
//Write the insert query to update the liked uname into the database
?>

Thats it! No need of AJAX!

rajarajan07, thanks for your reply. As I haven't tried it yet, I have a question to ask. How can javascript, which forks on the client side can reach and execute server side .php files?

Member Avatar for rajarajan2017

It will not execute server side files, we can just pass the variable to javascript from php. or we can call a php file from javascript function like window.location="delete.php". Thats it! we can't do more from javascript since it was a client side script functions.

I will respond asw soon as I try it. Thanks for your replies already. Daniweb users are fast (:

i m using html page.i want to call php function on button click of html form.
how can i do it?
plz help me

It will not execute server side files, we can just pass the variable to javascript from php. or we can call a php file from javascript function like window.location="delete.php". Thats it! we can't do more from javascript since it was a client side script functions.

I agree with Krstevski, using AJAX for this is a perfect example of its usage.

Raja's example of the form and php script will redirect the user away from the page and to a php script every time they click a like button. It will work but will require two page loads to effectively get back to where the user already was.

  1. The user's ID is passed to a javscript function
  2. The function takes that value and makes a POST request to a php script
  3. The php script takes that value via $_POST and makes a database request liking the page by the user
  4. The php script can then return data back to the ajax request, usually via JSON or XML.
  5. The javascript function updates the button to a "liked" button instead of "like"

There are a lot of ways to accomplish this. Me personally I like working with jQuery for ajax and dom manipulation (http://api.jquery.com/jQuery.ajax/) but you could also implement this yourself without an external library if you're proficient enough in javascript.

In that case you have to use AJAX call to a php page and then in that PHP page u can do whatever u want to do i.e.,either storing result or any calcualtion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.