How to redirect a user based on their input.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricC
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 9:55 am

How to redirect a user based on their input.

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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> 
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
EricC
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 9:55 am

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

Post by EricC »

Thanks this was the response:



Method Not Allowed
The requested method POST is not allowed for the URL /cart.html.
EricC
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 9:55 am

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

Post by EricC »

Any thoughts on why the code provided didn't work?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Is cart.html the file with this code or is it some other something that you downloaded or wrote?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
EricC
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 9:55 am

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
EricC
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 9:55 am

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

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
Last edited by AbraCadaver on Fri Dec 04, 2009 9:28 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
EricC
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 9:55 am

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply