It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

redirection to an another webpage

redirection to an another webpage

Postby paul555 on Sat Jun 14, 2008 8:39 am

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: Select all
<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: Select all
<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?
paul555
 
Posts: 6
Joined: Sat Jun 14, 2008 7:47 am

Postby rangana on Sat Jun 14, 2008 9:19 am

Here's a working example which on the light side, take in consideration other names, any name that is inside the dname textbox:
Code: Select all
]
<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.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby paul555 on Sat Jun 14, 2008 10:34 am

Thanks for your answer.I tried your example like that
Code: Select all
<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: Select all
<script type="text/javascript">
function displaypage(dname)
{

   if(dname="paul"){
      location.href=p/paul.html;
      }
</script>

with
Code: Select all
<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?
paul555
 
Posts: 6
Joined: Sat Jun 14, 2008 7:47 am

Postby rangana on Sat Jun 14, 2008 10:57 am

Because you're changing my script erroneously.
Code: Select all
<script type="text/javascript">
function displaypage()
{
  var ext='html';
  location.href=document.getElementsByName('dname')[0].value+'.'+ext;
}
</script>
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby paul555 on Sat Jun 14, 2008 11:20 am

Sorry i didn't noticed that i did that error.I tried that
Code: Select all
<script type="text/javascript">
function displaypage()
{
  var ext='html';
  location.href=document.getElementsByName('dname')[0].value+'.'+ext;
}

and also that
Code: Select all
<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
paul555
 
Posts: 6
Joined: Sat Jun 14, 2008 7:47 am

Postby rangana on Sat Jun 14, 2008 11:31 am

This part is wrong:
Code: Select all
if(dname=p)


You haven't declared dname and comparison should be double equal sign (==) :(

Hopefully, this tweak should work:
Code: Select all
<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>
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby paul555 on Sat Jun 14, 2008 12:25 pm

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)
paul555
 
Posts: 6
Joined: Sat Jun 14, 2008 7:47 am

Postby rangana on Sat Jun 14, 2008 12:45 pm

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).
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby paul555 on Sat Jun 14, 2008 1:03 pm

But i want the change happen through an input text box.So that isn't possible?
paul555
 
Posts: 6
Joined: Sat Jun 14, 2008 7:47 am

Postby paul555 on Sat Jun 14, 2008 1:30 pm

I am sorry.My mistake :oops: I removed theese two lines
Code: Select all
<form action="" method="post" onSubmit="return displaypage()">

Code: Select all
</form>

And all are working ok.Thanks very much for your help.
paul555
 
Posts: 6
Joined: Sat Jun 14, 2008 7:47 am

Next

Who is online

Users browsing this forum: No registered users and 3 guests