如何在.php页面中添加外部javascript页面



所以我的。php文件以前是一个。html页面,但是我想在这个文件中添加一个会话,并使用javascript来验证我的表单。但是当我添加标签时,什么也没发生。当我的文件是。html时,它工作得很好…下面是我到目前为止的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
?>
 <html>
      <html>
      <head> 
          <title>Hompage</title> 
         <script type="text/javascript" src="my_js.js"></script> 
      </head> 
      <body style = "margin-left:5%;"> 
           <form method = "post" id = "Form1" action = "my_js.js">
           <b>Login:</b>
           <br/>
           <br/>
                 User Name: &nbsp &nbsp &nbsp 
                 <input type="text" name="uname" id ="uname"/> 
                 <br/>
                 <br/>
                 Password: &nbsp &nbsp &nbsp 
                 <input type="text" name="pwd" id ="pwd"/> 
                 <br/>
                 <br/>
                 <input type ="button" id="Login" value ="Login" onclick = "validate();"/>
           </form>  
           <br/><br/>
           <hr/>    
           <br/><br/>
           <form method = "post" name = "Form2" id = "Form2" action="register_add.php">
           <b>Register:</b>
           <br/>
           <br/>
                 <span style = "color: red;">*</span> First Name:&nbsp &nbsp &nbsp 
                 <input type="text" name="fname" id ="fname"/> 
                 <br/>
                 <br/>
                 <span style = "color: red;">*</span> Last Name: &nbsp &nbsp &nbsp 
                 <input type="text" name="lname" id ="lname"/>
                 <br/>
                 <br/>
                 <span style = "color: red;">*</span> Date of Birth: &nbsp &nbsp &nbsp 
                 <input type = "text" name = "dob" id ="y" value = "yyyy/mm/dd">
                 <br/>
                 <br/>
                 <span style = "color: red;">*</span> User Name: &nbsp &nbsp &nbsp 
                 <input type="text" name="uname" id ="uname"/>
                 <br/>
                 <br/>
                 <span style = "color: red;">*</span> Password: &nbsp &nbsp &nbsp 
                 <input type="password" name="pwd" id ="pwd"/>
                 <br/>
                 <br/>
                 <input type ="Submit" id="Register" value ="Register"/>
           </form>

      </body> 
</html> 

和我的。js文件:

// Login Form Validation
function validate(){
      var x=document.forms["Form1"]["uname"].value; 
      var y=document.forms["Form1"]["pwd"].value;
      if (x==null || x=="" || uname == null){ 
      //alert("User name must be filled out"); 
        document.getElementById('para1').innerHTML = "User Name field must be filled out";
        return false; 
                }
      if (y==null || y=="" || pwd==null){
        document.getElementById('para1').innerHTML = "Password field must be filled out";
        return false;
                }
      else{
        document.getElementById('Form1').innerHTML="User Name: " + x + "nPassword: " + y;
                    }
    // Registration Form Validation
    var a=document.forms["Form2"]["fname"].value;
    var b=document.forms["Form2"]["lname"].value;
    var c=document.forms["Form2"]["m"].value;
    var d=document.forms["Form2"]["d"].value;
    var e=document.forms["Form2"]["y"].value;
    var f=document.forms["Form2"]["uname"].value;
    var g=document.forms["Form2"]["pwd"].value;
    if (var==null || var==""){
         document.getElementById('para2').innerHTML = "All field with '*' must be filled out";
         return false;
            }
     else{
        document.getElementById('Form2').innerHTML="First Name: " + a + "nLast Name: " + b +"nDate of Birth: " + m + d + y + "nUsername: " + uname + "nPassword: " + pwd;
            }
    } 

看来你的javascript有一些错误。记得使用开发工具(chrome/IE中的F12)并检查控制台。

更新后的代码和.js如下…我所做的是:

  • 删除所有的var =,因为它们在我的端抛出错误。

  • 您的登录和注册的错误消息(id="para1"和div id="Para2")没有在页面上的元素,所以我添加了它们。

  • 我完成了注册测试代码(一些表单元素,其中错过命名和抛出错误…注释添加到JS第21行)。

你要做的是分开这些方法(ValidateLogin()和validaterregistration())或使它们更通用通过循环通过表单变量检查所需的属性可能?

工作代码:

HTML:

<html>
  <head> 
     <title>Hompage</title> 
     <script type="text/javascript" src="my_js.js"></script> 
  </head> 
<?php
session_start();
?>
  <body style = "margin-left:5%;"> 
       <form method = "post" id = "Form1" action = "my_js.js">
       <b>Login:</b>
       <br/>
       <br/>
             User Name: &nbsp &nbsp &nbsp 
             <input type="text" name="uname" id ="uname"/> 
             <br/>
             <br/>
             Password: &nbsp &nbsp &nbsp 
             <input type="text" name="pwd" id ="pwd"/> 
            <div id="para1"></div>
             <br/>
             <br/>
             <input type ="button" id="Login" value ="Login" onclick = "validate();"/>
       </form>
       <br/>
       <br/>
       <hr/>   
       <br/>
       <br/>
       <form method = "post" name = "Form2" id = "Form2" action="register_add.php" >
       <b>Register:</b>
       <br/>
       <br/>
             <span style = "color: red;">*</span> First Name:&nbsp &nbsp &nbsp 
             <input type="text" name="fname" id ="fname"/> 
             <br/>
             <br/>
             <span style = "color: red;">*</span> Last Name: &nbsp &nbsp &nbsp 
             <input type="text" name="lname" id ="lname"/>
             <br/>
             <br/>
             <span style = "color: red;">*</span> Date of Birth: &nbsp &nbsp &nbsp 
             <input type = "text" name = "dob" id ="y" value = "yyyy/mm/dd">
             <br/>
             <br/>
             <span style = "color: red;">*</span> User Name: &nbsp &nbsp &nbsp 
             <input type="text" name="uname" id ="uname"/>
             <br/>
             <br/>
             <span style = "color: red;">*</span> Password: &nbsp &nbsp &nbsp 
             <input type="password" name="pwd" id ="pwd"/>
             <br/>
             <br/>
             <div id="para2"></div>
             <input type ="button" id="Register" value ="Register" onclick = "validate();"/>
       </form>

  </body> 
</html> 

JS脚本:

function validate() {
    x = document.forms["Form1"]["uname"].value;
    y = document.forms["Form1"]["pwd"].value;
    if (x == null || x == "" || uname == null) {
        //alert("User name must be filled out"); 
        document.getElementById('para1').innerHTML = "User Name field must be filled out";
        return false;
    }
    if (y == null || y == "" || pwd == null) {
        document.getElementById('para1').innerHTML = "Password field must be filled out";
        return false;
    } else {
        document.getElementById('Form1').innerHTML = "User Name: " + x + "nPassword: " + y;
    }
    // Registration Form Validation
    a = document.forms["Form2"]["fname"].value;
    b = document.forms["Form2"]["lname"].value;
    dob = document.forms["Form2"]["dob"].value;
    ///These values do not exist in the form...commenting them out 
    //c=document.forms["Form2"]["m"].value;
    //d=document.forms["Form2"]["d"].value;
    //e=document.forms["Form2"]["y"].value;
    f = document.forms["Form2"]["uname"].value;
    g = document.forms["Form2"]["pwd"].value;
    if ((a == null || a == "") || (b == null || b == "") || (dob == null || dob == "") || (f == null || f == "") || (g == null || g == "")) {
        document.getElementById('para2').innerHTML = "All field with '*' must be filled out";
        return false;
    } else {
        document.getElementById('Form2').innerHTML = "First Name: " + a + "nLast Name: " + b + "nDate of Birth: " + m + d + y + "nUsername: " + uname + "nPassword: " + pwd;
    }
}

你必须把

<?php
session_start();
?>

之前
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

相关内容

  • 没有找到相关文章

最新更新