Page 2 of 2

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

Posted: Sun Apr 06, 2008 10:49 am
by John Cartwright
Are you doing anything with the $page variable? i.e.

Code: Select all

header('Location: '. $page);
exit();

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

Posted: Sun Apr 06, 2008 10:50 am
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>

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

Posted: Sun Apr 06, 2008 10:53 am
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.

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

Posted: Sun Apr 06, 2008 10:55 am
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();
 }
 ?>

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

Posted: Sun Apr 06, 2008 11:01 am
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

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

Posted: Sun Apr 06, 2008 11:39 am
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

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

Posted: Sun Apr 06, 2008 6:13 pm
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.

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

Posted: Mon Apr 07, 2008 2:31 am
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

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

Posted: Mon Apr 07, 2008 7:38 am
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();
 }
 }
 ?>
 

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

Posted: Mon Apr 07, 2008 11:00 am
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)