Reply
Using an "if" statement to direct to a page
Old 10-09-2009, 01:56 PM Using an "if" statement to direct to a page
Novice Talker

Posts: 9
Trades: 0
I have created a simple form with 3 check boxes and depending on how the boxes are checked I want the individual to be directed to a specific page. Here is the link http://beylahrosenbaum.com/test/ - and the code for this page

Code:
<form action="selection.php" method="post">

<table cellpadding="10" cellspacing="0" border="1">
<tr><td><input name="food" type="checkbox" value="Yes"> &nbsp; <span class="head">Food</span><br /><br /><strong>Includes:</strong> Fruit Tray, Drinks & Snacks</td>

<td><input name="it" type="checkbox" value="Yes"> &nbsp; <span class="head">IT Equipment</span><br /><br /><strong>Includes:</strong> Tele/Video Conferencing, Laptops, Projector & Camera</td>

<td><input name="misc" type="checkbox" value="Yes"> &nbsp; <span class="head">Misc</span><br /><br /><strong>Includes:</strong> Screen & Rearrangment of Furniture</td>

<tr><td colspan="3" align="center"><input onclick="return candosubmit(this.form)" type="image" value="submit" src="images/submit.jpg" border="0"></td></tr>
</table>

</form>
Here is the if statements:

Code:
<?php
if($food=="Yes")
   {
      $value1="1";
   } else {
      $value1="0";
   }   
if($it=="Yes")
   {
      $value2="3";
   } else {
      $value2="0";
   }   
if($misc=="Yes")
   {
      $value2="5";
   } else {
      $value2="0";
   }   

if($value1 + $value2 + $value3 ="1")
{ include("form-1.html"); }

elseif($value1 + $value2 + $value3 ="3")
{ include("form-2.html"); }

elseif($value1 + $value2 + $value3 ="5")
{ include("form-3.html"); }

elseif($value1 + $value2 + $value3 ="9")
{ include("form-4.html"); }

elseif($value1 + $value2 + $value3 ="4")
{ include("form-5.html"); }

elseif($value1 + $value2 + $value3 ="8")
{ include("form-6.html"); }

elseif($value1 + $value2 + $value3 ="6")
{ include("form-7.html"); }

?>
beylah is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 10-09-2009, 02:19 PM Re: Using an "if" statement to direct to a page
Average Talker

Posts: 23
Location: Germany
Trades: 0
PHP Code:
<?php
$food 
$_POST['food'];
$it $_POST['it'];
$misc $_POST['misc'];

if(
$food=="Yes")
   {
      
$value1=1;
   } else {
      
$value1=0;
   }   
if(
$it=="Yes")
   {
      
$value2=3;
   } else {
      
$value2=0;
   }   
if(
$misc=="Yes")
   {
      
$value3=5;
   } else {
      
$value3=0;
   }   

if(
$value1 $value2 $value3 ==1)
{ include(
"form-1.html"); }

elseif(
$value1 $value2 $value3 ==3)
{ include(
"form-2.html"); }

elseif(
$value1 $value2 $value3 ==5)
{ include(
"form-3.html"); }

elseif(
$value1 $value2 $value3 ==9)
{ include(
"form-4.html"); }

elseif(
$value1 $value2 $value3 ==4)
{ include(
"form-5.html"); }

elseif(
$value1 $value2 $value3 ==8)
{ include(
"form-6.html"); }

elseif(
$value1 $value2 $value3 ==6)
{ include(
"form-7.html"); }

?>
__________________
|MuscularBrain.com - Psychology, Philosophy and Self Development
|Criticizer.net - All About the Internet and Programming
Trey Walter is offline
Reply With Quote
View Public Profile Visit Trey Walter's homepage!
 
Old 10-09-2009, 06:26 PM Re: Using an "if" statement to direct to a page
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Try this.

PHP Code:
<?php

$food 
= isset($_GET['food']) ? 0;
$it = isset($_GET['it']) ? 0;
$misc = isset($_GET['misc']) ? 0;

$sum food it misc;

$redirect = array(
   
=> "page for none selcted",
   
=> "page for only food selcted",
   
=> "page for only it selcted",
   
=> "page for only misc selcted",
   
=> "page for food and misc selcted",
   
=> "page for it and misc selcted",
   
=> "page for all selcted",
);

include(
$redirect[$sum]);

// Or, if you actually want to redirect, not just include the page,
// uncomment this line
// header("Location: " . $redirect[$sum]);

?>
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f

Last edited by lizciz; 10-13-2009 at 07:12 PM..
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-13-2009, 09:42 AM Re: Using an "if" statement to direct to a page
Novice Talker

Posts: 9
Trades: 0
delete

Last edited by beylah; 10-13-2009 at 10:08 AM..
beylah is offline
Reply With Quote
View Public Profile
 
Old 10-13-2009, 09:55 AM Re: Using an "if" statement to direct to a page
Novice Talker

Posts: 9
Trades: 0
delete

Last edited by beylah; 10-13-2009 at 10:09 AM..
beylah is offline
Reply With Quote
View Public Profile
 
Old 10-13-2009, 10:01 AM Re: Using an "if" statement to direct to a page
Novice Talker

Posts: 9
Trades: 0
I used Trey - thank you thank worked - if I may ask a question:

I understand now that I needed to post the results first - makes sense

my question is when do you use the == and when do you use the =

i see you had to correct that for me

Last edited by beylah; 10-13-2009 at 10:05 AM..
beylah is offline
Reply With Quote
View Public Profile
 
Old 10-13-2009, 07:11 PM Re: Using an "if" statement to direct to a page
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You use a single = to assign a value to a variable, as in
$x = 3;

You use double == to compare two values, to see if they are equal, as in

$x == 3 will be true
$x == 4 will be false

So by using a single = in an if() statement like so

if ($x = 4) {...}

means you assign the value 4 to $x, regardless of it's previous value, and if the operation is succesfull (which it always is) it will return true and the if() statement will always run.

There is also a third operator with three ===, which takes variable types into account when comparing. This should show the difference.

PHP Code:
$x 3// the number 3
$y '3'// the string '3'

if ($x == $y) {
   
// this is true
}
if (
$x === $y) {
   
// this is false

__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-13-2009, 07:26 PM Re: Using an "if" statement to direct to a page
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Easy PHP Search with Opera
Posts: 1,068
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by lizciz View Post
means you assign the value 4 to $x, regardless of it's previous value, and if the operation is succesfull (which it always is) it will return true and the if() statement will always run.
That's not 100% true. The return value of an assignment is the value being assigned. The following code will echo 'One':
PHP Code:
if($x NULL)
  echo 
'Null';
else if(
$x 0)
  echo 
'Zero';
else if(
$x '')
  echo 
'Empty String';
else if(
$x 1)
  echo 
'One'
This is because in PHP false == NULL == '' == 0.
__________________
Tinsology | How to Post Code | EverythingDev
NullPointer is offline
Reply With Quote
View Public Profile
 
Old 10-14-2009, 09:14 AM Re: Using an "if" statement to direct to a page
Novice Talker

Posts: 9
Trades: 0
thank you all for you help - this makes sense now
beylah is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Using an "if" statement to direct to a page
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.38323 seconds with 13 queries