1

I have a form that on submit I want to pass the email address value to the url as a parameter. How can I incorporate that into my current setup below:

<form action="/event/" method="POST" id="signupForm">

And here's my

$("#signupForm").submit(function(e) {
    e.preventDefault();
    var form = $(this);
    var email = $("#EmailAddress").val();

    $.ajax({
           type: "POST",
           url: form.attr('action'),
           data: form.serialize(),
           success: function(data)
           {
               console.log(data); //data contain response from your php script
               register_signup();
               form.replaceWith("<br /><p>Thanks for signing up!</p>");
           }
         });

});

3 Answers 3

2

You could just append it to the url:

$("#signupForm").submit(function(e) {
    e.preventDefault();
    var form = $(this);
    var email = $("#EmailAddress").val();

    $.ajax({
           type: "POST",
           url: form.attr('action') + '?email=' + email,
           data: form.serialize(),
           success: function(data)
           {
               console.log(data); //data contain response from your php script
               register_signup();
               form.replaceWith("<br /><p>Thanks for signing up!</p>");
           }
         });
});

In the server you will need to retrieve the data from the query string for the parameters in the URL and from POST for the data posted from your form:

parse_str($_SERVER['QUERY_STRING'], $query);
echo $query['email'];

And for post values just simply use $_POST['input_name'].

Sign up to request clarification or add additional context in comments.

2 Comments

Great thanks, wasn't sure on the syntax. If I understand it correctly, as it's ajax the page won't reload so the parameter doesn't show in the url, is that right? I'm trying to call a php file that then pulls in the parameter but it doesn't appear to pull it in.
See the edit, hope it helps. You might as well include the email in the form as a post value instead of adding it to the URL, no? If you don't want the users to see it just add it as a hidden field.
1

As an alternative you can add an input field with hidden flag to your form and set its value to the email address before serializing.

Comments

0

instead of using url: form.attr('action') use it like

url: "/event/?email="+email

OR

url: form.attr('action') + '?email=' + email

So,the ajax will be like

$("#signupForm").submit(function(e) {
  e.preventDefault();
  var form = $(this);
  var email = $("#EmailAddress").val();

$.ajax({
  type: "POST",
   url:  "/event/?email="+email, //OR  url: form.attr('action') + '?email=' + email,
   data: form.serialize(),
   success: function(data){}
 });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.