Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:22:38+00:00 2026-05-25T19:22:38+00:00

I’m learning Javascript (mainly using JQuery) and was wondering if somebody could help me.

  • 0

I’m learning Javascript (mainly using JQuery) and was wondering if somebody could help me.

I’ve written an if/else statement that checks to see if a particular string appears in the URL, then posts a forms content to a particular php script depending on the string.

Q: The code I’m using works, BUT feels clumsy and unnecessarily long! – I figure there MUST be a simpler way to write this!? I’ve had a look around but most answers just seem to provide code without anything to help me understand how it was done..

What I’m trying to achieve is:

  1. A user completes one of 4 signup forms (prereg1.html, prereg2.html, prereg3.html, prereg4.html)
  2. Depending on the URL, the if/else statement sends the form data to a corresponding PHP script (add_prereg1.php, add_prereg2.php etc.) which adds the data to an email list database (hosted by campaign monitor)

note: My php knowledge is pretty poor too – because the campaign monitor lists each have unique IDs, the only way I can get the php to post the form data in the right list is to have 4 separate ‘prereg.php’ scripts. I’m sure there is a way to get the php script to figure out which list to post to based on the variables passed by the javasscript but this is a bit beyond me at this stage! Any pointers welcome..

Anyhow here is the if/else statement:

    if (document.location.href.indexOf('prereg1') > -1) {
           $.ajax({
                type: "POST",
                url: "bin/add_prereg1.php",
                data: 'name=' + name + '&email=' + email
            });
        } 

        else {
                if (document.location.href.indexOf('prereg2') > -1) {
                   $.ajax({
                        type: "POST",
                        url: "bin/add_prereg2.php",
                        data: 'name=' + name + '&email=' + email
                    });
                }

            else {
                if (document.location.href.indexOf('prereg3') > -1) {
                   $.ajax({
                        type: "POST",
                        url: "bin/add_prereg3.php",
                        data: 'name=' + name + '&email=' + email
                    });
                }

            else {
                    if (document.location.href.indexOf('prereg4') > -1) {
                       $.ajax({
                            type: "POST",
                            url: "bin/add_prereg4.php",
                            data: 'name=' + name + '&email=' + email
                        });
                    }
                }                   
            }
        }

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:22:39+00:00Added an answer on May 25, 2026 at 7:22 pm

    Consider this:

    var match = window.location.href.match( /prereg(\d)/ );
    
    if ( match ) {
        $.ajax({
            type: 'POST',
            url: 'bin/add_prereg' + match[1] + '.php',
            data: 'name=' + name + '&email=' + email
        });
    }
    

    Live demo: http://jsfiddle.net/4MWDm/

    You simply search the string for the sequence “prereg” followed by a digit. The parens capture the digit, so you can access it with [1].


    Btw use window.location instead of document.location.

    document.location was originally a read-only property, although Gecko
    browsers allow you to assign to it as well. For cross-browser safety,
    use window.location instead.

    Source: https://developer.mozilla.org/en/DOM/document.location

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a text area in my form which accepts all possible characters from
I am writing an app with both english and french support. The app requests
I'm parsing an XML file, the creators of it stuck in a bunch social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.