Page 1 of 1

How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 10:09 am
by EricC
:banghead: I've tried the examples from several other posts and have had zero success so I figured that I would start from scratch :banghead:

I need a snippet of code that I can throw into a page that asks for a user ID or card# I then need the code to append that # onto a url that is hard coded into the snippet and direct the user to the resulting url.

Any help is GREATLY appreciated as I have run out of ideas and tried the answers from post similar to mine without luck.

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 10:32 am
by AbraCadaver
Just an example, you'll have to work it:

Code: Select all

<?php
if(isset($_POST['id'])) {
    $id = urlencode($_POST['id']);
    header("Location: http://www.example.com/somepage.php?id=$id");
    exit;
}
?>
<form method="post">
   <input type="text" name="id">
   <input type="submit" name="submit">
</form> 

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 10:44 am
by EricC
Thanks this was the response:



Method Not Allowed
The requested method POST is not allowed for the URL /cart.html.

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 12:32 pm
by EricC
Any thoughts on why the code provided didn't work?

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 1:10 pm
by AbraCadaver
Is cart.html the file with this code or is it some other something that you downloaded or wrote?

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 2:03 pm
by EricC
cart.html is just the file that this code is sitting in. It is an empty page save the snippet that you provided.

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 2:08 pm
by AbraCadaver
EricC wrote:cart.html is just the file that this code is sitting in. It is an empty page save the snippet that you provided.
Well, most web servers will not parse/execute PHP code unless the file is a .php extension. Try that.

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 2:18 pm
by EricC
Ok I just created cart.php and pasted the code into that.

Warning: Cannot modify header information - headers already sent by (output started at /home/content/E/c/a/Ecanha/html/cart.php:9) in /home/content/E/c/a/Ecanha/html/cart.php on line 12

was returned

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 3:02 pm
by AbraCadaver
EricC wrote:Ok I just created cart.php and pasted the code into that.

Warning: Cannot modify header information - headers already sent by (output started at /home/content/E/c/a/Ecanha/html/cart.php:9) in /home/content/E/c/a/Ecanha/html/cart.php on line 12

was returned
Then you have a bunch of other stuff in that file that I am unaware of, because my code was only 11 lines long and unable to have an error on line 12.

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 5:41 pm
by EricC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if(isset($_POST['id'])) {
$id = urlencode($_POST['id']);
header("Location: http://www.mydomain.com/Images/Youth/Ba ... rt/?id=$id");
exit;
}
?>
<form method="post">
<input type="text" name="id">
<input type="submit" name="submit">
</form>
</body>
</html>




This is the entire file as uploaded to the server with the exception that 'mydomain' is the actual domain name.

Re: How to redirect a user based on their input.

Posted: Fri Dec 04, 2009 9:35 pm
by AbraCadaver
You can't have any output (html tags, echo anything, etc...) before the header(). So move all of the HTML to the bottom. If the form is posted then the PHP will execute, if not then the HTML will be displayed.

-Shawn