This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Need to get form data after form is submitted $_POST.


  1. Steve1951
    Member

    Just purchased MachForm and have created my first form. I have integrated the form into a page using the advanced form code and the form is completing properly.

    I need to pass some data from the form to the program specified in the redirect URL. Normally I would retrieve the data using the $_POST function ($_POST['element_2_1']) but the $_POST data doesn't seem to be available to the redirected program.

    What is the proper method to pass form data to the redirected program?

    Thanks,
    Steve

    Posted 13 years ago #
  2. yuniar

    If the redirected page is within the same domain, then the easiest way to get the form data is by saving the $_POST data into a $_SESSION variable.

    So, right above the form code, you could do something like this:

    $_SESSION['post_data'] = $_POST;

    You can then access this session variable easily from your other pages.


    MachForm Founder

    Posted 13 years ago #
  3. Steve1951
    Member

    Are you saying to place the above referenced $_SESSION assignment just above the "function process_form($input){" statement in file /includes/post-functions.php?

    If not there, where?

    Thanks,
    Steve

    Posted 13 years ago #
  4. yuniar

    It should be right above your advanced form code.
    So let say you are having this advanced form code:

    <?php
    	require("/mysite/machform-test/machform.php");
    	$mf_param['form_id'] = 14;
    	$mf_param['base_path'] = 'http://localhost/machform-test/';
    	display_machform($mf_param);
    ?>

    You can modify it to be like this:

    <?php
            $_SESSION['post_data'] = $_POST;
    	require("/mysite/machform-test/machform.php");
    	$mf_param['form_id'] = 14;
    	$mf_param['base_path'] = 'http://localhost/machform-test/';
    	display_machform($mf_param);
    ?>

    Also, to ensure that the session variable is working properly, insert the code below into the first line of the page where you embed your form AND the redirected page:

    <?php session_start(); ?>

    MachForm Founder

    Posted 13 years ago #
  5. Steve1951
    Member

    I still need a little assistance here. I'm not sure how to access the data in the redirected page. As a test I did a

    print_r($_SESSION['post_data']);

    and this returned

    Array ( [id] => 4 [review_submit] => Submit )

    How do I access the data in the redirected page?

    As a temporary workaround I have modified /includes/post-functions.php and placed the data I need in SESSION variables and that works fine, but I would prefer not to modify your code.

    Posted 13 years ago #
  6. yuniar

    That's weird. Is that the only data within the variable?
    It should contains lot more data there (all your form fields data).

    Unless there is some problem with the $_POST variable before saving it into the session.

    Try to do this and let me know what's the result:

    <?php
            print_r($_POST);
            $_SESSION['post_data'] = $_POST;
            print_r($_SESSION['post_data']);
    	require("/mysite/machform-test/machform.php");
    	$mf_param['form_id'] = 14;
    	$mf_param['base_path'] = 'http://localhost/machform-test/';
    	display_machform($mf_param);
    ?>

    MachForm Founder

    Posted 13 years ago #
  7. Steve1951
    Member

    What is displayed is

    Array ( ) Array ( )

    and that makes sense since the print_r($_POST); command is before the form is displayed and posted. I would expect $_POST to be empty since the form has not yet posted. It seems the $_SESSION['post_data'] = $_POST; line should be after the post data has been submitted.

    Posted 13 years ago #
  8. yuniar

    Sorry if I wasn't clear. Try to submit your form and let me know what the result is.
    The empty result above is make sense if the form not being submitted indeed.


    MachForm Founder

    Posted 13 years ago #
  9. Steve1951
    Member

    Probably more my confusion than your instructions. After clicking "Continue" on the form input page, the data in arrays $_POST and $_SESSION['post_data'] are identical and properly contain the data that was entered on the form.

    The print_r data is again displayed just before the "Review Your Entry" screen is displayed. This time the print_r data is

    This is print_r for POST
    Array ( )
    This is print_r for SESSION
    Array ( )

    and of course when the redirected URL script (on the same server) is executed, the $_SESSION['post_data'] is empty.

    Posted 13 years ago #
  10. yuniar

    Ah... I see. I was assuming that you didn't use the "form review" option for your form. My mistake.

    The above code should work if you don't have form review enabled.

    If you need to have form review enabled, then we need to modify the post-functions.php file to store the form variables into session there.

    It should be similar as this:
    http://www.appnitro.com/forums/topic/how-to-limit-access-to-redirect-page-after-submission?replies=19#post-3176

    Is that okay for you?


    MachForm Founder

    Posted 13 years ago #
  11. Steve1951
    Member

    Thanks - That should work. I'll give it a try.

    Posted 13 years ago #
  12. Steve1951
    Member

    Everything worked great with one exception.

    As listed in the referenced post, I added the following lines in post-functions.php

    $_SESSION['is_valid_user'] = true;
    $_SESSION['form_data'] = $table_data;

    All values were returned as expected with one exception. Element_8 is a phone number. What I expected in $_SESSION['form_data']['element_8_*'] for phone number 123-456-7890 was

    [element_8_1] => 123
    [element_8_2] => 456
    [element_8_3] => 7890

    in the same manner as an address field.

    But what I got was

    [element_8] => 1234567890

    Obviously I can extract the three groups of numbers from the complete phone number in my PHP script, or I can add lines to post-functions.php to assign $_SESSION['form_data']['element_8_*'] from $user_input['element_8_*'] but I wanted to check to see why it functions this way. I prefer to keep everything clean without making exceptions for certain types of fields.

    Posted 13 years ago #
  13. yuniar

    Actually, that is the intended behaviour, since the phone number being saved into the database doesn't include any dashes.

    I suppose you will need to extract it manually.


    MachForm Founder

    Posted 13 years ago #

RSS feed for this topic

Reply