Alternative to form generation?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Alternative to form generation?

Post by Eran »

usability and accessibility will be sorely limited if all feedback is done through js and javascript is disabled for whatever reason.
I usually have PHP generated error messages to back up javascript messages. The PHP generated messages are usually a simple unordered list with some minor styling.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to form generation?

Post by Christopher »

pytrin wrote:I usually have PHP generated error messages to back up javascript messages. The PHP generated messages are usually a simple unordered list with some minor styling.
Have you thought about using the unordered list as also being the data source for Javascript. So doing something like this:

Code: Select all

<form ... id="form1">
<div id="form1_error">
  <!--
  UL would be generated with PHP. 
  Javascript would set this UL to not display. If not Javascript then UL/LI styled to show all error messages here. 
  Javascript would then use the LIs as the source of the error message data. 
  -->
  <ul id="form1_error_data">
     <li id="form1_error_data1">Foo required. </li>
     <li id="form1_error_data2">Bar required. </li>
  </ul>
</div>
 
<div>Foo</div>
<div><input type="text" name="foo" id="form1_value1"/></div>
<div id="form1_error1"><!-- error message from above put here by javascript --></div>
 
<div>Bar</div>
<div><input type="text" name="bar" id="form1_value2"/></div>
<div id="form1_error2"><!-- error message from above put here by javascript --></div>
 
</form>
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Alternative to form generation?

Post by Eran »

How would that work? normally you wouldn't display errors right off the bat. And then how'd they be accessible to Javascript?
You can't tell from PHP whether javascript is available or not, so you can't hide/show them accordingly
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to form generation?

Post by Christopher »

There wouldn't be and error text in the list to start with. It would just be:

Code: Select all

<form ... id="form1">
<div id="form1_error">
  <ul id="form1_error_data">
     <li id="form1_error_data1"></li>
     <li id="form1_error_data2"></li>
  </ul>
</div>
The Javascript could easily loop through the list and get the text. It would know which field the error is for by the ID. It would probably be better to use the field name rather than numbers like I did, so "form1_foo_error" and form1_bar_error". Just split it to figure out the form and field.

The PHP template would be:

Code: Select all

<form ... id="form1">
<div id="form1_error">
  <?php echo $error_list; ?>
</div>
It would be nice to do something similar with the form values, so you just put them in one place and the Javascript form handler would fill in the form for you. I guess you could just put JSON into a script next to the error messages. So:

Code: Select all

<form ... id="form1">
<div id="form1_error">
  <?php echo $error_list; ?>
  <script> <!--
     $form1 = <?php echo $form_json; ?>
  --></script>
</div>
(#10850)
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: Alternative to form generation?

Post by inghamn »

Normally I'm very fond of including information as markup and having Javascript make it snazzier. However, for the purposes of client-side validatation, I'm not quite sure it makes sense to go through that extra effort. The reason is that without Javascript, you don't get client side validation, and thus would not need to the error messages at all.

I think that, for the purposes of having error messages available to displayed using Javascript, there's no reason to mark them up as HTML. Rather just include them as a javascript array or something.

Error messages that are reported during server-side validation would just be directly inserted into the output where necessary.
Post Reply