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?
outputvariable in javascript. So you just need to echo your return value or depending on what type of data the return value fromsome_functionis maybeecho json_encode($return)(for example if was an array).