Javascript beginner question - how to avoid long/ugly if/else statements?
<p>I'm learning Javascript (mainly using JQuery) and was wondering if somebody could help me..</p>

<p>I've written an if/else statement that checks to see if a particular string appears in the URL  then posts a forms contents to a particular php script depending on the string.</p>

<p><strong>Q: The code I'm using works, BUT feels clumsy and unnecessarily long! - I figure there MUST a simpler way to write this!? I've had a look around but most answers just seems to provide code without anything to help me to understand how it was done..</strong></p>

<p>What I'm trying to achieve is:</p>

<ol>
<li>A user completes one of 4 signup forms (prereg1.html, prereg2.html, prereg3.html, prereg4.html)</li>
<li>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)</li>
</ol>

<p><em>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 theres 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 bit beyond me at this stage! Any pointers welcome..</em></p>

<p><strong>Anyhow heres the if/else statement:</strong></p>

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

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

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

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

<p>Thanks for any help..</p>
