I'm attempting to pass a JavaScript function into a php file using Ajax, as far as I can see my Ajax syntax is right but it doesn't seem to work. This is the first time I've tried to use Ajax so any help would be much appreciated.
I made some test files just to test if the Ajax code passes the variable and I'll put it below -
script.js -
var number1 = Math.round(Math.random() * 6) + 1;
var number2 = Math.round(Math.random() * 6) + 1;
var randomAnswer = number1 + number2;
$ (document).ready(function() {
return $.ajax({
url: 'index.php',
type: 'POST',
data: randomAnswer,
});
});
index.php -
<script src = "jquery-3.1.1.min.js"></script>
<script src = "script.js"></script>
<?php
$answer = $_POST ['randomAnswer'];
echo $answer;
?>