Jump to content

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/78173-solved-html-php-clarity/
Share on other sites

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.

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.

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>

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.