帮助JavaScript警报

时间:2009-07-31 18:41:57

标签: javascript

function msg ($msg) {

 echo "<script type='text/javascript'>alert('$msg');</script>";


}

if (count($_POST)) {
   msg('TEST!!!!!');
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website</title>
<link rel="stylesheet" type="text/css" href="/assets/css/main.css" />   
</head>
<body>

<div id="content">
<form action="/user/register" method="post">

<table cellspacing="2" cellpadding="0">
<tr>

 <td>Username:</td>
 <td><input name="username" maxlength="25" size="25" type="text" />
 </td>
</tr>

<tr>
 <td>E-mail:</td>
 <td><input name="email" maxlength="255" size="25" type="text" /></td>
</tr>

<tr>
 <td>Password:</td>
 <td><input name="password" maxlength="255" size="25" type="password" /></td>
</tr>

<tr>
 <td>Confirm password:</td>
 <td><input name="password2" maxlength="255" size="25" type="password" /></td>
</tr>

<tr>

 <td>&nbsp;</td>
 <td><input type="submit" value="Register" /></td>
</tr>

</table></div>

</body>
</html>

当我发布它时,doews显示警告但在空白页面上。当我按提交时,表单消失了。我怎么能这样做仍然显示html?

消息弹出但在空白页面上。我希望它仍然显示您的页面

2 个答案:

答案 0 :(得分:2)

首先,您需要将脚本放在标签中,而不是放在DOCTYPE上方。试试这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website</title>
<link rel="stylesheet" type="text/css" href="/assets/css/main.css" />   
<?php
if (count($_POST)) {
   msg('TEST!!!!!');
}
?>
</head>

之后,可能会出现更多问题。

答案 1 :(得分:0)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website</title>
<link rel="stylesheet" type="text/css" href="/assets/css/main.css" /> 
<script type='text/javascript'>
(function(){
   var countPost = <?php echo ((count($_POST)>0)?count($_POST):0); ?>;
   if (countPost>0)
      alert('msg test');
})()
</script>
</head>