Help with PHP form

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
Red_Rain
Forum Newbie
Posts: 4
Joined: Sat Jan 07, 2012 2:19 pm

Help with PHP form

Post by Red_Rain »

Hey guys,
New to the forum, new to PHP and could really use som help. All i really understand are the basics (as you will see from my code lol) but i am the dive in and do it person or else i wont learn :/. I think that my syntax is written correctly but i am to new to this language and programming in general to have a valid opinion. I do not know how to use AJAX yet so i basically having people fill out a form to get to one of two other forms. Does that make sense? Here is my code and i am open to any and all wisdom and knowledge this community can give. Thanks in advance everyone!

Code: Select all

 <!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>Untitled Document</title>
<?php if($_POST['in_or_out'] == "inside" ) { $inside = $_POST['in_or_out']; } elseif($_POST['in_or_out'] == "outside" ) {$outside = $_POST['in_or_out']; }?>
</head>

<body>
<h4> Please answer the following questions to receive a hardware recommendation or Fill out the contact us form for more complex questions</h4>
<?php if(!isset($_POST['form'])) { ?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>" method="post" name="form">
<p>Indoor or Outdoor application: <select name='in_or_out'><option value=''>Select one</option><option name="inside" value="inside">Inside</option><option name="outside" value="outside">Outside</option>
</select>
</p>
<input type="submit" name="submit" value="Submit">
</form>
<?php } if(isset($_POST['form'])) { ?> 
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>" method="post" name="form">
<p>Indoor or Outdoor application: <select name='in_or_out'><option value=''>Select one</option><option name="inside" <?php if(isset($inside)){echo "selected"; }?> value="inside">Inside</option><option name="outside" <?php if(isset($outside)) {echo "selected" ; } ?>  value="outside">Outside</option>
</select>
</p>
<?php if(isset($outside)) { ?>
<select name="application" >
<option value="opt1">option1</option>
<option value="opt2">option2</option>
<option value="opt3">option3</option>
</select>
<?php } elseif(isset($inside)) { ?>
<select name="application" >
<option value="opt1">option1</option>
<option value="opt2">option2</option>
</select>
<?php } ?> 

<br /> 
<input type="submit" name="submit" value="Submit">
</form>

<?php } ?>
</body>
</html>
landshark
Forum Newbie
Posts: 5
Joined: Sat Jan 07, 2012 10:34 am

Re: Help with PHP form

Post by landshark »

1) Structure your code to help you understand it

2) If you're going to put multiple forms on a page, they need to have unique names. (Resist the urge to put multiple forms on the same page. When you do need to do so, it will be for wholly separate functionality, like putting login, email subscription and search forms all on the same page)

3) $POST variables are not available to you until after the form has been submitted. They cannot help you generate the form.

4) The select option value from the first form is not available to the second form. This cannot be done in a single page in PHP. You need to either

a) split this out into two separate pages, or
b) you can use JavaScript to interrogate the value of the first select

If you choose b), do not put the first select in a separate form, put everything in the same form.

Form 1

Code: Select all

<!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>Untitled Document</title>
</head>

<body>
	<h4> Please answer the following questions to receive a hardware recommendation or Fill out the contact us form for more complex questions</h4>
	
	<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>" method="post" name="form">
		<p>Indoor or Outdoor application: 
			<select name='in_or_out'>
				<option value=''>Select one</option>
				<option name="inside" value="inside">Inside</option>
				<option name="outside" value="outside">Outside</option>
			</select>
		</p>
		<input type="submit" name="submit" value="Submit">
	</form>
</body>
</html>
Form 2

Code: Select all

<!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>Untitled Document</title>
<?php 
	if($_POST['in_or_out'] == "inside" ) { 
		$inside = $_POST['in_or_out']; 
	} 
	elseif($_POST['in_or_out'] == "outside" ) {
		$outside = $_POST['in_or_out']; 
	}
?>
</head>

<body>
	<h4> Please answer the following questions to receive a hardware recommendation or Fill out the contact us form for more complex questions</h4>
	
	<?php if(isset($_POST['form'])) { ?>
	<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>" method="post" name="form">
		<?php if(isset($outside)) { ?>
			<select name="application" >
				<option value="opt1">option1</option>
				<option value="opt2">option2</option>
				<option value="opt3">option3</option>
			</select>
		<?php } elseif(isset($inside)) { ?>
			<select name="application" >
				<option value="opt1">option1</option>
				<option value="opt2">option2</option>
			</select>
		<?php } ?>
		
		<br />
		<input type="submit" name="submit" value="Submit">
	</form>
	
	<?php } ?>
</body>
</html>
Note that I haven't tested these
Red_Rain
Forum Newbie
Posts: 4
Joined: Sat Jan 07, 2012 2:19 pm

Re: Help with PHP form

Post by Red_Rain »

thanks a lot for your reply! Greatly appreciate it. It works when i only use form 2 but i must submit to get the next values. I do not now javascript or AJAX. PHP is the first scripting language i am learning. I will just make it really noobish i guess as long as it works lol Thanks again bro
Post Reply