Link to home
Start Free TrialLog in
Avatar of joedunn
joedunn

asked on

Redirect to correct page based on form entry.

I would like to create a simple form that when a user enters a code they are redirected to a given page for that code.

I tried doing it with the following JavaScript and HTML but there must be an error as it does not work - If someone can either help me fix this or come up with a better solution.

It should be a simple solution - user enters a value and then are redirected to a page that I can set.

Also, if someone enters an invlaid code I would like to display a simple message.

Thanks
{literal}
<script language="javascript" type="text/javascript">
function doRedirect() {
var ec = document.myForm["ec"].value;
if ( ec == "1232" ) {
window.location.href = "example.php";
} else if ( ec == "8832" ) {
window.location.href = "8832.tpl";
}
}
</script>
{/literal}
 
<div id="content-info">
  <div align="center">
  <table cellspacing="0">
    <tr>
      
	  <td>
        <form name="myForm">
        <br /><br />
       <h3>Please enter the Email Code that you received to retrieve your message</h3>
         <br />
		<p><blockquote>Email Code:
           <input type="text" name="ec" maxlength="4" size="6" /></blockquote>
         </p>
         <br />
        <form action="#">
          
            <div align="left">
              <input type="button" value="Submit" onclick="doRedirect()">
            </div>
        </form>
 
  </form>
  </td>
  </tr>
  </table></div>
</div>

Open in new window

Avatar of pradeep_agnihotri
pradeep_agnihotri

joedunn,
the script you had written is working fine.
It looks like that you are using the smarty template engine, if you are not using smarty and executing this file directly then remove the words {literal} and {/literal} from your code.
ASKER CERTIFIED SOLUTION
Avatar of darron_chapman
darron_chapman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,

I'm trying to use the above solution, with a small modification, but it's not working.

I have one of two pages I'd like to redirect to, based on an input field (like above). However, rather than just one variable that can be allowed, there's about 200 for each.  So, for example, I tried:

<?php
  $ec=$_POST['ec'];
  if (isset($ec))
  {
        if ($ec=="1234" or "5678")
          header("Location: index.htm");
        elseif ($ec="8832" or "9898" or "4545")
      header("Location: warranty");      
  }
  ?>

As soon as I added the or "5678" - now, no matter what is put in the field - any value - , it goes to the index.htm page. How can I list a series of choice for each redirect?

Also, I didn't see an answer to the earlier question of if someone types in a value that's not on either "list" - how can I display a message saying "not valid entry"??

Thanks! Cory
You have to specify the variable you are comparing it to every time.  So in this case

This:
if ($ec=="1234" or "5678")

becomes this:
if ($ec=="1234" OR $ec=="5678")
Thanks Darron,

That works great, I was also trying to make it work in an array. So far, I have this working, but now I can't figure out how to have a statement for what happens when the value is NOT in either array:

<?php
  $sitenumbersvegas = array ("1234", "5678", "9989");
  $sitenumbersatlanta = array ("2345", "3456", "4567");
  $ec=$_POST['ec'];
        if(in_array($ec, $sitenumbersvegas)) {
          header("Location: index.htm");
  }     elseif(in_array($ec, $sitenumbersatlanta)) {
          header("Location: warranty");    
 //Here's where I don't know what code to use to tell it where to go if ec isn't a value in either array:
 }
        elseif header("Location: contact.htm");
  ?>
Here is the code - the else statement will execute if all other if's and elseif's are false... you really need to post a separate question on this site.  Experts like myself rely on points and when we answer questions that are posted in questions that have already been answered, then we are denied such points.  I don't mind answering questions, but I would recommend that if you have any other questions to go ahead and open a new question.  Thanks for your understanding..

        if(in_array($ec, $sitenumbersvegas)) {
          header("Location: index.htm");
  }     elseif(in_array($ec, $sitenumbersatlanta)) {
          header("Location: warranty");
  }     else {
          header("Location: contact.htm");  
  }
Darron - thanks a ton - and sorry about not opening a new one! Will do so in the future- Cory
I have a similar need for a code like this. I need to validate the form to ensure all fields are filled out correctly, redirect to one of 2 URLs based on a user's input in one of the text boxes, and at the same time email the results of the entire form to an address.

I get this:
Warning: Cannot modify header information - headers already sent by (output started at /home/*******) in /home/********* on line 8. (or line 6, depending on which option i'm choosing to enter in the form)

I can't even get far enough to add in any sort of form validation or emailing results, because I can't get past this point.