| View previous topic :: View next topic |
| Author |
Message |
paul555
Joined: 14 Jun 2008 Posts: 6
|
Posted: Sat Jun 14, 2008 9:39 am Post subject: redirection to an another webpage |
|
|
Hi all .I start learning html and javascript a little way back and now i wonder how to implement the situation described here.I have an index.html with a form like this
| Code: |
<form action="" method="post">
<p>
<label>name</label>
<input name="dname" value="Your name" type="text" size="30" />
<br /><br />
<input type="submit" value="Submit" onclick="displaypage()/>
</p>
</form> |
Now i want if the name entered in the input is "paul" and the user click the submit button the browser to go and display in the same window the paul.html page else if the name entered in the input is "jim" and the user click the submit button the browser to go and display in the same window the jim.html page.I suppose i have to make something like this in the head section of index.html page
| Code: |
<script type="text/javascript">
function displaypage()
{
switch(n)
{
case 1:
paul.html
break;
case 2:
jim.html
break;
default:
alert("Wrong Name");
}
}
</script> |
Any suggestions how the above may work? |
|
| Back to top |
|
 |
|
|
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 718 Location: Cebu City Philippines
|
Posted: Sat Jun 14, 2008 10:19 am Post subject: Re: redirection to an another webpage |
|
|
Here's a working example which on the light side, take in consideration other names, any name that is inside the dname textbox:
| Code: |
]
<script type="text/javascript">
function displaypage()
{
var ext='htm'; // Extension of your page. You cou
location.href=document.getElementsByName('dname')[0].value+'.'+ext;
}
</script>
<form action="" method="post">
<p><label>name</label>
<input name="dname" value="Your name" type="text" size="30" />
<br /><br />
<input type="button" value="Submit" onclick="displaypage()"/>
</p>
</form>
|
If you only want to accept exclusive values (Jim and Paul) do some restrictions. Just get back if you're still stumped. |
|
| Back to top |
|
 |
paul555
Joined: 14 Jun 2008 Posts: 6
|
Posted: Sat Jun 14, 2008 11:34 am Post subject: Re: redirection to an another webpage |
|
|
Thanks for your answer.I tried your example like that
| Code: |
<script type="text/javascript">
function displaypage()
{
var ext='html';
location.href=document.getElementsByName('dname').value+'.'+ext;
}
</scrit> |
but it didn't work.Also i think and i tried that
| Code: |
<script type="text/javascript">
function displaypage(dname)
{
if(dname="paul"){
location.href=p/paul.html;
}
</script>
|
with
| Code: |
<form action="" method="post">
<p><label>name</label>
<input name="dname" value="Your name" type="text" size="30" />
<br /><br />
<input type="button" value="Submit" onclick="displaypage()"/>
</p>
</form> |
and also didn't work.Now i am confused.What can i do? |
|
| Back to top |
|
 |
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 718 Location: Cebu City Philippines
|
Posted: Sat Jun 14, 2008 11:57 am Post subject: Re: redirection to an another webpage |
|
|
Because you're changing my script erroneously.
| Code: |
<script type="text/javascript">
function displaypage()
{
var ext='html';
location.href=document.getElementsByName('dname')[0].value+'.'+ext;
}
</script>
|
|
|
| Back to top |
|
 |
paul555
Joined: 14 Jun 2008 Posts: 6
|
Posted: Sat Jun 14, 2008 12:20 pm Post subject: Re: redirection to an another webpage |
|
|
Sorry i didn't noticed that i did that error.I tried that
| Code: |
<script type="text/javascript">
function displaypage()
{
var ext='html';
location.href=document.getElementsByName('dname')[0].value+'.'+ext;
}
|
and also that
| Code: |
<script type="text/javascript">
function displaypage()
{
var p = "paul";
if(dname=p){
var ext='html';
location.href=document.getElementsByName('dname')[0].value+'.'+ext;
}
else
alert("Wrong name");
}
</script>
|
but still no luck |
|
| Back to top |
|
 |
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 718 Location: Cebu City Philippines
|
Posted: Sat Jun 14, 2008 12:31 pm Post subject: Re: redirection to an another webpage |
|
|
This part is wrong:
You haven't declared dname and comparison should be double equal sign (==)
Hopefully, this tweak should work:
| Code: |
<script type="text/javascript">
function displaypage()
{
var p = "paul",
dname=document.getElementsByName('dname')[0].value;
if(dname==p){
var ext='html';
location.href=dname+'.'+ext;
}
else
alert("Wrong name");
}
</script>
|
|
|
| Back to top |
|
 |
paul555
Joined: 14 Jun 2008 Posts: 6
|
Posted: Sat Jun 14, 2008 1:25 pm Post subject: Re: redirection to an another webpage |
|
|
| Thanks for your reply.Now if i type a different name than paul it shows the alert box but if i type paul it just refresh index.html.If i understand correctly the paul.html file should be in the same folder as index.html?(i have them in the same folder) |
|
| Back to top |
|
 |
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 718 Location: Cebu City Philippines
|
Posted: Sat Jun 14, 2008 1:45 pm Post subject: Re: redirection to an another webpage |
|
|
It works!
You're seeing the forms behavior when pressing the "Enter" button. If you hate its reaction when pressing enter, then remove your form, since you haven't used it (yet). |
|
| Back to top |
|
 |
paul555
Joined: 14 Jun 2008 Posts: 6
|
Posted: Sat Jun 14, 2008 2:03 pm Post subject: Re: redirection to an another webpage |
|
|
| But i want the change happen through an input text box.So that isn't possible? |
|
| Back to top |
|
 |
paul555
Joined: 14 Jun 2008 Posts: 6
|
Posted: Sat Jun 14, 2008 2:30 pm Post subject: Re: redirection to an another webpage |
|
|
I am sorry.My mistake I removed theese two lines
| Code: |
| <form action="" method="post" onSubmit="return displaypage()"> |
And all are working ok.Thanks very much for your help. |
|
| Back to top |
|
 |
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 718 Location: Cebu City Philippines
|
Posted: Sun Jun 15, 2008 7:31 am Post subject: Re: redirection to an another webpage |
|
|
No problem. You're completely welcome.  |
|
| Back to top |
|
 |
|