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 8926745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:03:02+00:00 2026-06-15T08:03:02+00:00

I am trying to inject a value stored in a PHP $_SESSION variable into

  • 0

I am trying to inject a value stored in a PHP $_SESSION variable into the “value” attribute of an input tag using javascript. I am able to successfully inject the PHP code in the “value” attribute, but the stored value does not appear in the text box. My code is below:

HTML:

<input type="text" name="fname" value="" />

Javascript:

fields.eq(n).attr('value', "<?php echo $_SESSION['fname'] ; ?>");

The problem is that this would usually place the value of the $_SESSION['fname'] variable in the input box. But that is not the case. When the button is clicked to inject the PHP code into the value attribute, this is what the HTML looks like:

<input type="text" name="fname" value="<?php echo $_SESSION['fname'] ; ?>" />

And this is what I want it to look like but in the text box, it actually shows the PHP code and not the value stored in $_SESSION['fname'].

Is there something I am missing? Shouldn’t this work? Any help is appreciated!

enter image description here

  • 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-06-15T08:03:03+00:00Added an answer on June 15, 2026 at 8:03 am

    PHP is a server-side script, which means: PHP will be executed on the server.

    JavaScript is a client script, which means: JavaScript will be executed by the browser.

    Usually by default, .php files will be processed by the PHP engine on the server, thus executing the PHP scripts. The result will be sent to the client. That’s why when you have a .php file, things between <?php and ?> will be processed by the PHP engine and you will not see the actual PHP script in the client. Sometimes you can configure the HTTP server such that .html files are also processed by the PHP engine.

    For other files, the HTTP server (e.g. Apache) will directly sent the whole file to the client, without being processed by the PHP engine, so that any PHP scripts will not be processed and will directly go to the client.

    Now your problem is that you tried to put some PHP into a JavaScript file (.js), and hoping that it will be executed, but in fact it will not. So when you try to set the value of your input element with your shown JavaScript, the “text” <?php echo $_SESSION['fname'] ; ?> will be set.

    If you either put the JavaScript into your .php file, or rename your JavaScript file (*.js) to *.php, you will get the value you desired.

    Or probably you could add an extra JavaScript in your page, setting a “global” variable with your PHP, and use that “global variable” in your .js file.
    i.e. window.myFname = <?php echo $_SESSION['fname'] ; ?>;


    Depending on what you want to do, you may need to choose how to design. If you just simply want to set the value, you may just simply do one of the two suggestions I’ve provided. If you want to get the value dynamically (i.e. obtain the value when you need it), you would like AJAX like others have suggested.

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

Sidebar

Related Questions

I am using FluentValidation and Ninject. I am trying to inject a service into
I'm trying to inject spring bean into JSF bean, I'm using Spring 3.1 and
I'm trying to inject an attribute into lxml.etree._Element , but as that module is
I'm trying to inject webapproot value into spring form like this <form:form action=${webappRoot}/user/create commandName=createUsr
I am trying to inject dependency to my session dictionary class into my controller's
I'm trying to inject a view using Ajax but it's not quite working. I
I am trying to inject JQuery into a UIWebView of a page that I
I am trying to use Spring to inject @PersistenceContext entityManager into my service. The
I am trying to properly use Ninject to inject log4net logging into my MVC3
I'm trying to inject a Map into a Grails controller. I want to inject

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.