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

时间:2014-05-19 18:14:24

标签: javascript php netbeans

所以我的.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;
            }
    } 

2 个答案:

答案 0 :(得分:0)

你必须把

<?php
session_start();
?>

之前的

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

答案 1 :(得分:0)

您的javascript似乎有些错误。请记住使用开发工具(chrome / IE中的F12)并检查控制台。

更新的代码和.js低于......我做的是:

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

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

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

您要做的是将这些方法(ValidateLogin()和ValidateRegistration())分开,或者通过循环遍历表单变量来检查所需的属性,使它们更通用?

工作代码:

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;
    }
}