Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .serialize() not processing value of dropdown list?

I think this should be a simple thing but for some reason all my form values are being serialized fine except for the selected value of the dropdown list, the form is below:

<form id="contactform">
    <label for="name">Name</label> 
    <input type="text" id=name name=name placeholder="First and last name" tabindex="1" />

    <label for="phonenumber">Phone Number</label> 
    <input type="text" id=phonenumber name=phonenumber placeholder="Please enter your phone number" tabindex="2" />

    <label for="email">Email</label> 
    <input type="text" id=email name=email placeholder="[email protected]" tabindex="3" />

    <label for="dropdown">Please Confirm:</label> 
        <select>
            <option value="question" selected="selected">I have a question</option>
            <option value="attending">I am attending</option>
            <option value="not-attending">I am not attending</option>
        </select>


    <label for="comment">Your Message</label>
        <textarea name="comment" id=comment name=comment placeholder="Enter something here, can't think" tabindex="5"></textarea> 

    <input name="submit" type="submit" id="submit" tabindex="6" value="Send Message"/>

</form>

and this is how I am serializing it:

$('#contactform').submit(function() {
    var query = $(this).serialize();

    $.ajax({
        type: "POST",
        url: "send.php",
        data: query,
        success: function(data) { // rest of function

and finally the bit of PHP I'm using to set the value as a variable is:

$dropdown     = $_POST['dropdown'];

AN example header is name=sgrggr&phonenumber=55555555555&email=me%40me.com&comment=quick+test so I'm stuck as to why the dropdown value isn't being picked up.

Thanks for your help.

like image 202
martincarlin87 Avatar asked Sep 13 '11 10:09

martincarlin87


People also ask

What is the use of serialize () method in jQuery?

The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

What is the use of serialize ()?

Definition and Usage The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network.

What is serialize array in jQuery?

The serializeArray() is an inbuilt method in jQuery which is used to create a JavaScript array of objects that is ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls. The controls can be of several types.

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .


1 Answers

Your dropdownlist needs a name attribute to be included by the submit.

<select name="dropdown">
        <option value="question" selected="selected">I have a question</option>
        <option value="attending">I am attending</option>
        <option value="not-attending">I am not attending</option>
    </select>

hope this helps!

like image 161
Dennis Avatar answered Sep 22 '22 13:09

Dennis