0

I have a long form that gets submitted. I need to change this to a multipage/multistep form over several pages.

I'll add a somewhat clean version of it, its custom made so there is no source. DOWNLOAD Line 189 I added a relevant remark

This plugin right now is just a long form on 1 page. I want to split it into multiple pages. So the result would be that a person sees first page with, lets say 10 questions (radio boxes), after answering those, he goes to the next page with another 10 etc, in total I will need 4 pages. Very simple I just have no idea how its done in WP.

<?php

$wprr_strings = array( 'error' => '', );

function wprr_check_input() {

    if ( ! ( isset( $_POST['wpcf_stage'] ) ) ) {
        return false;
    } // Shortcircuit.

    global $wprr_strings;

    $ok = true;


    if ( empty( $_POST['Q1'] ) or empty( $_POST['Q2'] ) or empty( $_POST['Q3'] )

        or empty( $_POST['Q4'] ) or empty( $_POST['Q5'] ) or empty( $_POST['Q6'] ) or empty( $_POST['Q7'] ) or empty( $_POST['Q8'] )

        or empty( $_POST['Q9'] ) or empty( $_POST['Q10'] ) or empty( $_POST['Q11'] ) or empty( $_POST['Q12'] ) or empty( $_POST['Q13'] )

        or empty( $_POST['Q14'] ) or empty( $_POST['Q15'] ) or empty( $_POST['Q16'] ) or empty( $_POST['Q17'] ) or empty( $_POST['Q18'] )

        or empty( $_POST['Q19'] ) or empty( $_POST['Q20'] ) or empty( $_POST['Q21'] ) or empty( $_POST['Q22'] ) or empty( $_POST['Q23'] )

        or empty( $_POST['Q24'] ) or empty( $_POST['Q25'] ) or empty( $_POST['Q26'] ) or empty( $_POST['Q27'] ) or empty( $_POST['Q28'] )

        or empty( $_POST['Q29'] ) or empty( $_POST['Q30'] ) or empty( $_POST['Q31'] ) or empty( $_POST['Q32'] )
    ) {

        $ok     = false;
        $reason = 'empty';

    }


    if ( $ok == true ) {

        return true;

    } else {

        if ( $reason == 'empty' ) {

            $wprr_strings['error'] = '<span style="color: #ff0000;"><b>Ошибка!</b> Заполнены не все поля формы.</span>';

        }

        return false;

    }

}


function wprr_callback( $content ) {

    global $wprr_strings;

    global $charset;

    global $wpdb;


    /* Run the input check. */

    if ( wprr_check_input() ) // If the input check returns true (ie. there has been a submission & input is ok)

    {

        $test_result = array( 'safety' => 0, 'agressiveness' => 0, 'courtesy' => 0, 'rage' => 0, );


        $test_result['safety'] = ( $_POST['Q1'] + $_POST['Q4'] + $_POST['Q11'] + $_POST['Q15'] + $_POST['Q17'] + $_POST['Q25'] +

                $_POST['Q26'] + $_POST['Q29'] ) * 2.5;

        $test_result['agressiveness'] = ( $_POST['Q2'] + $_POST['Q6'] + $_POST['Q8'] + $_POST['Q10'] + $_POST['Q14'] + $_POST['Q27'] +

                $_POST['Q28'] + $_POST['Q31'] ) * 2.5;

        $test_result['courtesy'] = ( $_POST['Q3'] + $_POST['Q5'] + $_POST['Q12'] + $_POST['Q13'] + $_POST['Q21'] + $_POST['Q23'] +

                $_POST['Q24'] + $_POST['Q32'] ) * 2.5;

        $test_result['rage'] = ( $_POST['Q7'] + $_POST['Q9'] + $_POST['Q16'] + $_POST['Q18'] + $_POST['Q19'] + $_POST['Q20'] +

                $_POST['Q22'] + $_POST['Q30'] ) * 2.5;


        // Оценка категорий

        // БЕЗОПАСНОСТЬ

        if ( $test_result['safety'] < 37.5 ) {

            $safe_msg = "С Вами опасно.";

        } elseif ( ( $test_result['safety'] >= 37.5 ) and ( $test_result['safety'] < 62.5 ) ) {

            $safe_msg = "С Вами не очень безопасно.";

        } elseif ( ( $test_result['safety'] >= 62.5 ) and ( $test_result['safety'] < 82.5 ) ) {

            $safe_msg = "Вы безопасный водитель.";

        } else {

            $safe_msg = "С Вами безопасно.";

        }

// More results (does not matter)


        // Добавляем данные в таблицу test_rr

        $sql_ins = 'INSERT INTO test_rr (sex, age, brand, color safe, agres, court, rage) VALUES (' . $_POST['sex'] . ',' . $_POST['age'] .

            ',' . $_POST['brand'] . ',' . $_POST['color'] . ',' . $test_result['safety'] . ',' . $test_result['agressiveness'] . ',' . $test_result['courtesy'] . ',' . $test_result['rage'] . ')';


        //echo $sql_ins;

        $wpdb->query( $sql_ins );


        // Подсчет средних значений

        $avgs = array();


        $avgs = $wpdb->get_row( "SELECT AVG(safe), AVG(agres), AVG(court), AVG(rage) FROM test_rr;", ARRAY_N );


        $success_msg = "BLA BLA"


        $results = '<p class="successmsg">' . $success_msg . '</p>';

        return $results;



    } else // This is the FORM that needs to be split into a few pages

    {


        $form = '<p>bla bla.</p>

<form id="contactform" action="' . get_permalink() . '" method="post">

' . $wprr_strings['error'] . '

    <fieldset>

    <h3>bla bla</h3>

    <br/>

===>>> FORM INPUTS, SELECTS, RADIOS ETC... (few dozen of those) Split the like 10 per page <<<======



    </fieldset>

</form>



';

        return $form;


    }

}

add_shortcode( 'test_rr_form', 'wprr_callback' );
  • Can you state clearly what the actual question is? Without seeing code, and without understanding your original problem it's difficult to make sense of this question – Tom J Nowell Sep 14 '14 at 14:17
  • Edited the original post. – Dima Sep 14 '14 at 14:46
  • Ah, so: "I have a long form, assembled in a PHP variable and outputted all at once. I'd like to split this form up into multiple pages, how do I do that?" – Tom J Nowell Sep 14 '14 at 15:23
  • I've edited your question to make more sense, and reformatted and posted the code inline – Tom J Nowell Sep 14 '14 at 15:31
0

Your question can be done using standard PHP, there is nothing special with WordPress you need to do ( assuming this is a frontend form ).

Lets say you have a form with 6 inputs. You want them to appear on 3 pages. Lets work off of this pseudocode:

if form submitted
    do checks and handle form
else
    display input 1
    display input 2
    display input 3
    display input 4
    display input 5
    display input 6

That's what you have right now.

What you need to do is:

  • Output the inputs that aren't on the current page as type 'hidden' so that they're remembered between pages
  • Maintain a hidden input with a page counter
  • Everytime the user submits the form, increment the counter and display the next page
  • When the counter is higher than the last page, process the form in full

So:

if form not submitted
    this is the first page
    display input 1
    display input 2
    display hidden input 3
    display hidden input 4
    display hidden input 5
    display hidden input 6
    display hidden count with value 0
else
    if counter == 1
        display hidden input 1
        display hiddeninput 2
        display input 3
        display input 4
        display hidden input 5
        display hidden input 6
        display hidden input counter+1
    else if counter == 2
        display hidden input 1
        display hidden input 2
        display hidden input 3
        display hidden input 4
        display input 5
        display input 6
        display hidden input counter+1
    else
        check and process form

Of course some refactoring to remove the duplication is necessary if you have a lot of fields, and a submit button to go to the next page

| improve this answer | |
  • Yes this is exactly what I asked for. Interesting approach with hidden fields )) So what You actually saying is to display the same form and show/hide different fields by testing the counter.. Isn't there just a simple way to divide them to actual pages? Something along the lines of {page1}PAGE1{/page1}, {page2}PAGE2{/page2} etc.. ? – Dima Sep 14 '14 at 18:19
  • That would add complexity, and require you to generate the whole form, then parse and process the form to pick up the opening and closing {page1} etc, then you'd need to parse those tags to extract the number, then fire up an XMLParser to process the resulting code and change the inputs to be hidden on the non-pages. I recommend against it, it's overcomplicating it. If you're looking at reducing the amount of code, defining all your form inputs using data structures such as objects and arrays then generating markup from that would be the way to go. But that's beyond the scope of this question – Tom J Nowell Sep 14 '14 at 18:22
  • The alternative used elsewhere are to store the intermediate values of prev pages in the database, but that's overcomplicating it too here, or storing the previous pages data inside a session cookie, but WordPress doesn't uses sessions for auth – Tom J Nowell Sep 14 '14 at 18:23
  • lol, thanks, I didn't mean to complicate by page1, page2... Rather to make it simpler, just thought that WP would have some function ready for such a thing, seem trivial yet.. I'll go with your first answer than, I appreciate You taking the time help, Thanks! – Dima Sep 14 '14 at 19:16
  • Hello Tom, I only just now edited the plugin using your solution. But, as it turns out, I need those 4 pages to be actual pages, so that search engines, analytics code and such will register them as different pages. So technically I can cook something in PHP in the plugin, but how do I change the URL from page to page? Thanks – Dima Oct 13 '14 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.