5

I want to send some data to Javascript from PHP.(these two file is different file in same folder) For example, If I calculate some value in PHP side, I want to send the data to javascript and I use the data. How can I do this??

5 Answers 5

6

There's complete technology for that called AJAX with a lot of tutorials on the internet.

And there's already a great and easy-to-deploy implementation - within jQuery.

Sign up to request clarification or add additional context in comments.

Comments

4

In practice, you can use this:

FILE: index.php

<HTML>
    <body>      
        <input type="text" id="test" value="123"><br>
        <input type="button" id="btn" onclick="send_to_php()" value="send to php">

        <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script>

                function send_to_php() {
                        $.ajax({
                            url: 'js_php.php',
                            type: 'POST',               
                            // Form data
                            data: function(){
                                var data = new FormData();
                                data.append('test', $("#test").val() );     
                                return data;
                            }(),
                            success: function (data) {
                                var obj = JSON.parse(data);
                                $("#test").val( obj.result );                   
                            },
                            error: function (data) {
                                console.log(data);
                            },
                            complete: function () {                 

                            },
                            cache: false,
                            contentType: false,
                            processData: false
                        });
                }

        </script>
    </body>
</HTML>

FILE: js_php.php

<?php
    //FILE: js_php.php

    $test = $_POST["test"];
    $test .= "456";

    $arrResult = array(     
        'result' => $test
    );          

    print json_encode($arrResult);
    die();
?>

The file "index.php" is the communication between JavaScript and PHP using jQuery Ajax method. When click the "send to php" button will run the "send_to_php ()" that will take the value of the input id "test" and send through ajax, for "js_php.php" file. In turn, the file "js_php.php" will receive this variable as POST, modify and print the value in JSON format. The method implemented by ajax function "send_to_php ()" will be "listening" all that "js_php.php" print.

After the success of the return, javascript convert the text "js_php.php" printed on a JSON object and then the JS able to process within the javascript code:

success: function (data) {
    var obj = JSON.parse (data);
    $("# test").val(obj.result);
},

Comments

3
<script type='text/javascript'>
var myVar = <?php echo $myVar; ?>;
</script>

in a nutshell. There are more sophisticated way to communicates though.

Comments

2

Have a Look at this AJAX Tutorial: http://news.php.net/php.general/219164

Comments

1

Assign a JavaScript global variable in a script tag in the PHP page, and include the other javascript files after.

Sample:

<html>
  <head>
    <script type='text/javascript'>var testGlobal = <?php echo $globalJSValue ?></script>
    <script type='text/javascript' src="url"></script>
    <script type='text/javascript' src ="url"></script>
  </head>
</html>

testGlobal variable will now be available to both javascript files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.