Jump to content

Recommended Posts

 

Hello,

I had a web page which had sharing (share issue on facebook, twitter....) and I want to record what each user had shared. So, they will click on alink to share it. I want to call a php function when the user click on a link, whats the best method to do this?

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/251883-call-php-script-when-a-link-is-clicked/
Share on other sites

 

Hi mate

 

This would need to be an ajax call.

 

Easiest way to do it is with jquery.

 

Check out this link for info http://api.jquery.com/jQuery.ajax/

 

Drongo

 

 

 

 

 

Hello,

I had a web page which had sharing (share issue on facebook, twitter....) and I want to record what each user had shared. So, they will click on alink to share it. I want to call a php function when the user click on a link, whats the best method to do this?

Thanks in advance

Hi mate

 

To be more helpful :)

 

This represents your page with the link:

 


<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <script type="text/javascript" src="jquery.js" ></script>
        
    </head>
    <body>
        
        
        <script type="text/javascript">        
            $(document).ready(function() {
                $("#a").click(function() {
                    
                    $.ajax({
                        type: "POST",
                        url: "ajax.php",
                        data: "name=John&location=Boston"
                    }).done(function( msg ) {
                        alert( "Data Saved: " + msg );
                    });
                                   
                });
            });
        </script>     
        
<!-- YOUR TEST LINK -->
   <a href="#"  id="a"> TEST LINK </a>      
    </body>
</html>

 

 

Ajax bit explained

 

The type: POST - simulates posting the data - just like posting from a html form.

URL: This is the php script you want to send the data to.

Data: This is the data you want to send to the script. This is hardcoded below but could be represented by variables.

Done function : This is the function that will be executed once your ajax call has been successful and it will return whatever you send from the php script. So you can do anything to your page here.

 

 

PHP Script - ajax.php

 

 
<?php

$name = $_POST['name'];  //access the post data you send via the ajax script

echo "$name";  // whatver you echo or return will be sent back to the callback (Done function).



?>

 

That php script obviously just illustrates simply how it works. But you could do a switch statement to see what variable is being sent in by the ajax and that can then make a call to a specific function to perform an action relative to the link clicked.

 

Hope that helps.

 

If you need any more clarification just shout :)

 

Drongo

 

 

 

[/code]

 

is there any example related to my requirements.

thanks

You need to download jquery and save it in a file with .js extension. You can get it from the jquery website.

 

The ajax obviously won't work without the jquery library. Sorry probably should have made that clearer.

 

Drongo

 

 

I had the html in a file and ajax.php now when I click on the link, I can't see it working.

where the javascript file that is included in the html file?

Thanks

Hi Mate

 

By the way - you can download the jquery library from jquery.com

 

What exactly are you trying to achieve? If they click a link what is meant to be updated?

 

 

Also, when the user click on the link the php script will be called which will update the database & the user will be directed to the requested URL

thanks for your support :)

for example look:

 

I had this link: <a href="http://www.facebook.com/ShareSomething">Click here to Share</a>

So when the user clicks on that to share an item, then I need to increments the shares by 1 ,& direct him to the page.

Then you can probably do this a simpler way if that's all you need to achieve.

 

So you very simply change the link to be

 

 
<a href="YouScript.php?inc=1"  id=""> Your Link </a>

 

 

All you're doing here is sending the user to your script page and we've tacked on a little query string on the end of the link to act as a flag to the script (query string is the ?inc=1 bit) so you know the person clicked this link and didn't just  type in the uri.

 

The scrip then runs your db query and send the user to the facebook link.

 

Then on your script page you just do

 

  
<?php


if(isset ($_GET['inc'])){
     //Check to see if the inc var is set to be sure someone has clicked the link
    // Run database query to increment your counter. Can you do this?
    
    //Once you have run all database stuff successfully then change
   // header location to redirect the user
    header('Location: http://www.facebook.com/ShareSomething');
    
    
}
else {
   //If the inc variable isn't set then send them back to your site
    header('Location: http://www.BackToYourSite');
}
    

 

 

I assume you can work out the database bit?

 

Drongo

 

 

 

 

for example look:

 

I had this link: <a href="http://www.facebook.com/ShareSomething">Click here to Share</a>

So when the user clicks on that to share an item, then I need to increments the shares by 1 ,& direct him to the page.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.