Pass JavaScript into PHP using Ajax
<p>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.</p>

<p>I made some test files just to test if the Ajax code passes the variable and I'll put it below - </p>

<p>script.js -</p>

<pre><code>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,
    });
});
</code></pre>

<p>index.php - </p>

<pre><code>&lt;script src = "jquery-3.1.1.min.js"&gt;&lt;/script&gt;
&lt;script src = "script.js"&gt;&lt;/script&gt;

&lt;?php
$answer = $_POST ['randomAnswer'];
echo $answer;
?&gt;
</code></pre>