Page 1 of 2
Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 8:45 am
by tazdevil
Hi...
I am new (just starting out) with PHP and learning... I have what I think is a simple question - please can someone help me.
I have a form on a page with the following elements, as below:
Code: Select all
Pet Type:
<label><input type="radio" name="breed" value="cat" />Cat<label>
<label><input type="radio" name="breed" value="dog" />Dog</label>
Age Range:
<label><input type="radio" name="age" value="under8" />Under 8</label>
<label><input type="radio" name="age" value="over8" />Over 8</label>
What I need to do is when the user clicks the above radio buttons in the following order and then submits the form, the following happens:
If value="cat" and value="under8" - send to http:www.domain.com/page_1.html
If value="cat" and value="over8" - send to http:www.domain.com/page_2.html
If value="dog" and value="under8" - send to http:www.domain.com/page_3.html
If value="dog" and value="over8" - send to http:www.domain.com/page_4.html
Help would be really appreciated...
many thanks
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 8:51 am
by blurredvision
The first thing you'll need to do is to have a unique name for each input so you can call each one seperately.
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 8:55 am
by tazdevil
blimey.... what a quick response!!! thanks for that...
Please can you explain what you mean or give an example.
do you mean like this (the name & value)?:
Code: Select all
Pet Type:
<label><input type="radio" name="1" value="1" />Cat<label>
<label><input type="radio" name="2" value="2" />Dog</label>
Age Range:
<label><input type="radio" name="3" value="3" />Under 8</label>
<label><input type="radio" name="4" value="4" />Over 8</label>
thanks
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:09 am
by John Cartwright
tazdevil wrote:blimey.... what a quick response!!! thanks for that...
Please can you explain what you mean or give an example.
do you mean like this (the name & value)?:
Code: Select all
Pet Type:
<label><input type="radio" name="1" value="1" />Cat<label>
<label><input type="radio" name="2" value="2" />Dog</label>
Age Range:
<label><input type="radio" name="3" value="3" />Under 8</label>
<label><input type="radio" name="4" value="4" />Over 8</label>
thanks
No, actually you were right the first time
Theres a couple ways to handle the code. For simplicities sake, using just if statements will suffice since you really are only comparing 2 items, however switch statements or arrays can also be used. Heres a quick example using if() statements and your form values:
Code: Select all
if (isset($_POST['breed']) && isset($_POST['age'])) { //make sure values exist before using them
if ($_POST['breed'] == 'dog') {
if ($_POST['age'] == 'under8') {
$page = 'page1';
} else {
// page2 ...
}
} else {
//same stuff as above but for cat etc..
}
}
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:22 am
by blurredvision
Well, my thinking was that he said he was just starting out with PHP and learning (just like me), so it'd be easier for him to simply name each one differently, assign each to a variable, and work from there. But if you want to throw him right in the fire with arrays and switch statements, be my guest.

Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:24 am
by tazdevil
Hi & thanks
Does the following look OK - with a file name of 'find_page.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 = 'page_url_1';
} else {
$page = 'page_url_2';
}
} else {
if ($_POST['breed'] == 'cat') {
if ($_POST['age'] == 'under8') {
$page = 'page_url_3';
} else {
$page = 'page_url_4';
}
}
?>
Are the brackets in the above code right?
and the form as below:
Code: Select all
<form action="find_page.php" method="post">
Pet Type:
<label><input type="radio" name="breed" value="cat" />Cat</label>
<label><input type="radio" name="breed" value="dog" />Dog</label>
Age Range:
<label><input type="radio" name="age" value="under8" />Under 8</label>
<label><input type="radio" name="age" value="over8" />Over 8</label>
<input type="submit" name="Submit" value="Find Page" />
</form>
appreciated....
thanks
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:28 am
by blurredvision
tazdevil wrote:Hi & thanks
Does the following look OK - with a file name of 'find_page.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 = 'page_url_1';
} else {
$page = 'page_url_2';
}
} else {
if ($_POST['breed'] == 'cat') {
if ($_POST['age'] == 'under8') {
$page = 'page_url_3';
} else {
$page = 'page_url_4';
}
}
?>
Are the brackets in the above code right?
You're missing the closing curly bracket for your ifelse statement on line 11.
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:35 am
by tazdevil
You're missing the closing curly bracket for your ifelse statement on line 11.
Sorry to be a pain... I don't know where to place this bracket on line 11 - please can you show me.
The rest of the code and the form look Ok - yes?
many thanks
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:41 am
by blurredvision
tazdevil wrote:You're missing the closing curly bracket for your ifelse statement on line 11.
Sorry to be a pain... I don't know where to place this bracket on line 11 - please can you show me.
Dude, don't worry about it all! I probably shouldn't even be trying to help, since I'm a beginner, too. I don't want to step on the toes of the "big guys" around here

.
Here's what I come up with:
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 = 'page_url_1';
} else {
$page = 'page_url_2';
}
} else {
if ($_POST['breed'] == 'cat') {
if ($_POST['age'] == 'under8') {
$page = 'page_url_3';
} else {
$page = 'page_url_4';
}
}
}
?>
Also, to explain my first reply above, here's how I would do this as a beginner, instead of using the multiple if/else conditionals. However, use the one you are already working on, just incase somebody comes in here and tells me I don't know what I'm talking about :
Code: Select all
<?php
if (isset($_POST['submit'])); {
if (isset($_POST['cat'] && $_POST['under8'])) {
$page = 'page_url_1';
}
if (isset($_POST['cat'] && $_POST['over8'])) {
$page = 'page_url_2';
}
if (isset($_POST['dog'] && $_POST['under8'])) {
$page = 'page_url_3';
}
if (isset($_POST['dog'] && $_POST['over8'])) {
$page = 'page_url_4';
}
}
?>
<form method="this file.php" action="post">
Pet Type:
<label><input type="radio" name="cat" value="cat" />Cat<label>
<label><input type="radio" name="dog" value="dog" />Dog</label>
Age Range:
<label><input type="radio" name="under8" value="under8" />Under 8</label>
<label><input type="radio" name="over8" value="over8" />Over 8</label>
<input type="submit" name="submit" value="Submit">
</form>
IMO, my way uses less code, and that's what I'm trying to achieve as I teach myself the language.
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 9:51 am
by John Cartwright
blurredvision wrote:tazdevil wrote:I probably shouldn't even be trying to help, since I'm a beginner, too. I don't want to step on the toes of the "big guys" around here

.
Nonsense! We encourage people from all skill levels to help out (it's part of the learning experience after all!)
Also, to explain my first reply above, here's how I would do this as a beginner, instead of using the multiple if/else conditionals. However, use the one you are already working on, just incase somebody comes in here and tells me I don't know what I'm talking about :
Code: Select all
<?php
if (isset($_POST['submit'])); {
if (isset($_POST['cat'] && $_POST['under8'])) {
$page = 'page_url_1';
}
if (isset($_POST['cat'] && $_POST['over8'])) {
$page = 'page_url_2';
}
if (isset($_POST['dog'] && $_POST['under8'])) {
$page = 'page_url_3';
}
if (isset($_POST['dog'] && $_POST['over8'])) {
$page = 'page_url_4';
}
}
?>
<form method="this file.php" action="post">
Pet Type:
<label><input type="radio" name="cat" value="cat" />Cat<label>
<label><input type="radio" name="dog" value="dog" />Dog</label>
Age Range:
<label><input type="radio" name="under8" value="under8" />Under 8</label>
<label><input type="radio" name="over8" value="over8" />Over 8</label>
<input type="submit" name="submit" value="Submit">
</form>
IMO, my way uses less code, and that's what I'm trying to achieve as I teach myself the language.
[/quote]
Using different names will allow the radio elements to all be selected. Remember, we only want one or the other. I haven't thrown in any deeper in the pool than you have, the only difference between our code really is how the forms are setup. In fact, I would argue my implementation is even simpler than yours.
P.S., put error_reporting(E_ALL); at the top of your snippet, and leave the under/over 8 box blank.

Remember, you should always check for the existence of variables (especially input) before using them.
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 10:00 am
by John Cartwright
I just notice there was still a mistake with the code
Code: Select all
if (isset($_POST['breed']) && isset($_POST['age'])) { //make sure values exist before using them
if ($_POST['breed'] == 'dog') {
if ($_POST['age'] == 'under8') {
$page = 'page_url_1';
} else {
$page = 'page_url_2';
}
} else { //cat
if ($_POST['age'] == 'under8') {
$page = 'page_url_3';
} else {
$page = 'page_url_4';
}
}
}
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 10:14 am
by blurredvision
Jcart wrote:I haven't thrown in any deeper in the pool than you have, the only difference between our code really is how the forms are setup. In fact, I would argue my implementation is even simpler than yours.
You know what, now that I look at yours more closely, you're right, your code
is a little simpler than mine. It's about how you look at it, isn't it?

Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 10:29 am
by tazdevil
Hi Guys...
Thanks for all your help, its really appreciated - But I'm in a position where I just need this to work. I will them study what you have offered and delve into what you've done/suggested & why.
Unfortunately I can't get the following to work... can you see anything wrong???
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>
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';
}
}
}
?>
In the above php I have just used some url's for the form to redirect to for testing.
many thanks
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 10:42 am
by John Cartwright
I'm not exactly sure what you mean by does not work. Try running the following code and report what is outputting please. I'm also assuming you infact did include the redirecting code (which I've included commented out below).
EDIT| Take a look at my last post. I updated an error from the previous snippets.
Re: Newbie Question - PHP Form link to URL's
Posted: Sun Apr 06, 2008 10:45 am
by tazdevil
Guys...
Maybe I should have explained earlier.
Smply what I'm trying to achieve is to send the user to 1 of 4 pages (hence the url's I've put in the php), dependant upon what they pick from the above radio buttons in the form that I have on one of my pages.
Simple... but tricky for me without php knowledge!!!
I don't know if I should post to a php page to do the work (hence the html & php files) or do it where the form is.
cheers