Getting data from the php script using Ajax

advertisements

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 ??


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