Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-12 Thread Tamara Temple


On Sep 11, 2010, at 12:14 PM, Jason Pruim wrote:

On Sep 11, 2010, at 12:39 PM, Tamara Temple wrote:

Rather than repeating all that code, I suggest the following:

[snip]
That's actually what I'm trying to get away from. I was hoping to do  
it all in HEREDOC syntax. I've always thought it made it cleaner.   
But that is a personal opinion :)


Well, from a maintainability aspect, the way i showed makes more sense  
because if there are changes to be made (such as adding another  
option), you only have to make one change, not n changes.




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread a...@ashleysheridan.co.uk
For a month selector, using a loop to output the months is good, as you can 
then within the loop check for that value sent and set the selected html 
attribute for that select element.

I should warn you that your code will throw a warning when no data has been 
posted to it. Consider using isset() instead to check for posted values rather 
than comparing a value (which might not exist) with true.

Thanks,
Ash
http://www.ashleysheridan.co.uk

- Reply message -
From: Jason Pruim li...@pruimphotography.com
Date: Sat, Sep 11, 2010 14:49
Subject: [PHP] Elegance is the goal... Sticky form submit help
To: PHP-General list php-general@lists.php.net

Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might be  
coherent enough to help me find a more elegant solution to a problem  
that I have...

I have a form for submitting an event to a website, and if the form is  
not submitted successfully (such as they didn't fill out a required  
field) I want it to redisplay the form with inline errors as to what  
happened and display the values they selected...

I have a working solution but was hoping for something a little more  
elegant. And something that would work better for a month selector as  
well... Here is the relevant code that I have that works:

?PHP
 if ($_POST['hidSubmit'] ==TRUE  $_POST['type'] == meeting):
echo HTML
 select name=type id=type
 option value=0-- select type --/option
 option value=meeting selectedMeeting/option
 option value=event Event/option
 /select
HTML;

elseif ($_POST['hidSubmit'] == TRUE  $_POST['type'] == event):
//if ($_POST['hidSubmit'] == TRUE  $_POST['type'] == event) {

echo HTML
 select name=type id=type
 option value=0-- select type --/option
 option value=meetingMeeting/option
 option value=event selectedEvent/option
 /select
HTML;

else:
//if ($_POST['hidSubmit'] != TRUE):


echo HTML
 select name=type id=type
 option value=0 selected-- select type --/option
 option value=meetingMeeting/option
 option value=eventEvent/option
 /select
HTML;
endif;

?

which works BUT I don't want to have to have that for a month selector  
or a day selector :)

Any ideas what I'm missing?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread tedd

At 9:49 AM -0400 9/11/10, Jason Pruim wrote:

Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might be 
coherent enough to help me find a more elegant solution to a problem 
that I have...


I have a form for submitting an event to a website, and if the form 
is not submitted successfully (such as they didn't fill out a 
required field) I want it to redisplay the form with inline errors 
as to what happened and display the values they selected...


-snip-


Any ideas what I'm missing?


Jason:

I think what you are missing is that this data collection should be 
split between client-side and server-side operations.


For client-side simply use javascript to monitor what they user 
enters and then immediately respond to the requirements imposed upon 
the user.


After the user fills out the information correctly and clicks submit, 
then have your server-side scripts check the data again and respond 
accordingly.


Here are a couple of examples:

http://webbytedd.com/c/form-calc/

http://webbytedd.com/c/form-submit/


Cheers,

tedd

--
---
http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Tamara Temple

Rather than repeating all that code, I suggest the following:


select name=type id=type
   option value=0-- select type --/option
   option value=meeting ?php echo (isset($_POST['hidSubmit']   
$_POST['hidSubmit'] == TRUE  $_POST['type'] == meeting) ?  
selected : '' ?
   option value=event ?php echo (isset($_POST['hidSubmit']   
$_POST['hidSubmit'] == TRUE  $_POST['type'] == event) ?  
selected : '' ?

/select


For a month selector, try:

?php
   // assume current month number is in $curr_month
   $months = array(1 = January, February, March, April,  
May, June, July, August, September, October, November,  
December); // will create an array of month names, with a starting  
base of 1 (instead of zero)


  echo select name=\month\ id=\month\\n;
  foreach ($months as $m = $mname) {
echo option value=\$m\;
if ($curr_month == $m) echo  selected ;
echo $mname/option\n;
  }
  echo /select\n;
?

There are other possiblities as well. One time, I didn't want to  
actually store the month names, perhaps allowing them to be localized.  
Instead I used strftime, which will return appropriate names based on  
locale:


?php
  // assume current month number is in $curr_month

  echo select name=\month\ id=\month\\n;
  for ($m = 1; $m = 12; $m++) {
echo option value=\$m\;
if ($curr_month == $m) echo  selected ;
echo ;
$mname = strftime(%B, 0,0,0,2010, $m, 1); // time, year and day  
don't matter, all we're after is the appropriate month name set in the  
locale

echo $mname;
echo /option\n;
  }
  echo /select\n;
?




On Sep 11, 2010, at 8:49 AM, Jason Pruim wrote:


Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might be  
coherent enough to help me find a more elegant solution to a problem  
that I have...


I have a form for submitting an event to a website, and if the form  
is not submitted successfully (such as they didn't fill out a  
required field) I want it to redisplay the form with inline errors  
as to what happened and display the values they selected...


I have a working solution but was hoping for something a little more  
elegant. And something that would work better for a month selector  
as well... Here is the relevant code that I have that works:


?PHP
   if ($_POST['hidSubmit'] ==TRUE  $_POST['type'] == meeting):
echo HTML
   select name=type id=type
   option value=0-- select type --/option
   option value=meeting selectedMeeting/option
   option value=event Event/option
   /select
HTML;

elseif ($_POST['hidSubmit'] == TRUE  $_POST['type'] == event):
//if ($_POST['hidSubmit'] == TRUE  $_POST['type'] == event) {

echo HTML
   select name=type id=type
   option value=0-- select type --/option
   option value=meetingMeeting/option
   option value=event selectedEvent/option
   /select
HTML;

else:
//if ($_POST['hidSubmit'] != TRUE):


echo HTML
   select name=type id=type
   option value=0 selected-- select type --/option
   option value=meetingMeeting/option
   option value=eventEvent/option
   /select
HTML;
endif;

?

which works BUT I don't want to have to have that for a month  
selector or a day selector :)


Any ideas what I'm missing?



--
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] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Tamara Temple
The debate on client-side vs. server-side form validation is ongoing.  
Client-side is more responsive, and attempts to keep bad data from  
ever reaching your application, but relies on javascript being  
enabled. Since this is something easily turned off by users, one can't  
always rely on it to do form validation. So server-side validation is  
needed as well to allow your full application to gracefully degrade in  
the absence of working javascript on the client's side. Coding  
defensively helps!


On Sep 11, 2010, at 10:55 AM, tedd wrote:


At 9:49 AM -0400 9/11/10, Jason Pruim wrote:

Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might  
be coherent enough to help me find a more elegant solution to a  
problem that I have...


I have a form for submitting an event to a website, and if the form  
is not submitted successfully (such as they didn't fill out a  
required field) I want it to redisplay the form with inline errors  
as to what happened and display the values they selected...


-snip-


Any ideas what I'm missing?


Jason:

I think what you are missing is that this data collection should be  
split between client-side and server-side operations.


For client-side simply use javascript to monitor what they user  
enters and then immediately respond to the requirements imposed upon  
the user.


After the user fills out the information correctly and clicks  
submit, then have your server-side scripts check the data again and  
respond accordingly.


Here are a couple of examples:

http://webbytedd.com/c/form-calc/

http://webbytedd.com/c/form-submit/


Cheers,

tedd

--
---
http://sperling.com/

--
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] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Jason Pruim


On Sep 11, 2010, at 11:55 AM, tedd wrote:


At 9:49 AM -0400 9/11/10, Jason Pruim wrote:

Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might  
be coherent enough to help me find a more elegant solution to a  
problem that I have...


I have a form for submitting an event to a website, and if the form  
is not submitted successfully (such as they didn't fill out a  
required field) I want it to redisplay the form with inline errors  
as to what happened and display the values they selected...


-snip-


Any ideas what I'm missing?


Jason:

I think what you are missing is that this data collection should be  
split between client-side and server-side operations.


Hey tedd,

Thanks for the response but for this particular project I'm avoiding   
using anything but standard HTML since it will be used almost  
exclusively  by people using screen readers and other assistive  
technology so I'm going a little old school with it to make sure it  
all works for everyone else first.





For client-side simply use javascript to monitor what they user  
enters and then immediately respond to the requirements imposed upon  
the user.


After the user fills out the information correctly and clicks  
submit, then have your server-side scripts check the data again and  
respond accordingly.


Here are a couple of examples:

http://webbytedd.com/c/form-calc/

http://webbytedd.com/c/form-submit/


I will definitely be checking out your examples though! Some of the  
cleanest, well documented, and easiest to read code I have seen in a  
long time!







--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Jason Pruim

Hey Ash,


On Sep 11, 2010, at 10:58 AM, a...@ashleysheridan.co.uk wrote:

For a month selector, using a loop to output the months is good, as  
you can then within the loop check for that value sent and set the  
selected html attribute for that select element.


that's what I was thinking too... Now just to work out the logic :)


I should warn you that your code will throw a warning when no data  
has been posted to it. Consider using isset() instead to check for  
posted values rather than comparing a value (which might not exist)  
with true.


I have other code that will catch it if it's empty but a good  
suggestion none the less! If I stick with that code for some reason  
I'll be updating it to use isset()


Thanks Ash!


Thanks,
Ash
http://www.ashleysheridan.co.uk

- Reply message -
From: Jason Pruim li...@pruimphotography.com
Date: Sat, Sep 11, 2010 14:49
Subject: [PHP] Elegance is the goal... Sticky form submit help
To: PHP-General list php-general@lists.php.net

Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might be
coherent enough to help me find a more elegant solution to a problem
that I have...

I have a form for submitting an event to a website, and if the form is
not submitted successfully (such as they didn't fill out a required
field) I want it to redisplay the form with inline errors as to what
happened and display the values they selected...

I have a working solution but was hoping for something a little more
elegant. And something that would work better for a month selector as
well... Here is the relevant code that I have that works:

?PHP
if ($_POST['hidSubmit'] ==TRUE  $_POST['type'] == meeting):
echo HTML
select name=type id=type
option value=0-- select type --/option
option value=meeting selectedMeeting/option
option value=event Event/option
/select
HTML;

elseif ($_POST['hidSubmit'] == TRUE  $_POST['type'] == event):
//if ($_POST['hidSubmit'] == TRUE  $_POST['type'] == event) {

echo HTML
select name=type id=type
option value=0-- select type --/option
option value=meetingMeeting/option
option value=event selectedEvent/option
/select
HTML;

else:
//if ($_POST['hidSubmit'] != TRUE):


echo HTML
select name=type id=type
option value=0 selected-- select type --/option
option value=meetingMeeting/option
option value=eventEvent/option
/select
HTML;
endif;

?

which works BUT I don't want to have to have that for a month selector
or a day selector :)

Any ideas what I'm missing?



--
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] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Jason Pruim

Hi Tamara,



On Sep 11, 2010, at 12:39 PM, Tamara Temple wrote:


Rather than repeating all that code, I suggest the following:


select name=type id=type
  option value=0-- select type --/option
  option value=meeting ?php echo (isset($_POST['hidSubmit']   
$_POST['hidSubmit'] == TRUE  $_POST['type'] == meeting) ?  
selected : '' ?
  option value=event ?php echo (isset($_POST['hidSubmit']   
$_POST['hidSubmit'] == TRUE  $_POST['type'] == event) ?  
selected : '' ?

/select



That's actually what I'm trying to get away from. I was hoping to do  
it all in HEREDOC syntax. I've always thought it made it cleaner.  But  
that is a personal opinion :)


For a month selector, try:

?php
  // assume current month number is in $curr_month
  $months = array(1 = January, February, March, April,  
May, June, July, August, September, October, November,  
December); // will create an array of month names, with a starting  
base of 1 (instead of zero)


 echo select name=\month\ id=\month\\n;
 foreach ($months as $m = $mname) {
   echo option value=\$m\;
   if ($curr_month == $m) echo  selected ;
   echo $mname/option\n;
 }
 echo /select\n;
?

There are other possiblities as well. One time, I didn't want to  
actually store the month names, perhaps allowing them to be  
localized. Instead I used strftime, which will return appropriate  
names based on locale:


?php
 // assume current month number is in $curr_month

 echo select name=\month\ id=\month\\n;
 for ($m = 1; $m = 12; $m++) {
   echo option value=\$m\;
   if ($curr_month == $m) echo  selected ;
   echo ;
   $mname = strftime(%B, 0,0,0,2010, $m, 1); // time, year and day  
don't matter, all we're after is the appropriate month name set in  
the locale

   echo $mname;
   echo /option\n;
 }
 echo /select\n;
?


I'm actually doing something similar to that right now...

?PHP
if (isset($_POST)) {
$date = date(n, $i);
echo $date;
$i++;
}
?

Prints just the numeric reference to the month.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Jason Pruim


On Sep 11, 2010, at 1:03 PM, Debbie . wrote:


Jason,
I don't really understand the responses you got to this email, I  
have attached the sticky form I made from a book called PHP Visual  
Quickstart guide. It uses an if conditional to print a response if  
the field is empty.

If I'm not helping, sorry, I'm new!

Good luck!

live, love and be happy!

Deb


Hi Deb,

Welcome to the list! It will be the best and worse place you'll ever  
come for help! :)


What you sent is helpful, but due to a personal preference I'm trying  
to avoid going in and out of PHP like you do in there... Trying to use  
HEREDOC for the entire page. It makes more sense to me that way :)


Thank you though! :)



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread Jim Lucas

Jason Pruim wrote:

Hi Tamara,



On Sep 11, 2010, at 12:39 PM, Tamara Temple wrote:


Rather than repeating all that code, I suggest the following:


select name=type id=type
  option value=0-- select type --/option
  option value=meeting ?php echo (isset($_POST['hidSubmit']  
$_POST['hidSubmit'] == TRUE  $_POST['type'] == meeting) ? 
selected : '' ?
  option value=event ?php echo (isset($_POST['hidSubmit']  
$_POST['hidSubmit'] == TRUE  $_POST['type'] == event) ? selected 
: '' ?

/select



That's actually what I'm trying to get away from. I was hoping to do it 
all in HEREDOC syntax. I've always thought it made it cleaner.  But that 
is a personal opinion :)


For a month selector, try:

?php
  // assume current month number is in $curr_month
  $months = array(1 = January, February, March, April, May, 
June, July, August, September, October, November, 
December); // will create an array of month names, with a starting 
base of 1 (instead of zero)


 echo select name=\month\ id=\month\\n;
 foreach ($months as $m = $mname) {
   echo option value=\$m\;
   if ($curr_month == $m) echo  selected ;
   echo $mname/option\n;
 }
 echo /select\n;
?

There are other possiblities as well. One time, I didn't want to 
actually store the month names, perhaps allowing them to be localized. 
Instead I used strftime, which will return appropriate names based on 
locale:


?php
 // assume current month number is in $curr_month

 echo select name=\month\ id=\month\\n;
 for ($m = 1; $m = 12; $m++) {
   echo option value=\$m\;
   if ($curr_month == $m) echo  selected ;
   echo ;
   $mname = strftime(%B, 0,0,0,2010, $m, 1); // time, year and day 
don't matter, all we're after is the appropriate month name set in the 
locale

   echo $mname;
   echo /option\n;
 }
 echo /select\n;
?


I'm actually doing something similar to that right now...

?PHP
if (isset($_POST)) {
$date = date(n, $i);
echo $date;
$i++;
}
?

Prints just the numeric reference to the month.





Here is a combination of Tamara's method and they way that I would do it 
based off her example.  Some of hers didn't work for me out of the box, 
so I modified it to my liking.  Then I included your request to do 
HEREDOC syntax for outputting the list.


?php

$o = array();
for ($m = 1; $m = 12; $m++) {
  $sel = ($curr_month == $m ? ' selected=selected':'');
  $mname = date('F', mktime(0,0,0,$m));

  $o[] = 'option value='.(int)$m.''.$sel.''.
 htmlspecialchars($mname).'/option';
}
$select_month_options = join(\n, $o);

echo HTML
select name=month id=month
{$select_month_options}
/select
HTML;

?

Jim

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread tedd

At 11:42 AM -0500 9/11/10, Tamara Temple wrote:
The debate on client-side vs. server-side form validation is 
ongoing. Client-side is more responsive, and attempts to keep bad 
data from ever reaching your application, but relies on javascript 
being enabled. Since this is something easily turned off by users, 
one can't always rely on it to do form validation. So server-side 
validation is needed as well to allow your full application to 
gracefully degrade in the absence of working javascript on the 
client's side. Coding defensively helps!


It's not a debate.

You can provide progressive enhancement to your form to help your 
users *IF* you want.


You should *always* validate all the information coming from the outside world.

The question of *if* you want to do both is your choice without any 
debate. Those are only choices that you can elect to follow or not.


Cheers,

tedd

--
---
http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Elegance is the goal... Sticky form submit help

2010-09-11 Thread tedd

At 1:09 PM -0400 9/11/10, Jason Pruim wrote:

Hey tedd,

Thanks for the response but for this particular project I'm avoiding
using anything but standard HTML since it will be used almost 
exclusively  by people using screen readers and other assistive 
technology so I'm going a little old school with it to make sure it 
all works for everyone else first.


That goes without saying.

Regardless of *if* your users use screen readers, or not, progressive 
enhancement should be followed.


Cheers,

tedd

--
---
http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php