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

Send the form data to another php program via the "Redirected URL" feature


  1. orachat
    Member

    The following code was written for the following conditions:
    1. MachForm version 2.4
    2. The "Redirected URL" already has some parameters. For example, http://xx.xx.xx.xx/process_form_data.php?form_name=template&itemcode=123432

    To send the form data to a php program, replace the line

    $commit_result['form_redirect'] = $form_redirect;

    in includes/post-functions.php with the following code (probably should be a function instead):

    $url_data = '';
    $entry_details = get_entry_details($form_id,$new_record_id,'');
    foreach ($entry_details as $data) {
        if ($data['element_type'] == 'textarea') {
            $data['value'] = nl2br($data['value']); //add br before newline
            $data['value'] = str_replace("\r\n", '', $data['value']); //remove newline because it breaks php
        }
        elseif ($data['element_type'] == 'file') {
            $data['value'] = strip_tags($data['value'], '<a>'); //remove the image tag, but keep href
        }
        elseif ($data['element_type'] == 'checkbox') {
            $data['value'] = strip_tags($data['value'], '<br>'); //remove the image tag, but keep <br>
            $data['value'] = str_replace('', ',', $data['value']); //replace <br> with ','
            $data['value'] = preg_replace ('/^ /', '', $data['value']); //remove the beginning space
            $data['value'] = preg_replace ('/,$/', '', $data['value']); //remove the trailing ','
        }
        elseif ($data['element_type'] == 'phone') {
            $data['value'] = str_replace(' ', '', $data['value']); //remove spaces
        }
        elseif ($data['element_type'] == 'address') {
            $data['value'] = str_replace('', ', ', $data['value']); //prefer ',' over <br>
        }               
    
        //target php program fails to get some data values correctly due to existence of non-alphanumeric characters
        $data['value'] = urlencode($data['value']);
    
        //replace '&nbsp;' with nothing, because empty fields have '&nbsp;' after using urlencode() previously
        $data['value'] = str_replace('%26nbsp%3B', '', $data['value']);
    
        $url_data .= $data['label'] . '=' . '"' . $data['value'] . '"' . '&';
    
        //fwrite ($mlog, $url_data . "\n"); //for testing only
    }               
    
    $commit_result['form_redirect'] = $form_redirect . '&' . $url_data;
    Posted 13 years ago #
  2. yuniar

    Thank you for sharing this code!


    MachForm Founder

    Posted 13 years ago #
  3. startagl
    Member

    I have added the following code to your procedure.

    $mlog = fopen("test.txt","w");
    fwrite ($mlog, $url_data . "\n"); //for testing only
    fclose($mlog);

    But I can not find the test.txt file anywhere, so
    I created the file and set its permissions to 666. But the file never gets updated.

    Does this file get written only if there is a redirected url, Or does it create the file for every form that is completed?

    Thanks.

    Posted 13 years ago #
  4. startagl
    Member

    I am not sure why but it seems like this code is not getting executed at all. Just for testing I have put in sleep(10000); and it still just jumps to the redirect page without any pause. what the?

    Posted 13 years ago #
  5. yuniar

    I think the code above assume that you need to enable "form review" for your form. So try to enable form review.


    MachForm Founder

    Posted 13 years ago #
  6. orachat
    Member

    I've always tested with "form review" on and never thought about it, but I just tested with it off and it does not work. I didn't realize this. I didn't review the entire code to see where I should put the code because I was referred to post-functions.php. I guess it's a question for the developers as to where this code should go if "form review" is off.

    Posted 13 years ago #
  7. orachat
    Member

    To answer the question "Does this file get written only if there is a redirected url,",
    the answer is "yes".

    Posted 13 years ago #
  8. startagl
    Member

    form review, did the trick. Thanks.

    Also your code seems to create a malformed post. The data returned starts with a & instead of a ?, did you do that on purpose?

    I also added to following code in case it helps anyone.

    //append the newly created record id to the $url_data stream
    $url_data .= 'RecNo=' . $new_record_id; //unique number for this form submission

    //saves the $url_data to a file so you can inspect it
    file_put_contents("/(path)/POST_FUNCTIONS.txt",$url_data,FILE_APPEND);

    Posted 13 years ago #
  9. orachat
    Member

    I start with & instead of ? in "$form_redirect . '&' . $url_data" because I'm assuming that $form_redirect will already have some parameters (see #2 in the first post), since this is how I use it. I suppose this part can be improved by checking to see if '?' exists in $form_redirect already, and if not, then use '?' instead of '&'.

    Posted 13 years ago #

RSS feed for this topic

Reply