Mahmoud Jbor Mahmoud Jbor - 2 years ago 262
Javascript Question

How to use jQuery variable inside php code?

I have PHP code inside jQuery scriptm, and I want to pass a jQuery variable to PHP.

This is my code :

$(document).ready(function() {
$('.editclass').each(function() {
var Id = $(this).attr('id');
include "config.php";
$query="SELECT * FROM users WHERE UserId=\'id\'";



I want the value of id to be exist in php code (

Answer Source

Use $.post:

$(this).on('click', function(e){

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

    $.post("yourscript.php", { 
        Id: Id 
    }, function(data){

        var theResult = data;
}, 'json' );


This is going to send two parameters (param1 and param2 to a php script called yourscript.php. You can then use PHP to retrieve the values:

$Id= isset($_POST['Id']) ? $_POST['Id'] : '';

The idea is you're sending variables from the client side to the server side via Ajax.


     include "config.php";
     $query="SELECT * FROM users WHERE UserId=$Id";

    /* Get query results */

    $results = use_mysql_method_here();

    /* Send back to client */

    echo json_encode($results);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download