HighDots Forums  

Re: Getting values into a form: PHP (Server) or Javascript (Client)Side?

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Getting values into a form: PHP (Server) or Javascript (Client)Side? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
J.O. Aho
 
Posts: n/a

Default Re: Getting values into a form: PHP (Server) or Javascript (Client)Side? - 03-03-2006 , 11:05 AM






Good Man wrote:

Quote:
I am on the 'edit' screen of a web form, and I have a bunch of variables
coming from a database that need to be placed into the form. In the
past, I have been using PHP to pre-populate each field, something like

input type="text" id="firstName" value="<?= $first_name ?>" /

But, since my "add" and "edit" screens are virtually the same, I'm
thinking about using PHP to dynamically create "onload" javascript
events that use a custom function... essentially something along the
lines of

onload = "setForm('firstName','<?= $first_name ?>');"

So, option one hard-codes the form-field value directly into the HTML.
Option two uses javascript to populate the fields.
Hardcode would be to use

<input type="text" id="firstName" value="John" />

directly in the page.


Quote:
This is a specific web application targeted to a finite audience who
will be using javascript-enabled browsers. At this point, I'm thinking
of going with the second option(javascript based), because I won't have
to scroll down to each form-field tag in my document and add the "value"
parameter (read: time saver).... the only drawback I can see is CPU Power
I guess... Or is that a ridiculous concern? What about using Javascript
to populate 100 fields?
You just moved the process time from the server to the client, what can slow
things down is the size of the javascript, the more the more lagish the page
will be.


Quote:
Most importantly, do I need to worry about client-side interruptions that
can prevent the form from being populated (other than turning javascript
off)?
I don't really see the point in using javascript at all in this case, you get
a more difficulty to figure out bugs.

if you use

<input type="text" id="firstName" value="<?= $first_name ?>" />
<input type="text" id="secondtName" value="<?= $second_name ?>" />
<input type="text" id="lastName" value="<?= $last_name ?>" />

You either have a value set of the variables $first_name, $second_name,
$last_name and so on (I know, I added the two last ones).

If a value isn't set, then the box will be empty and it's just to add the
values manually.


//Aho


Reply With Quote
  #2  
Old   
Good Man
 
Posts: n/a

Default Re: Getting values into a form: PHP (Server) or Javascript (Client) Side? - 03-03-2006 , 11:50 AM






"d" <d (AT) example (DOT) com> wrote in
news:zm_Nf.29523$wl.21033 (AT) text (DOT) news.blueyonder.co.uk:

Quote:
The only point would be saving me programming time. On a page with
50+ form-fields, I can save lot of time by writing a PHP loop that
writes an HTML call to a javscript function instead of going to each
form field and entering the value="<?= $first_name ?>" stuff...

May I suggest learning templates? They exist for just this sort of
thing. They take the edge off writing repetative pages. Either write
your own templating toolkit (better) or get one off the shelf.
is this where the SMARTY thing comes into play? I've heard of it for a
long time but have never used it, as I generally prefer to code my
applications personally...

i think i'm still looking for a good reason NOT to use javascript to
populate my form fields, since I know that my audience is finite and
restricted to javascript-enabled browsers.... especially when my form has
50+ fields. it's too late to look at templating (ie: learning something
new) at this point in the project, but i will do it in the future.


thanks


Reply With Quote
  #3  
Old   
d
 
Posts: n/a

Default Re: Getting values into a form: PHP (Server) or Javascript (Client) Side? - 03-03-2006 , 11:56 AM



"Good Man" <heyho (AT) letsgo (DOT) com> wrote

Quote:
"d" <d (AT) example (DOT) com> wrote in
news:zm_Nf.29523$wl.21033 (AT) text (DOT) news.blueyonder.co.uk:

The only point would be saving me programming time. On a page with
50+ form-fields, I can save lot of time by writing a PHP loop that
writes an HTML call to a javscript function instead of going to each
form field and entering the value="<?= $first_name ?>" stuff...

May I suggest learning templates? They exist for just this sort of
thing. They take the edge off writing repetative pages. Either write
your own templating toolkit (better) or get one off the shelf.

is this where the SMARTY thing comes into play? I've heard of it for a
long time but have never used it, as I generally prefer to code my
applications personally...
then write your own template toolkit It's not as hard as you'd imagine.
Mine is well under 200 lines, and that's got a lot of comments and
white-space. I prefer to write my own code as well

Quote:
i think i'm still looking for a good reason NOT to use javascript to
populate my form fields, since I know that my audience is finite and
restricted to javascript-enabled browsers.... especially when my form has
50+ fields. it's too late to look at templating (ie: learning something
new) at this point in the project, but i will do it in the future.
Of course - if you think it'll work, and if you're happy that your audience
can run it, then go for it just be aware that any errors in javascript
on the page will most likely result in the fields not being populated, and a
wee bit more processing power is required for each page (but nothing
drastic - barely noticeable).

use the javascript, and learn templating when you get a chance

Quote:
thanks
cheers!

dave




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.