1

This is about the functions.php file.

I have a homepage with two links to enter data into either this form (refinance) or that form (purchase). Both forms have identical fields, just one question is different.

This site is for home purchase and refinance loans, so the difference between the two forms is "What is the value of your home?" for the Refinance and "How much are you looking to for a loan?" for the Purchase option. The option values are identical for both, and I just write the transaction type into the database as a way to differentiate between the two types of transactions.

My code works for inserting the data into my database. Upon submission, I just wanted to ask how to redirect to the refinance thank you page (quote-thank-you-refinance) when the transaction comes from the refinance-form/ url, and how to redirect to the thank you page (quote-thank-you-purchase/) for purchase transactions.

The full URL is not shown in the pages listed above, just enough to differentiate between the two forms.

I have this for the redirection:

window.location = "quote-thank-you-purchase/";
and that works fine for the purchase.

How could I create a switch or if/then statement based on which URL the page is currently on to redirect to the proper thank you page depending on which type of transaction?

This is my form submission with redirect (domain has been removed for obvious reasons):

function submit_form() { ob_start(); global $wpdb;

$form_data = $wpdb->insert("wp_h_p", array(
'salesprice' => $_POST['salesprice'],
'loan_amount' => $_POST['loan_amount'],
'income' => $_POST['income'],
'fName'  => $_POST['fName'],
'lName'  => $_POST['lName'],
'property_street'   => $_POST['property_street'],
'property_city' => $_POST['property_city'],
'property_state' => $_POST['property_state'],
'property_zipcode' => $_POST['property_zipcode'],
'primary_phone'  => $_POST['primary_phone'],
'email'   => $_POST['email'],
'ip' => $_SERVER['REMOTE_ADDR'],
'formurl' => $_SERVER['REQUEST_URI'],
'user_message' => $_POST['user_message'],
));
if($form_data) {
?>
<script>
window.location = "https:///quote-thank-you-purchase/";
</script>
<?php
} else {
$message = '<div class="alert alert-error">There is an error adding the new record.  </div>';
}
<?php
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
<?php
return ob_get_clean();
}

add_shortcode('form_submit', 'submit_form');

Thank you in advance.

1
  • 1
    can you add a hidden html field to signify the particular form? Then use that field value to perform the redirect? If the forum submissions are 100% IDENTICAL, then you can't distinguish them on the backend - you need a flag, and a hidden HTML input, or a name/value on the submit buttons would do the trick.. Then you can if/else on that hidden field and redirect as you need. Would that work?
    – Paul G.
    Jun 19 at 14:50
0

using this

header("Location: http://example.com/myOtherPage.php");
die();

this redirects the page to whatever link we put there, if we want to choose depending if is one or another, its possible to use this inside a if statement. I only do not put the code here cause I do not full understand the code above..., but I would do it this way

0

I assume you have 2 different page templates for those 2 different thank you pages (quote-thank-you-refinance and quote-thank-you-purchase). Lets say your page templates are present in your theme's root folder and the names are 'thank-you-refinance.php' and 'thank-you-purchase.php'. Now add the following code in your functions.php file:

function ft_is_thankyou_purchase() {
    if( is_page_template( 'thank-you-purchase.php' ) ) { //replace 'thank-you-purchase.php' with your template file name. If your page template resides within a directory, you should use something like this: 'directory-name/thank-you-purchase.php'.
        return true; //thank-you-purchase.php is in use
    } else {
        return false; //thank-you-purchase.php is not in use
    }
}

Finally edit your submit_form() function's if($form_data) condition like this:

if($form_data) {
    if( ft_is_thankyou_purchase() ) { ?>
        <script>
        window.location = "https:///quote-thank-you-purchase/";
        </script>
<?php } else { ?>
        <script>
        window.location = "https:///quote-thank-you-refinance/";
        </script>
<?php } //end ft_is_thankyou_purchase condition
}

There are couple of articles present on FluentThemes.com about using the page template conditions to determine the page we are on. You can take a look if you need some more details on this with some more example codes.

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.