Newbie Question - PHP Form link to URL's

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

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Newbie Question - PHP Form link to URL's

Post by John Cartwright »

Are you doing anything with the $page variable? i.e.

Code: Select all

header('Location: '. $page);
exit();
tazdevil
Forum Newbie
Posts: 12
Joined: Sun Apr 06, 2008 8:30 am

Re: Newbie Question - PHP Form link to URL's

Post by tazdevil »

I have run the above code... using the below form and get the following results:

URL in browser: http://www.domain.com/post?breed=dog&ag ... mit=Submit

On page:
Not Found
The requested URL /post was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

form used:

Code: Select all

<form method="find_plan.php" action="post">
Pet Type:
<label><input name="breed" value="cat" type="radio" />Cat</label>
<label><input name="breed" value="dog" type="radio" />Dog</label>
<br />
Age Range:
<label><input name="age" value="under8" type="radio" />Under 8</label>
<label><input name="age" value="over8" type="radio" />Over 8</label>
<br />
<input type=submit name=submit value="Submit">
</form>
tazdevil
Forum Newbie
Posts: 12
Joined: Sun Apr 06, 2008 8:30 am

Re: Newbie Question - PHP Form link to URL's

Post by tazdevil »

Jcart wrote:Are you doing anything with the $page variable? i.e.

Code: Select all

header('Location: '. $page);
exit();
I'm really sorry - I am so new to php that I don't understand what you mean.
tazdevil
Forum Newbie
Posts: 12
Joined: Sun Apr 06, 2008 8:30 am

Re: Newbie Question - PHP Form link to URL's

Post by tazdevil »

just to confirm...

my form page: find_plan.html

Code: Select all

<form method="find_plan.php" action="post">
Pet Type:
<label><input name="breed" value="cat" type="radio" />Cat</label>
<label><input name="breed" value="dog" type="radio" />Dog</label>
<br />
Age Range:
<label><input name="age" value="under8" type="radio" />Under 8</label>
<label><input name="age" value="over8" type="radio" />Over 8</label>
<br />
<input type=submit name=submit value="Submit">
</form>
the php page: find_plan.php

Code: Select all

<?php 
 if (isset($_POST['breed']) && isset($_POST['age'])) { //make sure values exist before using them
    if ($_POST['breed'] == 'dog') {
       if ($_POST['age'] == 'under8') {
          $page = 'http://www.google.co.uk';  
       } else {
          $page = 'http://www.yahoo.co.uk';
       }
    } else {
    if ($_POST['breed'] == 'cat') {
       if ($_POST['age'] == 'under8') {
          $page = 'http://www.msn.co.uk';  
       } else {
          $page = 'http://www.bbc.co.uk';
       }
    }
 
    echo $page;
    header('Location: '. $page);
    exit();
 }
 ?>
tazdevil
Forum Newbie
Posts: 12
Joined: Sun Apr 06, 2008 8:30 am

Re: Newbie Question - PHP Form link to URL's

Post by tazdevil »

I thought I'd found something...

I change the form from:
<form method="find_plan.php" action="post">
to:
<form method="post" action="find_plan.php">

But now I'm getting the following error:
Parse error: syntax error, unexpected $end in /home/fastnet/public_html/fastnetria/find_plan.php on line 22
tazdevil
Forum Newbie
Posts: 12
Joined: Sun Apr 06, 2008 8:30 am

Re: Newbie Question - PHP Form link to URL's

Post by tazdevil »

Just found another thing...

Because of the above error, I added another } curly bracket to the end of the code.

I now get the following:

http://www.bbc.co.uk
Warning: Cannot modify header information - headers already sent by (output started at /home/fastnet/public_html/fastnetria/find_plan.php:1) in /home/fastnet/public_html/fastnetria/find_plan.php on line 19
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Newbie Question - PHP Form link to URL's

Post by John Cartwright »

tazdevil wrote:Just found another thing...

Because of the above error, I added another } curly bracket to the end of the code.

I now get the following:

http://www.bbc.co.uk
Warning: Cannot modify header information - headers already sent by (output started at /home/fastnet/public_html/fastnetria/find_plan.php:1) in /home/fastnet/public_html/fastnetria/find_plan.php on line 19

Code: Select all

echo $page;
header('Location: '. $page);
exit();
Take out the

Code: Select all

echo $page;
line, as that is considered output. If you take a look at the message, it will fail if any output has been sent prior to sending the header.
tazdevil
Forum Newbie
Posts: 12
Joined: Sun Apr 06, 2008 8:30 am

Re: Newbie Question - PHP Form link to URL's

Post by tazdevil »

mmm... still not working with the following:

find_plan.html

Code: Select all

<!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>
<form method="post" action="find_plan.php">
<!--<form method="find_plan.php" action="post">-->
Pet Type:
<label><input name="breed" value="cat" type="radio" />Cat</label>
<label><input name="breed" value="dog" type="radio" />Dog</label>
<br />
Age Range:
<label><input name="age" value="under8" type="radio" />Under 8</label>
<label><input name="age" value="over8" type="radio" />Over 8</label>
<br />
<input type=submit name=submit value="Submit">
</form>
</body>
</html>
find_plan.php

Code: Select all

<?php
 if (isset($_POST['breed']) && isset($_POST['age'])) { //make sure values exist before using them
    if ($_POST['breed'] == 'dog') {
       if ($_POST['age'] == 'under8') {
          $page = 'http://www.google.co.uk';  
       } else {
          $page = 'http://www.yahoo.co.uk';
       }
    } else {
    if ($_POST['breed'] == 'cat') {
       if ($_POST['age'] == 'under8') {
          $page = 'http://www.msn.co.uk';  
       } else {
          $page = 'http://www.bbc.co.uk';
       }
    }
    header('Location: '. $page);
    exit();
 }
 ?>
I've taken away the

Code: Select all

echo $page;
and get the following error, rather than transferring me to one of the $page url's...

Parse error: syntax error, unexpected $end in /home/fastnet/public_html/fastnetria/find_plan.php on line 20

Any guidance, help or correction of the code would be really appreciated.
Does the above work on your system?

many thanks
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: Newbie Question - PHP Form link to URL's

Post by lafever »

You're missing a closing } at the end

Code: Select all

 
 <?php
 if (isset($_POST['breed']) && isset($_POST['age'])) { //make sure values exist before using them
    if ($_POST['breed'] == 'dog') {
       if ($_POST['age'] == 'under8') {
          $page = 'http://www.google.co.uk';
       } else {
          $page = 'http://www.yahoo.co.uk';
       }
    } else {
    if ($_POST['breed'] == 'cat') {
       if ($_POST['age'] == 'under8') {
          $page = 'http://www.msn.co.uk';
       } else {
          $page = 'http://www.bbc.co.uk';
       }
    }
    header('Location: '. $page);
    exit();
 }
 }
 ?>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Newbie Question - PHP Form link to URL's

Post by John Cartwright »

tazdevil wrote: I've taken away the

Code: Select all

echo $page;
and get the following error, rather than transferring me to one of the $page url's...

Parse error: syntax error, unexpected $end in /home/fastnet/public_html/fastnetria/find_plan.php on line 20

Any guidance, help or correction of the code would be really appreciated.
Does the above work on your system?

many thanks
I admit I held your hand a little because I realized you were new to PHP.. but you need to learn to fix these kinds of errors on your own. To help you, take a look at our editors thread for an editor that can match the brackets and highlight parse errors in real time.

It doesn't mean don't post a question if you have a parse error, but I noticed everyone you got one you posted saying you had a parse error when the solution if just a few ounces to energy away ;)

Apologies if that came out wrong. 8)
Post Reply