Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2011, 02:17 AM   PM User | #1
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
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.
drakerehfeld is offline   Reply With Quote
Old 11-28-2011, 10:17 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,601
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 11-28-2011, 03:01 PM   PM User | #3
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
.....

Last edited by drakerehfeld; 11-29-2011 at 04:45 AM..
drakerehfeld is offline   Reply With Quote
Old 11-29-2011, 03:53 AM   PM User | #4
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
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..
drakerehfeld is offline   Reply With Quote
Old 11-29-2011, 04:12 AM   PM User | #5
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Users who have thanked BluePanther for this post:
drakerehfeld (11-30-2011)
Old 11-29-2011, 04:41 AM   PM User | #6
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
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..
drakerehfeld is offline   Reply With Quote
Old 11-29-2011, 04:47 AM   PM User | #7
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
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..
drakerehfeld is offline   Reply With Quote
Old 11-29-2011, 05:06 AM   PM User | #8
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 11-30-2011, 03:04 AM   PM User | #9
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
Thanks for the help

Last edited by drakerehfeld; 12-01-2011 at 12:32 AM..
drakerehfeld is offline   Reply With Quote
Old 12-01-2011, 12:31 AM   PM User | #10
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
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');
?>
drakerehfeld is offline   Reply With Quote
Old 12-01-2011, 12:55 AM   PM User | #11
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 12-01-2011, 01:03 AM   PM User | #12
drakerehfeld
New Coder

 
Join Date: Jun 2011
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
drakerehfeld is an unknown quantity at this point
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..
drakerehfeld is offline   Reply With Quote
Old 12-01-2011, 01:46 AM   PM User | #13
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Depends how you store the users. Could you clarify how you store the users currently?
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Tags
box, html, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:01 AM.


Advertisement
Log in to turn off these ads.