URL HELP

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

Post Reply
jsk1gcc
Forum Newbie
Posts: 17
Joined: Fri Dec 31, 2010 4:02 pm

URL HELP

Post 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. :)
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL HELP

Post 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
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: URL HELP

Post 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'); 
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jsk1gcc
Forum Newbie
Posts: 17
Joined: Fri Dec 31, 2010 4:02 pm

Re: URL HELP

Post 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";
		}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: URL HELP

Post 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 );
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply