Home » Php » php – Jquery .serialize() not processing value of dropdown list?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&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!

Answer:

I was facing the similar situation. SELECT tag has an attribute named form. Define the form=”#ID_OF_THE_FORM_YOU_WANT_YOUR_SELECT_TO_ATTACH_TO”. Don’t forget to define an ID for your FORM as well.