简体   繁体   中英

How to use PHP header redirect after sending REST data?

First of all I'd like to thank all the people participating in Stackoverflow, I always find solutions on this website but never got the opportunity to help someone else so far.

I'm currently working on a form, which submits data via REST API to our CRM and everything works as it should except for the redirect.

The form, which is designed by adapting the following code, is working correctly: https://codepen.io/SethCalkins/pen/JoXymK :

<form id="theForm" class="simform" autocomplete="off" method="post" action="">
                    <div class="simform-inner">
                        <ol class="questions">
                            <li>
                                <span class="genericform"><label for="first_name">Name:</label></span>
                                <input id="first_name" name="first_name" type="text" pattern="{3,}" required autofocus="true" autocomplete="off"/>
                            </li>
                            <li>
                                <span><label for="last_name">Surname:</label></span>
                                <input id="last_name" name="last_name" type="text" pattern="{3,}" required/>
                            </li>
                            <li>
                                <span><label for="phone">Phone:</label></span>
                                <input id="phone" name="phone" type="tel" data-validate="phone" inputmode="numeric" required/>
                            </li>
                            <li>
                                <span><label for="email">Email:</label></span>
                                <input id="email" name="email" type="email" data-validate="email" required/>
                            </li>

                        </ol><!-- /questions -->

                        <div class="controls">
                            <button class="next"><i class="svg-inline--fa fa-arrow-right></i></button>
                            <div class="progress"></div>
                            <span class="number">
                                <span class="number-current"></span>
                                <span class="number-total"></span>
                            </span>
                            <span class="error-message"></span> 
                        </div><!-- / controls -->
                    </div><!-- /simform-inner -->
                    <span class="final-message generic-popup-form-fade"><button class="popup-form-button-icon" type="submit" value="send">Submit</button></span>
                </form>  
                </div>    
        <script>
// On form submit event

            var theForm = document.getElementById( 'theForm' );

            new stepsForm( theForm, {
                onSubmit : function( form ) {
                    // hide form
                    classie.addClass( theForm.querySelector( '.simform-inner' ), 'hide' );

                    var messageEl = theForm.querySelector( '.final-message' );
                    $(".submit").css({"display":"inline"});
                    $(".uk-heading-medium").css({"text-align":"center"});
                    classie.addClass( messageEl, 'show' );
                }
            } );
        </script>

The PHP used to send to write in the log file and send the lead is the following. It writes the log correctly and the lead arrives to the CRM aswel, it is just that the redirect isn't working:

<?
/**
 * Write data to log file.
 *
 * @param mixed $data
 * @param string $title
 *
 * @return bool
 */
function writeToLog($data, $title = '') {
 $log = "\n------------------------\n";
 $log .= date("Y.m.d G:i:s") . "\n";
 $log .= (strlen($title) > 0 ? $title : 'DEBUG') . "\n";
 $log .= print_r($data, 1);
 $log .= "\n------------------------\n";
 file_put_contents(getcwd() . '/logfile.log', $log, FILE_APPEND);
 return true;
}

$defaults = array('first_name' => '', 'last_name' => '', 'phone' => '', 'email' => '');

if (array_key_exists('saved', $_REQUEST)) {
 $defaults = $_REQUEST;
 writeToLog($_REQUEST, 'webform');

 $queryUrl = 'https://queryurl/rest/';
 $queryData = http_build_query(array(
 'fields' => array(
 "TITLE" => $_REQUEST['first_name'].' '.$_REQUEST['last_name'],
 "NAME" => $_REQUEST['first_name'],
 "LAST_NAME" => $_REQUEST['last_name'],
 "STATUS_ID" => "NEW",
 "PHONE" => array(array("VALUE" => $_REQUEST['phone'], "VALUE_TYPE" => "WORK" )),
 "EMAIL" => array(array("VALUE" => $_REQUEST['email'], "VALUE_TYPE" => "WORK" )),
 ),
 'params' => array("REGISTER_SONET_EVENT" => "Y")
 ));

 $curl = curl_init();
 curl_setopt_array($curl, array(
 CURLOPT_SSL_VERIFYPEER => 0,
 CURLOPT_POST => 1,
 CURLOPT_HEADER => 0,
 CURLOPT_RETURNTRANSFER => 1,
 CURLOPT_URL => $queryUrl,
 CURLOPT_POSTFIELDS => $queryData,
 ));

 $result = curl_exec($curl);
 curl_close($curl);

 $result = json_decode($result, 1);
 writeToLog($result, 'webform result');

if ($result) {
 header( 'Location: https://www.REDIRECTURL.com/thank-you/');
}


 if (array_key_exists('error', $result)) echo "Error al enviar la solicitud: ".$result['error_description']."<br/>";
}

?>

I've tried just adding the header include like:

 header( 'Location: https://www.REDIRECTURL.com/thank-you/');

I've looked for whitespace after "?>" and the code doesn't seem to have any errors.

Not sure what I'm missing..

For anyone wondering, I've solved it by moving the header('') redirect and all other php code to the header instead of on the bottom of the page (footer).

Works as it should now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM