MyBB Community Forums

Full Version: Multi-Step Form Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

I need your help (those who know PHP of course Smile).

I'm trying to create a multi-step form, which uses the MyBB code base.

Here is the partial code, for the beginning step...

if($mybb->input['wizard'] == "begin")
{
	if($mybb->request_method == "post")
	{
		$variable = $mybb->input['input_name'];
		if($variable == "")
		{
			$errors[] = "You did not fill out the first field...";
		}
		if($errors > 0)
		{
			$inline_errors = inline_error($errors);
		}
	}
	
	eval("\$page = \"".$templates->get('wizard_1')."\";");
	output_page($page);	
}
elseif($mybb->input['wizard'] == "step2")
{
	eval("\$page = \"".$templates->get('wizard_2')."\";");
	output_page($page);	
}

And here's my html form, which when submitted (and no errors), should move to the second step...

<form action="./page.php?wizard=step2" method="post">
<input type="text" name="input_name" value="">
<input type="submit" name="submit" value="Next Step">
</form>

But, when I press the "Next Step" button, it just goes right to "step2" without giving me the error that I didn't fill in all the required fields.

How can I make it so if all required input fields are filled in, it moves from the "begin" step to "step2", and if they're not, it gives me the inline_error without moving to the next step?

Thank you.
(2010-12-06, 02:18 AM)Dutchcoffee Wrote: [ -> ]Hi,

I need your help (those who know PHP of course Smile).

I'm trying to create a multi-step form, which uses the MyBB code base.

Here is the partial code, for the beginning step...

if($mybb->input['wizard'] == "begin")
{
	if($mybb->request_method == "post")
	{
		$variable = $mybb->input['input_name'];
		if($variable == "")
		{
			$errors[] = "You did not fill out the first field...";
		}
		if($errors > 0)
		{
			$inline_errors = inline_error($errors);
		}
	}
	
	eval("\$page = \"".$templates->get('wizard_1')."\";");
	output_page($page);	
}
elseif($mybb->input['wizard'] == "step2")
{
	eval("\$page = \"".$templates->get('wizard_2')."\";");
	output_page($page);	
}

And here's my html form, which when submitted (and no errors), should move to the second step...

<form action="./page.php?wizard=step2" method="post">
<input type="text" name="input_name" value="">
<input type="submit" name="submit" value="Next Step">
</form>

But, when I press the "Next Step" button, it just goes right to "step2" without giving me the error that I didn't fill in all the required fields.

How can I make it so if all required input fields are filled in, it moves from the "begin" step to "step2", and if they're not, it gives me the inline_error without moving to the next step?

Thank you.

Heres How I would do it:

if($mybb->input['wizard'] == "step2")
{
	if($mybb->request_method == "post")
	{
		$variable = $mybb->input['input_name'];
		if($variable == "")
		{
			$errors[] = "You did not fill out the first field...";
		}
		if($errors > 0)
		{
			$inline_errors = inline_error($errors);
			$mybb->input['wizard'] = "begin"
		}
		else
		{
			eval("\$page = \"".$templates->get('wizard_2')."\";");
			output_page($page);	
		}
	}
}

if($mybb->input['wizard'] == "begin")
{
	eval("\$page = \"".$templates->get('wizard_1')."\";");
	output_page($page);	
}

**Above code not tested, just theoretical.
(2010-12-06, 07:47 AM)- G33K - Wrote: [ -> ]Heres How I would do it:

if($mybb->input['wizard'] == "step2")
{
	if($mybb->request_method == "post")
	{
		$variable = $mybb->input['input_name'];
		if($variable == "")
		{
			$errors[] = "You did not fill out the first field...";
		}
		if($errors > 0)
		{
			$inline_errors = inline_error($errors);
			$mybb->input['wizard'] = "begin"
		}
		else
		{
			eval("\$page = \"".$templates->get('wizard_2')."\";");
			output_page($page);	
		}
	}
}

if($mybb->input['wizard'] == "begin")
{
	eval("\$page = \"".$templates->get('wizard_1')."\";");
	output_page($page);	
}

**Above code not tested, just theoretical.

That works somewhat well.

In addition, how can I make it so the user cannot manually type in the step number. For example, say they've just started the form, and the url is http://somedomain.com/page.php?wizard=begin, and lets say they skip all the way to step three by typing in the url http://somedomain.com/page.php?wizard=step3. What would be the best way to check to make sure they've completed the other two steps, so they can't view step three yet?

I thought about using cookies, but I don't think that's the best way.

Regards.
No expert here, but this might work for you;

if($_SERVER['HTTP_REFERER'] != "./page.php?step=2") {
   error code & redirect shizzle
}
(2010-12-07, 02:08 AM)Scoutie44 Wrote: [ -> ]No expert here, but this might work for you;

if($_SERVER['HTTP_REFERER'] != "./page.php?step=2") {
   error code & redirect shizzle
}

I don't believe this will work. Thanks for trying to help though! Smile
You can accomplish that by not calling the steps in the GET query string.
Instead of <form action="./page.php?wizard=step2" method="post">

Use:
<form action="./page.php" method="post">

Then in the form include a hidden field with the step:
<input type="hidden" name="wizard" value="step2">

Your function would then change like this:

if($mybb->input['wizard'] == "step2")
{
    if($mybb->request_method == "post")
    {
        $variable = $mybb->input['input_name'];
        if($variable == "")
        {
            $errors[] = "You did not fill out the first field...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
            $mybb->input['wizard'] = "begin"
        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_2')."\";");
            output_page($page);    
        }
    }
    else
    {
           // They didn't come from post, send them back to begin
            $mybb->input['wizard'] = "begin"
    }
}

if(!$mybb->input['wizard'] || $mybb->input['wizard'] == "begin")
{
    eval("\$page = \"".$templates->get('wizard_1')."\";");
    output_page($page);    
} 


Ok, that works pretty good, except for one thing.

On the second step, if I don't input anything, it goes back to the "begin" step, instead of staying on the current step (wizard_2), and displaying the error message via the inline_error() function.

I'm using this code:

if($mybb->input['wizard'] == "step2")
{
    if($mybb->request_method == "post")
    {
        $variable = $mybb->input['input_name'];
        if($variable == "")
        {
            $errors[] = "You did not fill out the first field...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
            $mybb->input['wizard'] = "begin";
        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_2')."\";");
            output_page($page);    
        }
    }
    else
    {
           // They didn't come from post, send them back to begin
            $mybb->input['wizard'] = "begin";
    }
}

// this is the part I'm talking about in the post...
if($mybb->input['wizard'] == "step3")
{
    if($mybb->request_method == "post")
    {
        $variable = $mybb->input['input_name_2'];
        if(!$variable)
        {
            $errors[] = "No input specified...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
			$mybb->input['wizard'] = "step2";
        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_3')."\";");
            output_page($page);    
        }
    }
    else
    {
        // They didn't come from post, send them back to begin
        $mybb->input['wizard'] = "begin";
    }
}

if(!$mybb->input['wizard'] || $mybb->input['wizard'] == "begin")
{
	$tabactive = " tabbertabdefault";
	
    eval("\$page = \"".$templates->get('wizard_1')."\";");
    output_page($page);    
} 

Regards.
You need to process the steps backward, meaning process step3 first, then step2 and so on.

if($mybb->input['wizard'] == "step3")
{
    if($mybb->request_method == "post")
    {
        $variable = $mybb->input['input_name_2'];
        if(!$variable)
        {
            $errors[] = "No input specified...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
            $mybb->input['wizard'] = "step2";
        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_3')."\";");
            output_page($page);    
        }
    }
    else
    {
        // They didn't come from post, send them back to begin
        $mybb->input['wizard'] = "begin";
    }
}

if($mybb->input['wizard'] == "step2")
{
    if($mybb->request_method == "post")
    {
        $variable = $mybb->input['input_name'];
        if($variable == "")
        {
            $errors[] = "You did not fill out the first field...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
            $mybb->input['wizard'] = "begin";
        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_2')."\";");
            output_page($page);    
        }
    }
    else
    {
           // They didn't come from post, send them back to begin
            $mybb->input['wizard'] = "begin";
    }
}

if(!$mybb->input['wizard'] || $mybb->input['wizard'] == "begin")
{
    $tabactive = " tabbertabdefault";
    
    eval("\$page = \"".$templates->get('wizard_1')."\";");
    output_page($page);    
} 
Okay, I put the steps backwards, however, it's still doing the same thing. It's going back to the "begin" step if you don't specify the input for step 2.
Sorry, overlooked that

Here:

$step3 = 0;
if($mybb->input['wizard'] == "step3")
{
    if($mybb->request_method == "post")
    {
        $variable = $mybb->input['input_name_2'];
        if(!$variable)
        {
            $errors[] = "No input specified...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
            $mybb->input['wizard'] = "step2";
            $step3 = 1;

        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_3')."\";");
            output_page($page);    
        }
    }
    else
    {
        // They didn't come from post, send them back to begin
        $mybb->input['wizard'] = "begin";
    }
}

if($mybb->input['wizard'] == "step2")
{
    if($mybb->request_method == "post")
    {
        if($step3)
       {
               // We came here from step3
               if($errors > 0)
                       {
                           $inline_errors = inline_error($errors);
eval("\$page = \"".$templates->get('wizard_2')."\";");
                           output_page($page);    
                       }
               }
               else
               {

        $variable = $mybb->input['input_name'];
        if($variable == "")
        {
            $errors[] = "You did not fill out the first field...";
        }
        if($errors > 0)
        {
            $inline_errors = inline_error($errors);
            $mybb->input['wizard'] = "begin";
        }
        else
        {
            eval("\$page = \"".$templates->get('wizard_2')."\";");
            output_page($page);    
        }
}
    }
    else
    {
           // They didn't come from post, send them back to begin
            $mybb->input['wizard'] = "begin";
    }
}

if(!$mybb->input['wizard'] || $mybb->input['wizard'] == "begin")
{
    $tabactive = " tabbertabdefault";
    
    eval("\$page = \"".$templates->get('wizard_1')."\";");
    output_page($page);    
} 
Pages: 1 2