Page 1 of 1

URL HELP

Posted: Sat Jan 01, 2011 6:23 pm
by jsk1gcc
Hi, I've been researching and all I can find is information on how to change URLS not how to redirect a user if statement matches input.

I don't think this makes any difference but the $result comes from a drop down box.
(if that code is needed, let me know)

I would like something like this:

Code: Select all

if $result="Swinging Ship" GO TO PAGE <a href="Swing.php">Ultra Rides</a> 

if $result="Roller Coaster" GOT TO PAGE <a href="Roller.php">Ultra Rides</a>

if $result="Ice Blast" GO TO PAGE <a href="Ice.php">Ultra Rides</a>
I need the syntax for this, should I use a loop, if else, while maybe?
not sure what to use.

thanks for any help on this. :)

Re: URL HELP

Posted: Sat Jan 01, 2011 8:30 pm
by Neilos
You can use the header() function to send a raw http header. Read more http://php.net/manual/en/function.header.php.

Code: Select all

if ($result == "Swinging Ship") {
	header('Location: Swing.php');
} elseif ($result == "Roller Coaster") {
	header('Location: Roller.php');
} elseif ($result == "Ice Blast") {
	header('Location: Ice.php');
} else {
	// there was an error
}

Re: URL HELP

Posted: Sun Jan 02, 2011 2:37 am
by social_experiment
Use a case statement instead of if-elseif-else.

Code: Select all

<?php
 switch($result) {
 case('Swinging Ship'):
 header('location: swing.php');
 break;
 case('Roller Coaster'):
 header('location: roller.php');
 break;
 case('Ice Blast'):
 header('location: ice.php');
 break;
 // in case none of the results match
 default:
 header('default.php'); 
 }
?>

Re: URL HELP

Posted: Sun Jan 02, 2011 8:12 am
by jsk1gcc
hi again, having trouble getting the result from the drop down box to direct to another page

the if statement works when i don't try and do anything with it.

should the IF statement go on another page and the submit button POST the $result, i'm really not sure. any help would be great..

code so far

index.php

Code: Select all

<?php
$query = "SELECT * FROM `ride` ORDER BY name";  
$result = mysql_query($query);  

	echo"<select name='name'><option value=''>Select Ride</option>";
		while( $row = mysql_fetch_array($result) ) 
		{ 
    		echo '<option>'.$row['name'].'</option>'; 
		}  
			echo '</select>'; 
	mysql_free_result( $result ); 

//// do rest of form when done/////

echo "<form method=post name=f1 action='dd-check.php'>";
echo "<input type=submit value=Submit>";
echo "</form>";

?>
dd-check.php

Code: Select all

<?php
$result =$_POST['WHAT GOES HERE?']; <-----------???? is this even needed?
echo  "Ride Choosen = $result";            <-----------

if ($result == "Swinging Ship") {
       		 header('Location: Swing.php');
		} elseif ($result == "Roller Coaster") {
       		 header('Location: Roller.php');
		} elseif ($result == "Ice Blast") {
       		 header('Location: Ice.php');
		} else {
        echo "choose a ride";
		}
?>

Re: URL HELP

Posted: Sun Jan 02, 2011 11:44 am
by social_experiment
This has to go between <form> tags or it isn't part of the form.

Code: Select all

<?php
$query = "SELECT * FROM `ride` ORDER BY name";  
$result = mysql_query($query);  

        echo"<select name='name'><option value=''>Select Ride</option>";
                while( $row = mysql_fetch_array($result) ) 
                { 
                echo '<option>'.$row['name'].'</option>'; 
                }  
                        echo '</select>'; 
        mysql_free_result( $result );
?>