JQuery Dialog box- how can I make a multipage form?

  • Thread starter Thread starter Bes
  • Start date Start date

Bes

Bes

Soldato
Joined
18 Oct 2002
Posts
7,318
Location
Melbourne
Hi

I have a JQuery dialog box.

I want to make a multi- page form where the user can click "next", "next", etc but always stay inside the same little jquery dialog. I also need to pass the data the user has entered back to PHP... I can't figure it out....How can I do this?

Thanks
 
The simplest way to do this would be to insert all the markup for your multi-stage form into the $.dialog() box, and use CSS to hide the different steps.

Every time the user clicks 'next', simply hide the previous step and show the next one. For example, you markup would be as so:

Code:
<form action="/" method="post" id="multi-form">
 <fieldset id="step-1">
  <label>Step 1</label>
  <some form fields... />
  <a href="#step-2" class="next-link">Next</a>
 <fieldset>

 <fieldset id="step-2" class="hidden">
  <label>Step 2</label>
  <some form fields... />
  <a href="#step-3" class="next-link">Next</a>
 <fieldset>

 <fieldset id="step-3" class="hidden">
  <label>Step 3</label>
  <some form fields... />
  <input type="submit" value="Submit" />
 <fieldset>
</form>

Class "hidden" having a rule of "display: none;" or such. And your js something like this:

Code:
$('#multi-form .next-link').bind('click', function(e) {
  e.preventDefault();
  var parentFieldset = $(this).parents('fieldset);
  parentFieldset.addClass('hidden').next('fieldset').removeClass('hidden');
}

Hopefully that's simple enough to understand what's going on.

Doing it this way means your multi-stage form is accessible without JS, and you don't need to worry about storing form data between each step - the form will work like a traditional input form.

If you do want to pass data to PHP during the form input process, for validation perhaps, you'll need to bind $.ajax() events to your next buttons i.e. on the click event of the next button, an $.ajax() request would fire that pulled the current step's form data out and passed it to PHP. PHP would then need to process this and send an appropriate response back. On the success/complete callback function of $.ajax(), you can decide to show/hide the next stage of the form or show validation errors.
 
Ah that looks really neat actually- I never thought of using CS!

Javascript was cauing me issues because of the AJAX elements possibly....

Anyway thanks, I'll try it
 
Back
Top Bottom