Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3395368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:16:54+00:00 2026-05-18T04:16:54+00:00

For passing dynamic values to js code, is it better to do like this?

  • 0

For passing dynamic values to js code, is it better to do like this?

var myVar = '<?php echo addslashes($_SESSION['myVar']); ?>';

Or make an AJAX call at run time to get the session variable and assign it to myVar?

What are the pros and cons?

I personally prefer the AJAX method. It makes the code look better in case of complex array assignments etc.

Would like to get expert opinions.

Updates

I understand that using the AJAX method, every http request counts. But if we have well defined functions inside the javascript files, we would only need to have the function parameters available. And the parameters can be passed in the function calls whenever we want to call them, e.g. like this:-

<a onclick="some_function('<?php echo $_SESSION['var'] ?>')">

This is how I normally do and in my project I rarely need to pass them inline in javascript files. And I have AJAX calls for doing server side processing (of course when it is needed).

The basic idea I have here is to have separate js files intead of inline js scripts. I was looking at this question. I would definitely not make an AJAX call for fetching each and every variable (as it may look like from my question) because such a situation would not arise.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:16:55+00:00Added an answer on May 18, 2026 at 4:16 am

    An AJAX call is another request, and the results will not be immediately available – writing dynamic data inline is available straight away. In this case, I would tend to choose the inline approach.

    PHP’s json_encode() might make it relatively easy to write out even huge arrays without cluttering the code much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm used to passing around string like this in my C++ applications: void foo(const
I'm trying to make a time with Time.local which in my code is dynamic.
i have a javascript function like this: function addfamily(divName){ var newdiv = document.createElement('div'); newdiv.innerHTML
I'm passing raw HTTP requests to an apache server (received by PHP). The request
I'm passing a reference of a form to a class. Within this class I
Passing an undimensioned array to the VB6's Ubound function will cause an error, so
When you're passing variables through your site using GET requests, do you validate (regular
If I am passing an object to a method, why should I use the
I'm currently passing the pid on the command line to the child, but is
I'm passing small (2-10 KB)XML documents as input to a WCF service. now I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.