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

  • Home
  • SEARCH
  • 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 5967855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:00:17+00:00 2026-05-22T20:00:17+00:00

i want to pass a javascript string to php … WHICH is RIGHT after

  • 0

i want to pass a javascript string to php … WHICH is RIGHT after the code .. in the script.

<script type="text/javascript">
  var myvar = "mytext" ;

 <?php echo myvar ; ?>
</script>

this does not work.
What should i do ?

  • 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-22T20:00:18+00:00Added an answer on May 22, 2026 at 8:00 pm

    When someone visits a website, this is generally what happens:

    1. Their browser sends a request to the server.
    2. The server evaluates that request.
    3. The server realizes, “Egad, the page they’re requesting has PHP!”
    4. The server evaluates the PHP, and only sends the results to the browser.
    5. The browser parses the content that it receives.
    6. The browser realizes, “Egad, the page I received has JavaScript!”
    7. The browser evaluates the JavaScript, entirely on the client’s machine.

    So PHP and JavaScript are basically at different ends of the process. Only the server handles PHP, and only the client handles JavaScript.

    To “give” a string to PHP, you’d have to make a request of the PHP page, sending that string as a GET variable:

    http://www.yourdomain.com/some_php_page.php?myvar=mytext
    

    There are a few ways to do this with JavaScript.

    1. If you only care about making that request on the PHP page, and you don’t need to worry about receiving any information back, you can just create an image and use the URL as the source:

      var fakeImg = new Image();
      fakeImg.src = 'http://www.yourdomain.com/some_php_page.php?myvar=mytext';
      

      Even though you’re requesting an image, the server doesn’t know that, and will process your request by calling the PHP evaluating it, etc.

    2. You can make an actual AJAX request. Start by creating an XMLHttpRequest object:

      var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
      

      There are some issues in IE with cached responses on AJAX requests, so make the url unique:

      var url = 'http://www.yourdomain.com/some_php_page.php?myvar=mytext&unique=whatever';
      

      Tell your XHR where you want it to go, and how you want it to get there:

      xhr.open('GET', url, true);
      // The "true" parameter tells it that we want this to be asynchronous
      

      Set up a method that will check for when a response is received:

      xhr.onreadystatechange = function () {
          if (xhr.readyState === 4 && xhr.status < 400) {
              success(xhr.responseText);
          }
      };
      

      And finally, send the request:

      xhr.send(null);
      // We set "null" because some browsers are pissy
      

      Some notes to keep in mind:

      • You have to build the success function yourself, to handle the string that your PHP page will return.
      • You can pass that function xhr.responseXML if you want, but that’s usually just a hassle for me.
      • Using onreadystatechange the way I have will (I believe) introduce memory leaks in some versions of IE
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to pass a string which contains Spanish text that could be
I am writing PHP code where I want to pass the session id myself
I want to pass a variable to a UIButton action, for example NSString *string=@one;
I'm playing with the flickr api and php. I want to pass some information
Objective: I would like to take a JSON String and pass it into JavaScript
I want to pass an integer value to a form in .Net so that
I want to pass an int list (List) as a declarative property to a
I want to pass an enum value as command parameter in WPF, using something
I want to pass in the tType of a class to a function, and
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the

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.