Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    External Javascript does not work

    Hi

    I'm trying to include a form validation script in a page made of templates. I could made it work, but now is not working. Can some one point me why?

    The script is located in a subdirectory in the root of the site called java.

    This is a custom setup of a WAMP server. The structure is like this:

    root.com
    |_divdev
    |__java (where the script resides)
    |__templates (where the header and the form templates resides)

    The form code is as follow:


    Code:
    <div class = "registerform">
      <div class = "registerheader">
        <?php echo $register['header']; ?>
      </div>
      
      <div class = "registertext">
        <?php echo $register['text']; ?>
      </div>
      
      <center>
      <form name = "registerform" class = "form" action="register.php" method="post" onsubmit="return validateregister()">
      <table width = 100%>
        <tr>
          <td width = 25% class = "registerformlabel"><?php echo $register['fname']; ?></td>
          <td width = 25%><input class = "registerforminput" name="fname" id="fname" type="text" tabindex = 1 /></td>
          <td width = 25% class = "registerformlabel"><?php echo $register['user']; ?></td>
          <td width = 25%><input class = "registerforminput" name="user" id="user" type="text" tabindex = 4 /></td>
        </tr>
        <tr>
          <td class = "registerformlabel"><?php echo $register['lname']; ?></td>
          <td><input class = "registerforminput" name="lname" id="lname" type="text" tabindex = 2 /></td>
          <td class = "registerformlabel"><?php echo $register['pwd']; ?></td>
          <td><input class = "registerforminput" name="pass1" id="pass1" type="password" tabindex = 5 /></td>
        </tr>
        <tr>
          <td class = "registerformlabel"><?php echo $register['email']; ?></td>
          <td><input class = "registerforminput" name="email" id="email" type="text" tabindex = 3 /></td>
          <td class = "registerformlabel"><?php echo $register['pwd2']; ?></td>
          <td><input class = "registerforminput" name="pass2" id="pass2" type="password" tabindex = 6 /></td>
        </tr>
        <tr>
        <td colspan = "4">&nbsp;</td>
        </tr>
        <tr>
        <td colspan = "4" align = "center"><input class = "submitbutton" type="submit" value="<?php echo $register['submit']; ?>" /></td>
        </tr>
      </table>
      
      </form>
      </center>
    </div>
    The header template:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <base href ="http://root.com/divdev/">
      <meta http-equiv="Content-Type" content="text/html; charset=Western (ISO-8895-1)" />
      <link rel="stylesheet" type="text/css" href="css/frontend.css">
      <script type="text/JavaScript" src="java/validate.js"></script>
      <title><?php echo $title['title']; ?></title>
      </head>
      <body>
    the script itself is not important, but here it goes:

    Code:
    // JavaScript Register Form Validation
    function validateregister() {
    {
      var fname=document.forms["registerform"]["fname"].value;
      var lname=document.forms["registerform"]["lname"].value;
      var email=document.forms["registerform"]["email"].value;
      var user=document.forms["registerform"]["user"].value;
      var pass1=document.forms["registerform"]["pass1"].value;
      var pass2=document.forms["registerform"]["pass2"].value;
    
      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
      var msg = ""
    
      if (fname==null || fname==""){msg = msg + "First name must be filled out\n";}
      if (lname==null || lname==""){msg = msg + "Last name must be filled out\n";}
      if (email==null || email==""){msg = msg + "E-mail must be filled out\n";}
      if (user==null || user==""){msg = msg + "User name must be filled out\n";}
      if (pass1==null || pass1==""){msg = msg + "Password must be filled out\n";}
      if (pass2==null || pass2==""){msg = msg + "Confirm Password must be filled out\n";}
    
      if (pass1 != pass2){msg = msg + "Passwords do not match";}
    
      if (!filter.test(email)){msg = msg + "Please provide a valid email address";}
    
      if (msg){
        Alert(msg);
        return false;
      }    
    }
    Can some of you guys help me with this?

    Thank You

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    reisve, you should not use the word java when you mean javascript. It confuses people.
    The script is located in a subdirectory in the root of the site called java.
    Using folder called js or javascript would be better. I said 'would ' don't mean that it's mandatory. You could call it 'fishFingers' if you wanted to but it would confuse people. You should not use 'echo a php variable' when none exist.
    And something important
    Code:
      if (msg){
        Alert(msg);
        return false;
      }
    js is case sensitive - the word is 'alert' all small letters

    The form:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=Western (ISO-8895-1)">
    <link rel="stylesheet" type="text/css" href="css/frontend.css">
    <script type="text/JavaScript" src="java/validate.js"></script>
    <title>Title of Page</title>
    <style type="text/css">
    td{
    	text-align: right
    }
    
    </style>
    </head>
    
    <body>
    <div style="text-align:center;">
    <h2>This is the Header for the Register</h2>
    
    <h4>Please fill out form and press the submit button</h4>
    
    <form id="registerform" class="form" action="register.php" method="post" onsubmit="return validateregister()">
    	<table style="margin: 0 auto;" border="0">
    		<tr>
    			<td><label for="fname">First Name</label></td><td><input class="registerforminput" name="fname" id="fname" type="text" tabindex = 1></td>
    			<td><label for="user">User Name</label></td><td><input class="registerforminput" name="user" id="user" type="text" tabindex = 4></td>
    		</tr>
    		<tr>
    			<td><label for="lname">Last Name</label></td><td><input class="registerforminput" name="lname" id="lname" type="text" tabindex = 2></td>
    			<td><label for="pass1">Password</label></td><td><input class="registerforminput" name="pass1" id="pass1" type="password" tabindex = 5></td>
    		</tr>
    		<tr>
    			<td><label for="email">Email Address</label></td><td><input class="registerforminput" name="email" id="email" type="text" tabindex = 3></td>
    			<td><label for="pass2">Type password again</label></td><td><input class="registerforminput" name="pass2" id="pass2" type="password" tabindex = 6></td>
    		</tr>
    		<tr><td colspan="4" style="text-align:center;"><input type="submit" value="Submit"></td></tr>
    	</table>
    </form>
    
    </div>
    
    <script type="text/javascript">
    function validateregister() {
      var fname=document.forms["registerform"]["fname"].value;
      var lname=document.forms["registerform"]["lname"].value;
      var email=document.forms["registerform"]["email"].value;
      var user=document.forms["registerform"]["user"].value;
      var pass1=document.forms["registerform"]["pass1"].value;
      var pass2=document.forms["registerform"]["pass2"].value;
    
      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
      var msg = ""
    
      if (fname==null || fname==""){msg = msg + "First name must be filled out\n";}
      if (lname==null || lname==""){msg = msg + "Last name must be filled out\n";}
      if (email==null || email==""){msg = msg + "E-mail must be filled out\n";}
      if (user==null || user==""){msg = msg + "User name must be filled out\n";}
      if (pass1==null || pass1==""){msg = msg + "Password must be filled out\n";}
      if (pass2==null || pass2==""){msg = msg + "Confirm Password must be filled out\n";}
    
      if (pass1 != pass2){msg = msg + "Passwords do not match";}
    
      if (!filter.test(email)){msg = msg + "Please provide a valid email address";}
    
      if (msg){
        alert(msg);
        return false;
      }
    }
    
    </script>
    </body>
    </html>
    In your main folder you should have this script probably called index.html and some folders; "java" is one of those folder with the js file in it. If not change it.
    Last edited by sunfighter; 05-05-2013 at 10:02 PM.

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hank You

    I will change the name of the folder. No problema and did change the Alert() to alert(). But still not working.

    As I wrote, the script is in a file in a folder called java (will chang it to js), and the script is collet by the header.tpl (template). Look to my directory layout.

    Anyway still not working.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    Your main code should be on the same level as your sub-folders.
    In other words index.php(this is your main file) exists with folder js and folder templates and, if you had pictures, folder images.
    You call the templates (and I would use the .html on them not .tpl) by naming your main file as index.PHP and then using :
    PHP Code:
    <?php
    include("templates/header.html");
    ?>
    Where you want the header to appear. Same for a footer, if you have one, and same for the js.
    Where you want the js:
    Code:
    <script type="text/javascript" src="js/filename.js"></script>
    Does this help?

  • #5
    New Coder
    Join Date
    Jan 2013
    Posts
    71
    Thanks
    3
    Thanked 2 Times in 2 Posts
    The mistake is in the header section .Correct it first.

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    external javascript file

    What is the mistake in the header?

  • #7
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @sunfighter

    It is like that.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    Show me your new index code please.

  • #9
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
      
    include "lib/configuration.php";
      include 
    "language/frontend/portuguese.php";

      include 
    "templates/frontend/header.tpl";
      include 
    "templates/frontend/loginspace.tpl";
      include 
    "templates/frontend/registerlogin.tpl";
      include 
    "templates/frontend/searchspace.tpl";
      include 
    "templates/frontend/search.tpl";
      include 
    "templates/frontend/hmenuspace.tpl";
      include 
    "templates/frontend/menubar.tpl";
      include 
    "templates/frontend/leftspace.tpl";
      include 
    "templates/frontend/leftside.tpl";
      include 
    "templates/frontend/contentspace.tpl";
      include 
    "templates/frontend/centerwelcome.tpl";
      include 
    "templates/frontend/centerlisting.tpl";
      include 
    "templates/frontend/rightspace.tpl";
      include 
    "templates/frontend/rightside.tpl";
      include 
    "templates/frontend/footerspace.tpl";
      include 
    "templates/frontend/footerdiv.tpl";
      include 
    "templates/frontend/footer.tpl";
    ?>
    Just don-t understand what this have to do with it. This is pure PHP

  • #10
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just FYI, the directory structure:

    Code:
    D:\WEB DATA\DIVDEV           <- Web data is the document root of my server, 
       index.htm                   divdev is the directory where the pages are installed
       index.php                <- My index.php
       login.php
       logout.php
       register.php
    
    +---admin
           index.htm
           index.php
    
    +---css
           backend.css
           frontend.css
           index.htm
    
    +---js
           validate.js
           validate_custom.js
    
    +---language
          index.htm
       
       +---backend
              index.htm
              portuguese.php
       
       +---frontend
               index.htm
               portuguese.php
    
    +---lib
           configuration.php
    
    +---templates
           index.htm
        
        +---backend
               centercontent_1.tpl
               footer.tpl
               footerspace.tpl
               header.tpl
               index.htm
               infospace.tpl
               leftspace.tpl
               midspace.tpl
               searchspace.tpl
        
        +---frontend
                centerlisting.tpl
                centerwelcome.tpl
                contentspace.tpl
                error.php
                footer.tpl
                footerdiv.tpl
                footerspace.tpl
                header.tpl       <- My header
                hmenuspace.tpl
                index.htm
                info.php
                leftside.tpl
                leftspace.tpl
                login.tpl
                loginspace.tpl
                menubar.tpl
                midspace.tpl
                msgspace.tpl
                register.tpl
                registerlogin.tpl
                rightside.tpl
                rightspace.tpl
                search.tpl
                searchspace.tpl
    And I said that before, This was working before. Don't know what I did but suddenly stopped working

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    Quote Originally Posted by reisve View Post
    @sunfighter

    It is like that.
    Not from what you just showed me.
    The path to your files should be
    Code:
    include ("../templates/frontend/header.tpl");
    Notice the double dots and slash.
    Maybe you need three dots??? if two don't work try three.
    Last edited by sunfighter; 05-06-2013 at 06:11 PM.

  • #12
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is wrong. It would go to the root of the server (Web Data). Anyway it is working. I don't have a problem there. My problem is with the javascript. All the rest is working.

    To do it like you say it, it would have to be like this:
    include ("../divdev/templates/frontend/header.tpl");

  • #13
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    Your showing that folder "templates" is in D:

    But I think you know what's going on why not have a js script that is just
    alert("You found me");
    and then try to include it as your first include. When you get the alert you got the path.

  • #14
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is just the way the tree command shows it. the template is under "Web Data\divdev"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •