I'm trying to send variables of username and password to a PHP file from a JS file, but the ajax request always fails (prints fail), no idea why.
JS file:
<script>
var imported = document.createElement('script');
imported.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js';
document.querySelector('head').appendChild(imported);
function collect() {
var user = "joe";
var pass = "123";
$.ajax({
type:'POST',
url: 'myphp.php',
data: {'username': user, 'password': pass},
success: function(){
alert("success!");
},
error: function(){
alert("fail");
}
});
}
</script>
PHP file:
<?php
$myval = $_POST['username'];
echo($myval);
?>