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.
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.
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:
// 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.
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>
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.