NYCPHP Meetup

[nycphp-talk] multi-part form without refresh - point me to a working solution?

Paul Houle paul at devonianfarm.com
Wed Jan 3 18:23:41 EST 2007


Brandy Whine wrote:
> Hi everyone. You have all helped me as a lurker for almost a year, but 
> now my PHP has advanced a bit and I need to ask a specific question if 
> that's ok. I don't know AJAX but I think I need it now.
>
    You can certainly do this with Javascript;  actual AJAX,  involving 
a call to a server,  may or may not be necessary.

    It's easy to change the "display" attribute of a <div> element in 
Javascript to show or hide the element:

http://support.internetconnection.net/CODE_LIBRARY/Javascript_Show_Hide.shtml

    The idea is that you put <div> elements containing all the form 
elements that could ever be displayed on the page:  use onChange 
handlers on controlling form form elements to  "morph" the form,  say,  
somebody chooses an option from a dropdown.

    Libraries like prototype,  scriptaculous,  moo and such smooth over 
differences between broswers,   make this kind of code more 
maintainable,  and can also give you really cool effects (parts of the 
form can slide or fade in.)

    AJAX can enter if you'd like to pull information in from the web in 
the process of updating the form.  For instance,  a user might a state 
from a list of US states,  and then the page uses AJAX to populate a 
list of counties from that state.  You can accomplish the same thing,  
in this case,  by including a big array with lots of states and counties 
in the page -- it saves bandwidth to send just the counties that are needed.

---

    It's old-school,  but you can get accomplish the same aims without 
Javascript.  The idea here is to make a "wizard" interface that has more 
than one form...  Let's call them form1, form2,  and form3:

form1: asks some really basic questions that help determine what to 
display on form2
form2: asks more questions to determine what's on form3
form3: collects the last information and "submits" the form for real.

    You can pass answers from form1 into form2 by using hidden form 
variables...  Make your life easy and write a function like

function hide_variable($name,$value) {
    print "<input type=hidden name=$name 
value=".htmlspecialchars($value).">\n";
}

    I still think that most people find this kind of interface to be the 
easiest to use,  when it's designed right.







More information about the talk mailing list