Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Advanced Search
  • 0

Custom Redirect Based on Form Selection


MelissaHostetter

Question

  • Answers 12
  • Created
  • Last Reply

Top Posters For This Question

Top Posters For This Question

Popular Posts

@creedon Worked like a charm! Thank you so much!

12 answers to this question

Recommended Posts

  • 0

Can you post the URL to the page where the form is? It helps us if we can see with what you are working.

Hello I'm Thomas. I've been getting websites up and running for 26 years. I want to help you at whatever level meets your needs, from as little (if you're a DIYer) to as much as you need.

Link to post
  • 0

Add the following to Settings > Advanced > Code Injection > HEADER.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>

Add the following to Page Settings > Advanced > Page Header Code Injection for the page.

<script>

  $( ( ) => {
  
    const stateUrlRedirectDataFormId = '5f8da955f46d167ee8ce5578';
    
    const stateUrlRedirectFormSelectId =
    
      'select-yui_3_17_2_1_1603057474472_404878-field';
      
    /* change URL Slug or External URL here */
    
    const stateUrlRedirects = {
    
      'Alabama' :         '/alabama',
      'Alaska' :          '/alaska',
      'Arizona' :         '/arizona',
      'Arkansas' :        '/arkansas',
      'California' :      '/california',
      'Colorado' :        '/colorado',
      'Connecticut' :     '/connecticut',
      'Delaware' :        '/delaware',
      'Florida' :         '/florida',
      'Georgia' :         '/georgia',
      'Hawaii' :          '/hawaii',
      'Idaho' :           '/idaho',
      'Illinois' :        '/illinois',
      'Indiana' :         '/indiana',
      'Iowa' :            '/iowa',
      'Kansas' :          '/kansas',
      'Kentucky' :        '/kentucky',
      'Louisiana' :       '/louisiana',
      'Maine' :           '/maine',
      'Maryland' :        '/maryland',
      'Massachusetts' :   '/massachusetts',
      'Michigan' :        '/michigan',
      'Minnesota' :       '/minnesota',
      'Mississippi' :     '/mississippi',
      'Missouri' :        '/missouri',
      'Montana' :         '/montana',
      'Nebraska' :        '/nebraska',
      'Nevada' :          '/nevada',
      'New Hampshire' :   '/new-hampshire',
      'New Jersey' :      '/new-jersey',
      'New Mexico' :      '/new-mexico',
      'New York' :        '/new-york',
      'North Carolina' :  '/north-carolina',
      'North Dakota' :    '/north-dakota',
      'Ohio' :            '/ohio',
      'Oklahoma' :        '/oklahoma',
      'Oregon' :          '/oregon',
      'Pennsylvania' :    '/pennsylvania',
      'Rhode Island' :    '/rhode-island',
      'South Carolina' :  '/south-carolina',
      'South Dakota' :    '/south-dakota',
      'Tennessee' :       '/tennessee',
      'Texas' :           '/texas',
      'Utah' :            '/utah',
      'Vermont' :         '/vermont',
      'Virginia' :        '/virginia',
      'Washington' :      '/washington',
      'West Virginia' :   '/west-virginia',
      'Wisconsin' :       '/wisconsin',
      'Wyoming' :         '/wyoming'
      
      }
      
    // don't change anything beyond this point
    
    /*
    
    get the state url redirect form and initialize url redirect with first
    state URL
    
    */
    
    let $stateUrlRedirectForm = $( '[data-form-id="' +
    
      stateUrlRedirectDataFormId + '"]' )
      
        .attr ( 'data-success-redirect', stateUrlRedirects [ 'Alabama' ] );
        
    $( '#' + stateUrlRedirectFormSelectId )
    
      .change ( function ( ) {
      
        $stateUrlRedirectForm
        
          .attr ( 'data-success-redirect', stateUrlRedirects [ $( this ).val ( )
          
            ] );
        } );
    } );
    
  </script>

Let us know how it goes.

Hello I'm Thomas. I've been getting websites up and running for 26 years. I want to help you at whatever level meets your needs, from as little (if you're a DIYer) to as much as you need.

Link to post
  • 0

@creedon Thank so much, you rock. I tried inserting the code as directed and still not seeing results. 

Perhaps there is a simpler solution. There are only 3 states that need to be re-directed to a different page so right I have the form "submit" set to redirect to the URL that applies to all states except CA, TX and IL , this is what that code looks like from SquareSpace: 

 <form data-form-id="5f8da955f46d167ee8ce5578" data-success-redirect="https://shoptreatyoak.com/" autocomplete="on" method="POST" action="https://hawk-sprout-ry8m.squarespace.com" onsubmit="return (function (form) {

  Y.use('squarespace-form-submit', 'node', function usingFormSubmit(Y) {

    (new Y.Squarespace.FormSubmit(form)).submit({

      formId: '5f8da955f46d167ee8ce5578',

      collectionId: '5f8da8994339540352e6f741',

      objectName: 'page-5f8da8994339540352e6f741'

    });

  });

  return false;

})(this);" id="yui_3_17_2_1_1603213358956_601">

 

 

Is there something that can be done to offer an alternate redirect when  

<option value="California">California</option>

 <option value="Illinois">Illinois</option>

  <option value="Texas">Texas</option>

 

Link to post
  • 0

Sure my code can be modified but the core behaviour will still be the same. It needs to replace the redirect url for CA, IL and TX. We don't want to mess with the SS code it actually handles the form submit process.

My code just substitutes the redirect URL. We need to figure out why my code isn't running.

Is the code still there or did you take it out? I'm not seeing it on the page.

Hello I'm Thomas. I've been getting websites up and running for 26 years. I want to help you at whatever level meets your needs, from as little (if you're a DIYer) to as much as you need.

Link to post
  • 0

Updated code that is closer to what I think @MelissaHostetter wants.

<script>

  $( ( ) => {
  
    const stateUrlRedirect = {
    
      dataFormId : '5f8da955f46d167ee8ce5578',
      formSelectId : 'select-yui_3_17_2_1_1603057474472_404878-field',
      states : [ 'California', 'Illinois', 'Texas' ],
      redirectUrl : 'https://www.squarespace.com/'
      
      };
      
    // don't change anything beyond this point
    
    let $stateUrlRedirectForm = $( '[data-form-id="' +
    
      stateUrlRedirect.dataFormId + '"]' );
      
    stateUrlRedirect.originalRedirectUrl = $stateUrlRedirectForm.attr (
    
      'data-success-redirect' );
      
    $( '#' + stateUrlRedirect.formSelectId )
    
      .change ( function ( ) {
      
        let url = stateUrlRedirect.states.includes ( $( this ).val ( ) ) ?
        
          stateUrlRedirect.redirectUrl : stateUrlRedirect.originalRedirectUrl;
          
        $stateUrlRedirectForm.attr ( 'data-success-redirect', url );
        
        } );
    } );
    
  </script>

 

Hello I'm Thomas. I've been getting websites up and running for 26 years. I want to help you at whatever level meets your needs, from as little (if you're a DIYer) to as much as you need.

Link to post
  • 0
1 hour ago, EENewHampshire said:

I am looking to do a very similar thing and was wondering if you could help me out as well.

I and others would be happy to take a look at your issue.

I suggest starting a new thread on the forum, describe what you need and then we can take a look. Be sure to post the URL for the page with your form.

If your site is not public please set up a site-wide password, if you've not already done so. Also post the password.

Hello I'm Thomas. I've been getting websites up and running for 26 years. I want to help you at whatever level meets your needs, from as little (if you're a DIYer) to as much as you need.

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment


×
×
  • Create New...