Passing a Javascript string to PHP

后端 未结 4 892
傲寒
傲寒 2021-01-20 08:21

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



        
相关标签:
4条回答
  • 2021-01-20 08:25

    You should get the difference between client side and server side code clear. The variable you are introducing in the php code isn't assigned before because that variable is set at the client. So your code example is in essence wrong. If you want a value that is present at the client (javascript) to be available at the server (php), you need to do something with the xmlhttprequest object of javascript (also know as ajax).

    You can do the other way around though...print a php value in javascript. This is because the script is than created server side and send to the client before it is being processed by the browser.

    Not sure what you are trying to reach but maybe this helps a bit.

    0 讨论(0)
  • 2021-01-20 08:26

    Your example is somewhat confusing:

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

    Because if I do this:

    <script type="text/javascript">
      <?php $myvar = "mytext"; ?>
      var myvar = "<?php echo $myvar; ?>" ;
    </script>
    

    Then it sets the JavaScript value of myvar to the PHP value of $myvar so they both stay the same. If you're trying to do something else you need to expand your example.

    0 讨论(0)
  • 2021-01-20 08:37

    PHP is executed server side while javascript is client side, so that means that the PHP is already executed when you're sending your javascript code.

    You might want to look into AJAX instead.

    0 讨论(0)
  • 2021-01-20 08:40

    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 讨论(0)
提交回复
热议问题