boushley Posted November 21, 2007 Share Posted November 21, 2007 I'm wondering what tips you have for making forms in php. I'm specifically wondering what tips everyone has about style I guess. My biggest question is about filling data in when someone returns to a page (ie they didn't fill out all the data and are sent back to the page) Generally I send the data back as post data. And when I put it back into the form things can start to look pretty hairy. such as... <input type="text" name="example" value="<?php if(isset($_GET['example'])){ echo $_GET['example']; } ?>"> another fun one is selecting the correct option in a dropdown... I can do it <select name="example"> <option value="1" <?php if($_GET['example']=='1'){ echo 'selected="selected"'; } ?>>1</option> <option value="2" <?php if($_GET['example']=='1'){ echo 'selected="selected"'; } ?>>2</option> </select> but that turns out pretty ugly. So lets see what style tips or suggestions you've got. Thanks for the help! I don't know where to find something like this. [and if you point me in the direction of some good "best practices" or style guides... that would be appreciated as well] Quote Link to comment https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/ Share on other sites More sharing options...
phpQuestioner Posted November 21, 2007 Share Posted November 21, 2007 well, this question really should be posted in the PHP Help Forum http://www.phpfreaks.com/forums/index.php/board,1.0.html but since I am on this forum right now and I know what you mean; let me give you a phrase for you to google. PHP Session if you do a little bit of research on this; I know that you can accomplish what your wanting to do. Quote Link to comment https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/#findComment-395656 Share on other sites More sharing options...
boushley Posted November 21, 2007 Author Share Posted November 21, 2007 Sorry about posting to the wrong forum... wasn't sure seeing as how it was a mixed topic. If a MOD could move it... that would be great. And I know how to use PHP Sessions... but I was just asking for advice on the style of putting the values back into the fields. I know you could use get, or you could register it to their session and then plug it back into the form. And I can accomplish the task at hand... I'm just wondering if there is a cleaner way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/#findComment-395983 Share on other sites More sharing options...
obsidian Posted November 21, 2007 Share Posted November 21, 2007 I personally don't like using sessions for simple forms. I like to have a form submit to itself and handle all the validation right there. That way, if it doesn't pass, you have the POST information already set to be able to set up the form for them again. Also, if you know you're going to want to set or "remember" the option selected in a dropdown, it's often worth the effort to dynamically generate the dropdown in the first place. Here's a simple form and handler to show you what I mean: <?php if (isset($_POST['submit'])) { $errors = array(); if (!preg_match('|^[a-z -\']+$|i', $_POST['name'])) { $errors[] = "Please provide a valid name!"; } if (!ctype_digit($_POST['number'])) { $errors[] = "Please choose your favorite number!"; } if (count($errors) == 0) { // No errors, so handle form here } } $name = isset($_POST['name']) ? htmlentities($_POST['name'], ENT_QUOTES) : ''; $number = isset($_POST['number']) ? $_POST['number'] : ''; if (isset($errors)) { echo "<p class=\"error\">" . implode('<br />', $errors) . "</p>\n"; } ?> <form name="my_form" action="" method="post"> Your Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br /> Favorite Number: <select name="number"> <option value="">Choose One</option> <?php for ($i = 1; $i <= 10; $i++) { echo "<option value=\"$i\""; echo ($i == $number) ? ' selected="selected"' : ''; echo ">$i</option>\n"; } ?> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/#findComment-396003 Share on other sites More sharing options...
revraz Posted November 21, 2007 Share Posted November 21, 2007 I do it the same way, not much you can do really. I'm wondering what tips you have for making forms in php. I'm specifically wondering what tips everyone has about style I guess. My biggest question is about filling data in when someone returns to a page (ie they didn't fill out all the data and are sent back to the page) Generally I send the data back as post data. And when I put it back into the form things can start to look pretty hairy. Quote Link to comment https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/#findComment-396008 Share on other sites More sharing options...
boushley Posted November 21, 2007 Author Share Posted November 21, 2007 Thanks for the advice obsidian. I had seen forms that were self submitting... and wondered what the benefits were. Thanks. And the dynamically generated select, thats a lot easier than the dirty way I was doing it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/#findComment-396029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.