How to pass php variable's value to jquery

How to pass php variable's value to jquery

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Pass PHP Variable Values to jQuery

You have a PHP variable, say $name_of_current_page, and you need that data inside your JavaScript context. The most common instinct is to echo the value directly into a string literal within your script tag:

$(document).ready(function () {
    var currentPage = "<?php echo $name_of_current_page; ?>";
});

This pattern works in theory, but it introduces significant fragility into your application. When you mix PHP logic and JavaScript syntax inside a single string, you create a maintenance nightmare. If $name_of_current_page contains a single quote character (e.g., It's the new name), the resulting JavaScript will break immediately because the string literal terminates prematurely. Furthermore, if the variable is empty or null, your application might crash with a cryptic syntax error rather than handling the missing data gracefully.

The robust solution separates concerns by moving the PHP execution to the server-side view layer and passing the raw value as an attribute on a DOM element. This approach relies on standard HTML attributes, which are universally supported and easy for developers to inspect in their browser's Developer Tools.

Instead of embedding the PHP directly inside the JavaScript logic, render the variable as an id or data-* attribute on your main container or a specific element:

<div id="page-context" data-current-page="<?php echo e($name_of_current_page); ?>">
    <!-- Your content here -->
</div>

Notice the use of Laravel's {!! $variable !!} or e() helper depending on your security needs. If $name_of_current_page contains HTML tags (like <span>), using e() escapes them, preventing XSS attacks. If you intend to render user-generated content inside that variable later, you must switch to unescaped output. This distinction is critical; never trust server-side variables without understanding the context of their content.

Once the attribute is set on the DOM, jQuery can retrieve it safely using .data(). This method automatically parses the string value and handles type conversion for numbers, though for strings, it returns the raw text. Here is how you access that value within your event handlers:

$(document).ready(function () {
    var currentPage = $('#page-context').data('current-page');

    console.log(currentPage); // Outputs the value of $name_of_current_page
});

This pattern scales exceptionally well. If you need to pass multiple variables, such as a title and an ID, simply add more data-* attributes:

<div id="page-context" 
     data-title="<?php echo e($title); ?>" 
     data-id="<?php echo $id; ?>">
</div>

And in JavaScript:

var title = $('#page-context').data('title');
var pageId = $('#page-context').data('id');

By decoupling the data injection from the logic execution, you ensure that your JavaScript remains pure and independent of server-side syntax errors. This separation also improves performance slightly, as jQuery parses simple attributes faster than it would if it had to re-evaluate complex string literals on every load.

If you are working within a Laravel environment and managing assets via Mix or Vite, consider injecting these variables directly into your JavaScript file during the build process using mix.js() or Vite. This keeps your final production bundle clean of inline PHP tags while still allowing dynamic values to be injected by the compiler if necessary.

For complex scenarios where you need to pass large objects or arrays, JSON serialization is the industry standard. You can convert the PHP array to a JSON string using json_encode() on the server and parse it back in JavaScript with JSON.parse(). This ensures that special characters are properly escaped for both languages without manual intervention.

Our network