0

I'm submitting a form via AJAX to a PHP script that sends an email to me with the form details. The form is pretty straight forward, except that I have a group of checkboxes that signify the type of project(s) the person filling out the form wants. I know how to assemble a datastring with a simple form, but I am having difficulty figuring out the best approach to adding the checkboxes with name equal to projecttype[]. In theory, I think I want something like:

website=example.com&projecttype=1,2,4,6&budget=$500-$1000. 

You can find the form and Javascript in this jfiddle: http://jsfiddle.net/H5MF8/

1 Answer 1

1

jQuery has a function exactly for that purpose: .serialize(). It takes every element in your form and creates a query string. The below snippet should be enough.

$("#submit").click(function() {
  $.ajax({
    type: "POST",
    url: "/bin/send_form_email.php",
    data: $('#contactform').serialize(),
    cache: false,
    success: function() {
    //    ...
    },
    return false;
  });   
});
Sign up to request clarification or add additional context in comments.

4 Comments

.serialize() gives me &projecttype=1&projecttype=5&projecttype=6. What's the best approach to reassembling the projecttype variable in PHP?
That's not what I get. I get projecttype[]=1&projecttype[]=2 etc. It should be accessible as an array $_POST['projecttype'] from PHP.
Sorry, I edited the field name, but its the same. OK I'll try that out, thanks.
If that's what you're getting, I suggest you upgrade your browser and possibly whatever JavaScript debugging tool you're using.

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.