1

I am hoping someone can help me with this problem I am facing:

I have an ajax call as follow:

$(document).ready(function(){  
    $("#booking").submit(function() {
        var arrival   = $('#arrival').attr('value');
        var departure = $('#departure').attr('value');
        var ap_ID     = $('#ap_ID').attr('value');

        $.ajax({
          type: "POST",
          url: "ajax/val_booking.php",
          data: "arrival="+ arrival +"&departure="+ departure +"&ap_ID=" + ap_ID,
        });

        return false;
    });
});

All the fields in the html form have a "name" attribute.

When sending info the ap_ID is sent but the arrival and departure are empty (checked with Firebug).

Also used serialize() but same result.

Does anyone know where might be the problem or what I might be doing wrong?

Thank you everybody for your help.

PS: I am using datepicker

4
  • Have you tried .val() instead of .attr() Commented Apr 11, 2011 at 16:22
  • 1
    I'd suggest posting the HTML for the arrival and departure elements. Commented Apr 11, 2011 at 16:22
  • Hi JacoM, Can you be more specific please? Commented Apr 11, 2011 at 16:24
  • Jason McCreary. Same resul...empty fields. Commented Apr 11, 2011 at 16:26

2 Answers 2

1

Try like this:

$('#booking').submit(function() {  
    var arrival = $('#arrival').val()  
    var departure = $('#departure').val();  
    var ap_ID = $('#ap_ID').val(); 
    $.ajax({  
        type: 'POST',  
        url: 'ajax/val_booking.php',  
        data: { 
            arrival: arrival, 
            departure: departure, 
            ap_ID: ap_ID 
        },
        success: function(result) {
            alert('success');
        }
    });  
    return false;  
});

or if you want to send all form values you could use the .serialize() function:

$('#booking').submit(function() {  
    $.ajax({  
        type: 'POST',  
        url: 'ajax/val_booking.php',  
        data: $(this).serialize(),
        success: function(result) {
            alert('success');
        }
    });  
    return false;  
});
Sign up to request clarification or add additional context in comments.

1 Comment

I did try the .serialize function before but it didn't work. I tried using your suggestion and it does work now. For some reasons none of the other suggestions work. Thank you very hepful.
1

You have a comma at the end of "data: "arrival="+ arrival +"&departure="+ departure +"&ap_ID=" + ap_ID,"

Remove the comma and you should be good.

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.