I'm a novice in Javascript and Ajax, but with some help of the internet I got halfway there. But now I'm stuck.
I'm sending some HTML form data to a PHP script using Ajax. The PHP file should return two values. When I put console.log(data);
in the success: function(data)
block, I can see an array of the two values in Console.
But how can I now use these two values in two separate variables?
Btw, the Ajax call is in the main html file, but I would need these two variables in a functions.js file, where all my other Javascript lives.
EDIT:
The console says [80,120]
.
A snippet from the bottom of index.html
<script src="assets/function.js"></script>
<script type="text/javascript">
$('#calculate').click(function() {
var val1 = $('#number').val();
$.ajax({
type: 'POST',
url: 'calc.php',
data: { number: val1 },
success: function(data) {
console.log(data);
}
});
});
</script>
calc.php
...some calculations which end in...
<?php
$Var1 = 80;
$Var2 = 120;
?>
function.js
...
var value1 = ??? //here I'd need the value of $Var1
var value2 = ??? //here I'd need the value of $Var1
...
further calculations