0

I have a button on my page :

<button class="play"></button>

When I click on it, it launches the video via jQuery

$('.play').on('click',function(){
    player.play();
});

But I also want to add PHP code to save the user ID in the database when he clicks on that button.

How can I mix PHP & jQuery? I'm not used to Ajax, is it the solution?

Thanks for your help!

2
  • 2
    RTM api.jquery.com/jQuery.ajax Commented Sep 3, 2013 at 15:44
  • At a high level, yes you would use AJAX for this. You'd create some kind of server-side handler which accepts the user ID as an input (form POST, query string value, it doesn't much matter) and stores it as needed. Then you'd use the jQuery AJAX (.ajax(), .get(), .post(), whichever is easiest for you) to make a request to that server-side handler, sending it the value from the page. Commented Sep 3, 2013 at 15:48

2 Answers 2

2

add a function:

function add_to_db(user_id){
    $.post('add.php', {userId: user_id}, function(response){
        //do something with the response
        r = JSON.parse(response);
        if (r.status === 'error'){
        //handle the error
        alert(r.message);
        }
    });
});

when you play do the following

$('.play').on('click',function(){
    player.play();
    user_id = //however you chose to set this in the page
    add_to_db(user_id);
});

your add.php:

//perform your db update or insert
//if successful:
$response['status'] = 'success';
//else:
$response['status'] = 'error';
$response['message'] = 'update was not successful';
//look into try/catch blocks for more detailed error messaging.

//then send the response back
echo json_encode($response);
Sign up to request clarification or add additional context in comments.

6 Comments

You are missing a ' after error in the first block of code.
No problem. I do it all the time ;)
No need for r = JSON.parse(response);. You can do it automatically by appending "json" after the success callback
@Eagle. Really? so function(response, JSON)?
@SmithSmithy, in fact in this case it should be $.post(url,data,callback,"json"); Source: api.jquery.com/jQuery.post
|
0

Try using ajax

 $('.play').on('click',function(){
    player.play();
    $.ajax({
      type: "POST",
      url: "some.php",  // your php file name
      data:{id :"id"}, //pass your user id
     success:function(data)
    {
   if(data==true)
    alert("success");
else
  alert("error");
    }
    });
    }

some.php

<?php
$success=false;  //Declare and initialize a variable 

// do your code to update your database
//and check if the database is updated , if so set $success=true

echo $success;


?>

1 Comment

You should check and see if there were any errors. If there was a php error success would still be displayed even though that is not entirely true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.