当我在html标记内运行时,函数debug_to_console不起作用。我应该将功能定位在其他地方吗?或者我应该在代码中再次声明该函数?另外,php中是否有功能范围?
编辑:看起来函数只有在if($ _ SERVER [“REQUEST_METHOD”] ==“POST”)循环
内部才会失败的index.php
<?php
function debug_to_console($data) {
if(is_array($data) || is_object($data))
{
echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");
}
}
?>
<?php
/*this worked*/
debug_to_console("ok");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" content="learning process">
<!--Javascript-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src= "web.js"></script>
</head>
<body>
<form id="email_re" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="post">
<input type="text" name="email" placeholder="Type in your email adress"></input><input type="submit" value="submit"></input></li></form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$email=$_POST["email"];
$message="You have successfully registered";
mail($email, 'My Subject', $message);
/*this one failed*/
debug_to_console("Done");
}
/*this one failed*/
debug_to_console("Done");
?>
</body>
</html>
的javascript:
$(document).ready(function () {
$(document).on("submit", "#email_re", function(event){
event.preventDefault();
$.ajax({
type:"POST",
url:"index.php",
data:$("#email_re").serialize(),
success:function(){
console.log("Great2");
}
})
});
})
这就是网站现在的样子http://cs20p.site11.com/