Question

I have a contact form on a landing page, when the user fills out the form the information is passed via the URL to a second landing page. On the second landing page, I have a javascript that pulls the first name, last name and email from the URL and inserts them into the corresponding fields. However the email is added to the email field on the second landing page with %40 instead of the '@' symbol (bob%40bob.com). So when the user tries to submit the form it doesn't validate.

I understand I need to decode the URI, but I'm not sure how I need to implement it into the existing javascript. Below is the javascript that I have on the second landing page to parse the URL and insert the variables into the form.

<script type="text/javascript">
function getQueryVariable(variable) {
               var query = window.location.search.substring(1);
               var vars = query.split("&");
               for (var i=0;i<vars.length;i++) {
                       var pair = vars[i].split("=");
                       if(pair[0] == variable){return pair[1];}
               }
               decodeURIComponent(vars);
               return(false);

        }



        var firstName = getQueryVariable("firstname");
        var lastName = getQueryVariable("lastname");
        var userEmail = getQueryVariable("email");

        window.onload = function() {
            document.getElementById('field[63]').value=firstName;
            document.getElementById('field[64]').value=lastName;
            document.getElementById('field[62]').value=userEmail;
        }

</script>

The form fields are created by a widget without any access to the code.

Était-ce utile?

La solution

You are attempting to decode after the match is returned, so instead of

if(pair[0] == variable){return pair[1];}

use

if(pair[0] == variable){return decodeURIComponent(pair[1]);}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top