0

I am trying to use some data calculated with a PHP script in JavaScript, this is my code:

<html>
<head>

        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
    <body>
        <button onclick="duplicate()"> button send</button>


        <script>
        function duplicate() 
            {   
                    var action = "CreationBoard";
                    alert(action);
                    $.ajax({
                        type : "POST",
                        url  : "file.php",
                        data : { action : action },
                        success: function(output) {
                            alert(output);
                        }
                    });
            }
        </script>
    </body>
</html>

So I'm calling file.php to calculate my data:

<?php
if(isset($_POST['action']))
{
    $return = some_function($_POST['action']);
}
?>

I need to use $return in JavaScript, how to do that? I've seen many examples, but they only show how to send data with Ajax to PHP, not from PHP to ajax. How should I do that?

1
  • whatever is output from the php script is what will be in the output variable in javascript. So you just need to echo your return value or depending on what type of data the return value from some_function is maybe echo json_encode($return) (for example if was an array). Commented Mar 2, 2015 at 20:09

2 Answers 2

2

All you need to do

<?php
if(isset($_POST['action']))
{
    $return = some_function($_POST['action']);
    echo $ return
}
?>

Javascript doesn't know php variable. All it know is the data returned

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

1 Comment

I tried this but I can't use output outside ajax, even if i use another variable, how can I use it ??
1

In your file.php I would do

echo json_encode($return);

And then modify your ajax to expect a json return:

function duplicate() 
{   
        var action = "CreationBoard";
        alert(action);
        $.ajax({
            type : "POST",
            url  : "file.php",
            data : { action : action },
            dataType: 'json', 
            success: function(output) {
                alert(output);
            }
        });
}

the output variable that you have in your alert should look like your array and you can do with that what you want.

2 Comments

I tried this but I can't use output outside ajax, even if i use another variable, how can I use it ??
Normally you would just do what you need to do inside that ajax success. If you absolutely needed to do something with them outside of that you could always set a cookie inside that success and retrieve them in other parts of your code or you could write the values to a hidden div or form and retrieve them later. Both might be a little hacky but if you can I'd say try to do everything within the success function.

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.