0

i'm new to this so i hope you could help me.

i'm try to build one page has the ability to add and remove from sqldb.

from some reason i have to send value from js to php and i try this basic code and it doesn't work .

<html>
<head>
      <script src="resources/js/jquery-2.2.3.js"> </script>
</head>
<body>

    <form method="get">
        <input type="button" value="submit" name="submit" class="test">
    </form>

    <div id="state"></div>
</body> 

<script type="text/javascript">
    $('.test').click(passValue);



    function passValue(e){

          document.getElementById('state').innerHTML="button is clicked";

        var js_var = "xml recevid";

        var xhr = new XMLHttpRequest();


            var url = 'test.php?js_var=' + js_var;
            xhr.open('GET', url, false);
            xhr.onreadystatechange = function () {
                if (xhr.readyState===4 && xhr.status===200) {

                    document.getElementById('state')xhr.responseText;

                }
            }
            xhr.send();

    }

</script>

<?php

 if (isset($_GET['js_var'])) $php_var = $_GET['js_var'];

    else $php_var = "<br />js_var is not set!";
    echo $php_var;

?>
Chandan Rai
  • 9,879
  • 2
  • 20
  • 28
  • 1
    You need to clarify _"doesn't work"_. Errors in the console? Checked the browsers network tab what the `test.php` actually returns? Checked your error log for PHP-errors? You might also want to put a `e.preventDefault()` in your `passValue`-function to stop the form from submitting. – M. Eriksson Jan 23 '17 at 13:40
  • Change `document.getElementById('state')xhr.responseText;` to `document.getElementById('state').innerHTML = xhr.responseText;` – tklg Jan 23 '17 at 13:44
  • Btw, is your PHP-code on the same page as the javascript? Then your ajax call will return the – M. Eriksson Jan 23 '17 at 13:48
  • sorry i fixed this to document.getElementById('state').innerHTML.responseText; but it's not the problematic, when i click the button submit the state innerHtml doesn't change to "xml recevid" – user7443732 Jan 23 '17 at 13:51
  • this what i got in console Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. – user7443732 Jan 23 '17 at 14:23

1 Answers1

-2

USE $php_var ="<script type='text/javascript'>document.write('js_var');</script>";

NOTE THAT js_var shoul be a global variable, not local one.

michaelitoh
  • 2,317
  • 14
  • 26
  • What would this accomplish? It would just give him a PHP variable with the literal text ``. You can't run javascript in PHP. – M. Eriksson Jan 23 '17 at 13:43
  • Yes, you can. try $php_var = ''; – michaelitoh Jan 23 '17 at 13:56
  • Like I said, that will only give you a variable _with that literal text_. If you echo it, it will be sent to the browser, which will parse it as javascript. PHP will _never_ be able to use the _results_ from that javascript snippet. PHP get's parsed on the server, the results will be sent to the client that will parse any html/js/css. – M. Eriksson Jan 23 '17 at 13:58
  • As I said before, put js variable with you any function and them... it'll run correctly. – michaelitoh Jan 23 '17 at 14:02
  • Man i mean the js variable put with out a javascript function like this: var js_var = "some value"; not function someFunction(){var js_var = "xml recevid";} – michaelitoh Jan 23 '17 at 14:03
  • i just got the literal text as Magnus Eriksson said. thanks – user7443732 Jan 23 '17 at 14:25