Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在.php页面中添加外部javascript页面?_Javascript_Php_Netbeans - Fatal编程技术网

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

如何在.php页面中添加外部javascript页面?,javascript,php,netbeans,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文件以前是一个.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> 
你必须把

<?php
session_start();
?>

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

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

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

  • 删除所有的var=因为他们在我这边抛出错误

  • 您的登录和注册错误消息(id=“para1”和div id=“Para2”)在页面上没有这些元素,因此我添加了它们

  • 我完成了注册测试代码(一些表单元素,其中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> 

my_js.js
文件是否与此php文件位于同一目录中?谢谢!!这有助于我的会话想法,并且我能够让我的用户保持登录状态,以便他们可以在站点上执行多个操作:)非常感谢,我完全忘记删除那些额外的变量。。。它现在起作用了,我多么希望它。。。
<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> 
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;
    }
}