How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Bruce Wilner Your Own Question
Bruce Wilner
Bruce Wilner,
Category: Programming
Satisfied Customers: 77
Experience:  principal at Network Security Labortories Inc.
Type Your Programming Question Here...
Bruce Wilner is online now
A new question is answered every 9 seconds

I have a javascript that needs to pass a variable to php or

Customer Question

I have a javascript that needs to pass a variable to php or to run a php script.
Can I use ajax to pass a variable to php and how would I do that?
Submitted: 11 months ago.
Category: Programming
Expert:  Russell H. replied 11 months ago.

Hi, thank you for contacting My name is Russell. I will do my best to provide the right answer to your question.

Try something along these lines, to pass a variable from JavaScript into PHP:




$(".clickable").click(function() {

var userID = $(this).attr('id');


type: "POST",

url: 'logtime.php',

data: "userID=" + userID,

success: function(data) { alert("success!"); }





// whatever php code you are doing things with



$uid = $_POST['userID'];

// Do whatever you want with the $uid




...where the

if isset

at the beginning, is to test whether the variable exists, before doing the POST to a PHP variable (which will be $uid ).

This is cribbed from this page:

If this answers your question, please rate my answer. A five-star rating would be appreciated!

If you need more info, or have any comments, use Reply To Expert to respond.


Customer: replied 11 months ago.
hey thanks for the response. I have been messing with your code and what wordpress has stated in their manual. (forgot to mention its a wordpress admin plugin):
I am using the first two blocks of code trying to learn how to pass the variable. When I call my_action_callback(); I get a return of 0. Why is the data not passing? This is straight from the handbook? lol
Expert:  Russell H. replied 11 months ago.

Admittedly I don't have a handbook for this particular operation.

I did not know there was a Wordpress context to the operation.

I fear I am not expert enough to advise you in Wordpress context programming in detail, so I have Opted Out, opening the case to other Experts (having at least established the basic context to the problem), with apologies for not getting farther with it.

Customer: replied 11 months ago.
okay, that is fine. I appreciate it. Thanks!
Expert:  Russell H. replied 11 months ago.

You're welcome. I have Opted Out.

Expert:  Bruce Wilner replied 11 months ago.

Seems to me that the Javascript should be in the same source file as the PHP code, and then you can follow Russell's earlier advice. If they live in different files, your safest option is to write the data to a disk file and then have the PHP code (operating from another source file) open that disk file and read the information. I can post the code if you need. I don't see offhand why such complication should be needed to pass things from here to there to here.