如何将表单数据发布到另一个页面?

时间:2015-03-04 08:13:14

标签: php wordpress forms curl gravity-forms-plugin

重要编辑:我现在已将我的问题编辑为更具体。

我想要做的是,当用户提交我的重力形式时,数据被发送到另一个wordpress平台,其中也是具有相同字段的表单。应自动输入和提交数据,并将用户重定向到感谢页面。我已经阅读了有关cURL和GF Web API的内容,但我还没有让它们工作。我已经制作了gform_after_submission HOOK,我有这个代码

add_action("gform_after_submission_7", "after_submission");
function after_submission($entry, $form){
//All the code that processes your form goes here 
$curl_handle = curl_init();
$body = array(
    'info' => rgar ( $entry, '5' ), 
    'name' => rgar ( $entry, '25' ), 
    'email' => rgar ( $entry, '2' ),
    'submit' => 'true',
    );
$data ='"info='.$body['info'].'&nimi='.$body["nimi"].'&email='.$body["email"].'"'; 

$url = "http://www.monica.ee/reisile/dev/kontakt";

curl_setopt ($curl_handle, CURLOPT_URL,$url);

curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data);

curl_setopt( $curl_handle, CURLOPT_SSL_VERIFYPEER, false );  //so we can post to https  

$result = curl_exec ($curl_handle);

curl_close ($curl_handle); 
//then once you have processed the form you can redirect to a success page
}

它应该POST表单数据的页面有这些行

jQuery(document).ready(function() {
        if ($("#input_9_1").length) {
            console.log("<?php echo $_GET['nimi']; ?>");
        $('#input_9_1').val("<?php echo $_POST['info']; ?>");
        $('#input_9_2').val("<?php echo $_GET['nimi']; ?>");
        $('#input_9_3').val("<?php echo $_POST['email']; ?>");
        //$('#input_9_4').val(<?php echo $_POST["mobiil"]; ?>);
        }
        <?php
        if (isset($_POST['submit'])) {
            echo "$('#gform_submit_button_9').click();";
        }
        ?>
});

但是现在当用户提交我的表单时,他会被自动重定向到感谢页面,它似乎没有POST并提交另一个表单,我试图发送我的数据。

有人可以帮我解决这个问题吗,我一直试图解决这个问题,现在仍然无法解决。我可能会以错误的方式做到这一点,但也许有人可以帮我解决这个问题。

提前致谢!

1 个答案:

答案 0 :(得分:1)

您可以在包含表单的页面顶部包含所有验证和表单处理代码。然后,如果验证和处理成功,则将用户重定向到成功页面。所以你的表单可能看起来像这样......

<?php

//Check if the form has been submitted
if( isset( $_POST['submit'] ) ) {
    //Validate form
    $validation = validate_form($_POST);

    //If validation is successful process Form
    if( $validation['success'] == true ) {
        process_form($_POST);
    }

    //If validation has failed then output error message
    else {
        echo '<p class="error" id="validation-failed">' . $validation['error_message'] . '</p>';
    }
}

?>

<form id="kontaktvorm" method="post" action="">
    <div class="tekstikast">
        <textarea class="kontaktivorm medium" tabindex="1" rows="10" cols="50" tabindex="1" name="info">Sinu sõnum ...</textarea>
    </div>
    <div class="nimekast">
        <input class="kontaktivorm poolikkast" type="text" tabindex="2" value="Nimi" name="nimi" />
    </div>
    <div class="epostikast">
        <input class="kontaktivorm poolikkast" tabindex="3" type="text" value="E-post" name="email" />
    </div>
    <div class="nupukast">
        <input type="submit" class="gform_image_button" tabindex="5" name="submit" />
    </div>
</form>

然后在您的函数文件中,您可以包含验证和处理表单的两个函数...

/**
 * validates your form
 * @param  array $data the post data from the page
 * @return array
 */
function validate_form( $data ) {
    //Build our output array
    $output = array(
        'success' => true,
        'error_message' => null
    );

    //validate your fields... for example
    if( !$data['email'] || !is_email($data['email']) ) {
        $output['success'] = false;
        $output['error_message'] = 'Please enter a valid email address';
    }

    return $output;

} 

/**
 * processes your form data
 * @param  array $data the post data from the page
 */
function process_form($data) {
    //All the code that processes your form goes here 
    //then once you have processed the form you can redirect to a success page
    wp_redirect( home_url() . '/success' );
}

希望有所帮助