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 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    HTML Enter Sub-directory Text Box?

    Hello!

    I am trying to code a text box so that the user can enter a subdirectory (www.example.com/subdirectory), but only the subdirectory, then press a "Go" button, and they will be taken to the subdirectory.

    So, for example, if I had a subdirectory named "apple" (www.example.com/apple/), the user could enter apple into a text box, then click go, and it would take them to www.example.com/apple/.

    What would my code be? Just give me a quick example, i can edit it from there.

    Thanks.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,630
    Thanks
    6
    Thanked 1,002 Times in 975 Posts
    That’s nothing HTML alone can do, you need a server side processing script and you can enhance this with JavaScript. HTML is only responsible for the existence of the form and input field. The rest is up to the script. I’m moving this over to the PHP forum.

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    .....
    Last edited by drakerehfeld; 11-29-2011 at 04:45 AM.

  • #4
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I actually have a problem in my code. Can you help? I used a standard code from someone else.
    • I dont like that they can't press enter, but rather have to click OK. How do I code it so that pressing enter clicks ok?
    • Is there a way to make the link open in a new window? When they click OK, it opens a new window with thier account?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>Login</title>
    <script type="text/javascript">
    
    function init() {
    
    /*******account numbers******/
    
       numbers=[          
                'gfillinger','rcoleman','taday',
                'drehfeld62','drehfeld60','drehfeld97',
               ]; 
    
    /*************************************************************/
    
       df=document.forms[0];
       df[0].focus(); 
    
    df[0].onkeyup=function(){
       this.value=this.value.toLowerCase();
     }
    
       df[1].onclick=function() {
    
    
    for(c=0;c<numbers.length;c++) {
    if(numbers[c]==df[0].value) {   
       location.href=df[0].value+'.html';
       return;
      }
     }
    
    if(numbers[c]!=df[0].value) {
    alert('This username is invalid.\n\nPlease correct your username (format: firstinitiallastname; example: jsmith)');
       df.reset();
       df[0].focus();
       return;
       }
      }
     }
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div id="container">
    <h1></h1>
    <input type="text">
    <input type="button" value="OK">
    </div>
    </form>
    
    </body>
    </html>
    Thanks
    PS sorry for being such a n00b. :P
    Last edited by drakerehfeld; 11-29-2011 at 03:57 AM.

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    They've used javascript, which I personally wouldn't have used. I know a little javascript and would consider changing the button type input to a submit type, and adding the onSubmit event with the function call.

    But, I'm not in a position to test that. So, what I would do is get rid of the relevant javascript and instead, have a script called subdir.php and change the form to this:
    Code:
    <form action="subdir.php" method="post">
    <div id="container">
    <h1></h1>
    <input type="text" name="subdir">
    <input type="submit" value="OK">
    </div>
    </form>
    Then, in subdir.php, I would have this:
    PHP Code:
    // Put user requested subdir into variable. Probably needs cleaning, in case of 'dodgey' url's containing things like .. or /'s
    $subdir $_POST['subdir'];
    header('location: http://www.example.com/'.$subdir.'/'); 
    That will redirect the user to the subdir they entered - whether they pressed enter or clicked the button. Using PHP allows for expansion and a more secure environment to navigate folders in my opinion.

  • Users who have thanked BluePanther for this post:

    drakerehfeld (11-30-2011)

  • #6
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think I did something wrong. Help?

    go to bizprotocol.com/usernames.html to see it for yourself (use the username taday).

    When I enter a username, it just takes me to a blank page, not redirect to the subdirectory.

    I really am a NOOB! I don't really know much about coding, so thanks for your help.


    subdir.php code:
    PHP Code:
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            <?php
            $subdir 
    $_POST['subdir']; 
    header('location: http://www.example.com/'.$subdir.'/'); ?>
        </body>
    </html>
    did I even do that right? I have no idea... i just guessed it.

    usernames.html code:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <script type="text/javascript">
    
    function init() {
    
    /******* usernames ******/
    
       numbers=[          
                'gfillinger','rcoleman','taday',
                'drehfeld62','drehfeld60','drehfeld97',
               ]; 
    
    /*************************************************************/
    
       df=document.forms[0];
       df[0].focus(); 
    
    df[0].onkeyup=function(){
       this.value=this.value.toLowerCase();
     }
    
       df[1].onclick=function() {
    
    
    for(c=0;c<numbers.length;c++) {
    if(numbers[c]==df[0].value) {   
       location.href=df[0].value+'.html';
       return;
      }
     }
    
    if(numbers[c]!=df[0].value) {
    alert('This username is invalid.\n\nPlease correct your username (format: firstinitiallastname; example: jsmith)');
       df.reset();
       df[0].focus();
       return;
       }
      }
     }
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    
    </script>
    
    </head>
    <body>
    
    <form action="subdir.php" method="post">
    <div id="container">
    <h1></h1>
    <input type="text" name="subdir">
    <input type="submit" value="OK">
    </div>
    </form>
    
    </body>
    </html>
    Any suggestions?
    Last edited by drakerehfeld; 11-29-2011 at 04:43 AM.

  • #7
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    did I really leave it as www.example.com? oops. NOOB



    I still get the blank page when running the script, with the correct URL.
    Last edited by drakerehfeld; 11-29-2011 at 04:56 AM.

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Yeah, and you also have error reporting off .

    You can't supply header information after the header has been sent. In other words, remove all html and have just the php and it will work.

  • #9
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help
    Last edited by drakerehfeld; 12-01-2011 at 12:32 AM.

  • #10
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So, after some testing, I have just one last (minor) problem.

    I have changed username to account number in all of the code.

    If they enter the wrong account number (lets say they enter "jsmith", jsmith is not one of the account numbers defined), the error popup opens just fine (explaining that they need to correct their account number), but, they are still progressed onto the page www.example.com/jsmith.html.

    How do I stop them if they put in the wrong account number?

    Here is my updated code:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>Login to ReInvestmentCloud.com</title>
    <script type="text/javascript">
    
    function init() {
    
    /******* account numbers ******/
    
       numbers=[          
                'b1e9391411','cfd7bb95337','06b71981d0',
                '79a1bedc95','2dca8594d7','268415397','a8f91c64a8',
               ]; 
    
    /*************************************************************/
    
       df=document.forms[0];
       df[0].focus(); 
    
    df[0].onkeyup=function(){
       this.value=this.value.toLowerCase();
     }
    
       df[1].onclick=function() {
    
    
    for(c=0;c<numbers.length;c++) {
    if(numbers[c]==df[0].value) {   
       location.href=df[0].value+'.html';
       return;
      }
     }
    
    if(numbers[c]!=df[0].value) {
    alert('Please enter your account number again, there was an error.');
       df.reset();
       df[0].focus();
       return;
       }
      }
     }
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    
    </script>
    
    </head>
    <body>
    <p><font face="verdana">Enter your account number:</font></p>
    <form action="subdir.php" method="post">
    <div id="container">
    <h1></h1>
    <input type="text" name="subdir">
    <input type="submit" value="OK">
    </div>
    </form>
    
    </body>
    </html>
    and PHP:
    PHP Code:
    <?php
    $subdir 
    $_POST['subdir']; 
    header('location: http://reinvestmentcloud.darkearsi.com/Accounts/'.$subdir.'.html');
    ?>

  • #11
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    In the redirect page, you'll have to add the user check. If you're checking users with javascript, you shouldn't. Javascript is user side executed, meaning they can track and see everything that's executed. If you use the PHP to check the users, they can't see it as it's server side - more secure.

  • #12
    New Coder
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Makes sense. I just realized that all they need to do is view the source of the page...

    How can i add the server side user check?


    this is my first (real) time using php.
    Last edited by drakerehfeld; 12-01-2011 at 01:14 AM.

  • #13
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Depends how you store the users. Could you clarify how you store the users currently?


  •  

    Tags for this Thread

    Posting Permissions

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