我在做留言板作业的时候,需要在用户点击提交成功后,alert("成功")然后跳转到index.php;
我把这两个JS代码写在一个JS方法里面:
function go(){
alert('成功');
location.href="index.php";
}
然后在PHP调用,if (file_put_contents($filename, $newfile)) {
echo "";
} else {
echo "";
}
我的页面结构是这样
function go(){
alert('成功');
location.href="index.php";
}
header("content-type:text/html;charset=utf-8");
date_default_timezone_set('PRC');//设置默认时区
$msg=[];
$filename='message.txt';
if(file_exists($filename)){
$text=file_get_contents($filename);//获取文件内容
if(strlen($text)>0){
$msg=unserialize($text);//反序列化;
// var_dump($msg);
}
}
if(isset($_POST['mybtn'])){//用户点击提交按钮后,我发现好奇怪啊,如果我把按钮单独拿出来定义会报noctice,说这个变量undefined;但是放在里面这里却没事
$username = strip_tags($_POST['username']);
$title = strip_tags($_POST['title']);
$time=date('Y-m-d h:i:s',time());
$content = strip_tags($_POST['content']);
$newMsg=compact(username,title,time,content);//快速创建数组
array_push($msg, $newMsg);//把数组推进unserialize后的文件数组里面
$newfile = serialize($msg);//序列化
if (file_put_contents($filename, $newfile)) {
echo "";
} else {
echo "";
}
}
?>
html>
留言板测试V1
说点什么吧
请输入留言信息
请留言
用户名
标题
内容
发布留言
这样写没问题,但是如果我在JS代码,加入一个window.onload
window.οnlοad=function(){
function go(){
alert('成功');
location.href="index.php";
}
}
我这么写是希望他加载页面完成的时候,就给我声明这个函数方法
但是这样写,当PHP调用go()函数的时候,会报undefined,
如果我把这个函数声明用script标签包住放在body闭合标签前面,也就是页面末尾,也会报undefined