1、将javascript写成js文件,然后在php文件中将js文件放到<head></head>部分。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php加载js的方法</title>
<script language="javascript" type="text/javascript"  src="NotNullCheck.js">
</script>
</head>
<body>
<h1 align="center">php加载js的方法</h1>


2、直接将javascript脚本写到php文件中。<head></head>或<body></body>都可。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试连接数据库</title>
<script language="javascript" type="text/javascript" >
function notnullcheck()
{
    var hostname=document.getElementById("host").value;
    var name=document.getElementById("username").value;
    var pass=document.getElementById("password").value;
    if(hostname=="")
    {
        alert("服务器地址不能为空!");
        return false;
    }
        if(name=="")
    {
        alert("用户名不能为空!");
        return false;
    }
        if(pass=="")
    {
        alert("密码不能为空!");
        return false;
    }
}
</script>
</head>
<body>
<h1 align="center">数据库连接测试</h1>
</body>

3、通过

echo "<script type='text/javascript' language='javascript' src='NotNullCheck.js'></script>";

加载js文件。