Jump to content

[SOLVED] Form with if statements


DarkPrince2005

Recommended Posts

Can someone explain and give me an example of how i would do the following:

 

I want to create a form with a drop down box and when the form is submitted the user would be redirected according to the selection he or she made in the drop down box.

 

Here  is a sample of what the drop down box would look like:

<form method="post" action="">
<center>

<table width="600" cellpadding="0" cellspacing="0">
<tr>
	<td>
		<table>
			<tr>
				<td><b>I need:</td>
				<td> </td>
				<td><select name="status">
					<option value="-SELECT-">-SELECT-</option>
					<option value="On Site Support">On Site Support</option>
					<option value="Website Development">Website Development</option>
					</select></td>
			</tr>
		</table>
	</td>
</tr>
</table>
</form>

Link to comment
https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/
Share on other sites

u can use javascript and use the "onchange" event on the <select> tag....

 

but with php, if you submit this form with the given elements, make a action="this.php" page with this...

 

<?php

if($_POST['status'] == "On Site Support")
  header("Location: url1.php");
elseif($_POST['status'] == "Website Development")
  header("Location: url2.php");
else
  exit("You didn't give a valid redirection point.");

?>

<?	if (isset($_POST['submit'])) {
$site = $_POST[status];
header('Location:' . $site .'');
}
?>
<!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>Redirect from forms</title>
</head>

<body>
<form method="post" action="">
<center>

<table width="600" cellpadding="0" cellspacing="0">
<tr>
	<td>
		<table>
			<tr>
				<td><b>I need:</td>
				<td> </td>
				<td><select name="status">
					<option value="-SELECT-">-SELECT-</option>
					<option value="http://google.com">On Site Support</option>
					<option value="http://yahoo.com">Website Development</option>
					</select>
                        <input type="submit" name="submit" value="submit" /></td>
			</tr>
		</table>
	</td>
</tr>
</table>
</form>
</body>
</html>

heres a simple example hope youll find it usefull


<script>

function go(){

var k = document.getElementById('sites');



location.href=k.value;

}

</script>

<select id="sites" name='sites' onchange='go();'>

<option value="http://google.com">site 2</option>

<option value="http://yahoo.com">site 2</option>

</select>

voyde, i think it is better practice to filter and try and examine any possible user input. with your method someone could possibly place whatever they want within the 'status' variable. maybe in this instance, it's not a big problem. but, someone could still play with your code.

Using case would be cleaner ;)

 

<?php

switch ($_POST['status']) { // Couldn't use spaces becuase it was messing up the way it showed on the forum
case "On Site Support":
header("Location: url1.php");
break;
case "Website Development":
header("Location: url2.php");
break;
default:
exit("You didn't give a valid redirection point.");
break;
}
?>

 

or

 

<?php

header("Location: {$_POST['status']}");

?>

<!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>Redirect from forms</title>
</head>

<body>
<form method="post" action="">
<center>

<table width="600" cellpadding="0" cellspacing="0">
<tr>
	<td>
		<table>
			<tr>
				<td><b>I need:</td>
				<td> </td>
				<td><select name="status">
					<option value="-SELECT-">-SELECT-</option>
					<option value="location1.php">On Site Support</option>
					<option value="location2.php">Website Development</option>
					</select>
           <input type="submit" name="submit" value="submit" />
         </td>
			</tr>
		</table>
	</td>
</tr>
</table>
</form>
</body>
</html>

 

On this one change the values of the selects to the location you want them to go to.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.