javascript - Ajax is not calling php file
what is wrong with my code? He doesn't do the alert 'PHPPPPP' Only the success one
Html: Add to Log
<script>
function create () {
$.ajax({
url:"log.php", //the page containing php script
type: "post", //request type,
data: {hour: "1", min: "2", email: "3"},
success:function(result){
alert("passed");
}, error: function(data) {
alert(data);
}
});
}
</script>
Php(log.php):
echo "<script> alert('PHPPPPPPPPPP') </script> " ;
Answer
Solution:
You want to add the script to your document. Since you are using jQuery
:
<script>
function create () {
$.ajax({
url:"log.php", //the page containing php script
type: "post", //request type,
data: {hour: "1", min: "2", email: "3"},
success:function(result){
alert("passed");
$('body').append(result);
}, error: function(data) {
alert(data);
}
});
}
</script>
This will simply add the script
to the body
element. It would be better if you just omit the HTML and instead just echo
the string so you could alert
the result instead of adding a script to your document.