Re: [PHP] How would you write this?
On Sat, Dec 03, 2005 at 10:00:14PM -0500, Michael B Allen wrote: The following code works but I'm a little new to PHP and I'd like to know if there are better ways to achive the same thing. In the below form example, if the user supplies one or more fields but not all that are required, the form is redisplayed but fields that were supplied are prepopulated and the ones that are required but are missing are highlighted in red. Can anyone recommend a better technique or elighten me about features of the language that I've missed? Mike html body ?php $set = 0; $username = ; $password = ; if (isset($_POST['username'])) { $username = trim($_POST['username']); if (strlen($username) 0) { $set |= 0x01; } } if (isset($_POST['password'])) { $password = trim($_POST['password']); if (strlen($password) 0) { $set |= 0x02; } } if (($set 0x03) == 0x03) { echo Logging in with: . $username . ':' . $password; } else { ? form action=login.php method=post table trtd colspan=2Login:/td/tr trtdUsername:/td ?php if ($set != 0 ($set 0x01) == 0) { echo td bgcolor=\#ff\; } else { echo td; } echo input type=\username\ name=\username\ value=\ . $username . \/; ? /td/tr trtdPassword:/td ?php if ($set != 0 ($set 0x02) == 0) { echo td bgcolor=\#ff\; } else { echo td; } ? input type=password name=password/ /td/tr trtd colspan=2input type=submit value=Login//td/tr /table /form style .errored { color: #FF; } /style ?php $set_class = array(0 = '', 1 = 'errored'); define('SET_USERNAME', 0x01); define('SET_PASSWORD', 0x02); define('SET_ALL', SET_USERNAME SET_PASSWORD); $set = 0; $username = ; $password = ; if (isset($_POST['username'])) { $username = trim($_POST['username']); if (strlen($username) 0) { $set |= SET_USERNAME; } } ... if (($set SET_ALL) == SET_ALL) { echo Logging in with: . $username . ':' . $password; } else { ? form action=login.php method=post table trtd colspan=2Login:/td/tr trtdUsername:/td ?php $class = $set_class[ $set SET_USERNAME ]; echo td class=$class echo input type=\username\ name=\username\ value=\ . $username . \/; ... It might be a bit overkill for such a simple thing.. but heck.. i was bored :) Curt. -- cat .signature: No such file or directory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How would you write this?
On Sun, 4 Dec 2005 11:44:12 -0800 Curt Zirzow [EMAIL PROTECTED] wrote: style .errored { color: #FF; } /style ?php $set_class = array(0 = '', 1 = 'errored'); define('SET_USERNAME', 0x01); define('SET_PASSWORD', 0x02); define('SET_ALL', SET_USERNAME SET_PASSWORD); $set = 0; $username = ; $password = ; if (isset($_POST['username'])) { $username = trim($_POST['username']); if (strlen($username) 0) { $set |= SET_USERNAME; } } ... if (($set SET_ALL) == SET_ALL) { echo Logging in with: . $username . ':' . $password; } else { ? form action=login.php method=post table trtd colspan=2Login:/td/tr trtdUsername:/td ?php $class = $set_class[ $set SET_USERNAME ]; echo td class=$class echo input type=\username\ name=\username\ value=\ . $username . \/; ... It might be a bit overkill for such a simple thing.. but heck.. i was bored :) Interesting. This gives me some ideas. I'm also happy to know I wasn't too far off :- Thanks, Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] How would you write this?
Hi... the easiest way I have found to do exactly what your looking for is to use a pear package called html_quickform. I found it easy to use and it saves me hours and hours of work too. If your interested go to www.pear.php.net/html_quickform and have a look. -Original Message- From: Michael B Allen [mailto:[EMAIL PROTECTED] Sent: Saturday, December 03, 2005 10:00 PM To: php-general@lists.php.net Subject: [PHP] How would you write this? The following code works but I'm a little new to PHP and I'd like to know if there are better ways to achive the same thing. In the below form example, if the user supplies one or more fields but not all that are required, the form is redisplayed but fields that were supplied are prepopulated and the ones that are required but are missing are highlighted in red. Can anyone recommend a better technique or elighten me about features of the language that I've missed? Mike html body ?php $set = 0; $username = ; $password = ; if (isset($_POST['username'])) { $username = trim($_POST['username']); if (strlen($username) 0) { $set |= 0x01; } } if (isset($_POST['password'])) { $password = trim($_POST['password']); if (strlen($password) 0) { $set |= 0x02; } } if (($set 0x03) == 0x03) { echo Logging in with: . $username . ':' . $password; } else { ? form action=login.php method=post table trtd colspan=2Login:/td/tr trtdUsername:/td ?php if ($set != 0 ($set 0x01) == 0) { echo td bgcolor=\#ff\; } else { echo td; } echo input type=\username\ name=\username\ value=\ . $username . \/; ? /td/tr trtdPassword:/td ?php if ($set != 0 ($set 0x02) == 0) { echo td bgcolor=\#ff\; } else { echo td; } ? input type=password name=password/ /td/tr trtd colspan=2input type=submit value=Login//td/tr /table /form ?php } ? p/a href=login.phpclick me/a /body /html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How would you write this?
I personally would use javascript to evaluate the form and highlight the fields onsubmit. However as a backup I'd do the evaluating in php and add an error label under the field. echo td; if ($noPass) echo color='red'; echo Password/td; if ($noPass) echo td color='red'You must supply a password/td; Something along those lines. On Saturday 03 December 2005 21:00, Michael B Allen wrote: The following code works but I'm a little new to PHP and I'd like to know if there are better ways to achive the same thing. In the below form example, if the user supplies one or more fields but not all that are required, the form is redisplayed but fields that were supplied are prepopulated and the ones that are required but are missing are highlighted in red. Can anyone recommend a better technique or elighten me about features of the language that I've missed? Mike html body ?php $set = 0; $username = ; $password = ; if (isset($_POST['username'])) { $username = trim($_POST['username']); if (strlen($username) 0) { $set |= 0x01; } } if (isset($_POST['password'])) { $password = trim($_POST['password']); if (strlen($password) 0) { $set |= 0x02; } } if (($set 0x03) == 0x03) { echo Logging in with: . $username . ':' . $password; } else { ? form action=login.php method=post table trtd colspan=2Login:/td/tr trtdUsername:/td ?php if ($set != 0 ($set 0x01) == 0) { echo td bgcolor=\#ff\; } else { echo td; } echo input type=\username\ name=\username\ value=\ . $username . \/; ? /td/tr trtdPassword:/td ?php if ($set != 0 ($set 0x02) == 0) { echo td bgcolor=\#ff\; } else { echo td; } ? input type=password name=password/ /td/tr trtd colspan=2input type=submit value=Login//td/tr /table /form ?php } ? p/a href=login.phpclick me/a /body /html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php