homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help with PHP form
Red_Rain




msg:4404860
 11:36 pm on Jan 7, 2012 (gmt 0)

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!

<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>

 

rocknbil




msg:4405257
 5:02 pm on Jan 9, 2012 (gmt 0)

Welcome aboard Red_Rain,

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.


This is a better approach anyway. Javascript should be used as progressive enhancement, that is, build it without JS and add it later. That way it will still work with JS disabled.

Is this working ok and you're just asking if it's optimized? The logic seems sound enough, I'd just add there's no reason to set the variables (though I can see why, it's in a few places and probably a bit more efficient.)

The only glaring thing is that there are vulerabilities with PHP_SELF, define a variable somewhere and use it instead.

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>" method="post" name="form">

Here

<?php if(isset($outside)) { ?>

you can do something like this. If you're learning, learn to error trap now. What if there are no matching conditions in your if/else if? Nothing displays, and you have to wonder what's wrong. You should terminate that with an error condition, even if it's never used when everything is working. Also it's always a good idea to check for the set condition from input. There is a good deal to be discussed about cleansing input, but in effect what you're doing in this piece is the foundation of managing data cleansing - accept only what you want and throw everything else away. You're looking for specific values, and if there's no absolute match you do nothing. Keep doing that. :-)

<?php
if(isset($_POST['in_or_out'])) {
if($_POST['in_or_out']=='inside') { // or use your variables ?>
<select name="application" >
<option value="opt1">option1</option>
<option value="opt2">option2</option>
<option value="opt3">option3</option>
</select>
<?php } else if($_POST['in_or_out']=='outside') { ?>
<select name="application" >
<option value="opt1">option1</option>
<option value="opt2">option2</option>
</select>
<?php } else { ?>
<p>An invalid in or out parameter was submitted.</p>
<?php }
}
else { // post in or out not set ?>
<p>Please use the form, it seems no values were submitted.</p>
<?php } ?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved