ajaxForm not working

X10nD picture X10nD · Jan 2, 2011 · Viewed 18.2k times · Source

Here is my ajaxForm code

 var qx = $('#XText').attr('value');

    $.ajax({

     type: "post",
     url: "qsubmit.php",
     data: "q="+qx,
     success: function() {

     }
    });

And the insert code

include('db-config.php');

$q = $_POST['q'];


 $insert_ann = sprintf("INSERT INTO med_tab (med_title) VALUES ('$q')");
 mysql_select_db($database_med_pharm, $med_pharm);
 $Result1 = mysql_query($insert_ann, $med_pharm) or die(mysql_error());

For some reason this is not working not sure why, any and all assistance would be great.

I want to pass in 2 values in data: "q="+qx, in the ajax js, how do I get that done.

Thanks Jean

Answer

Darin Dimitrov picture Darin Dimitrov · Jan 2, 2011

If you are talking about the jquery form plugin your code should simply look like this:

$(function() {
    $('#idofyourform').ajaxForm(function(result) {
        alert('form successfully submitted');
    });
});

If not, then make sure you properly encode the request:

$.ajax({
    type: "post",
    url: "qsubmit.php",
    data: { q1: 'value 1', q2: 'value 2' },
    success: function(result) {
        alert('form successfully submitted');
    }
});

or if you want to send the contents of the form:

$.ajax({
    type: "post",
    url: "qsubmit.php",
    data: $('#idoftheform').serialize(),
    success: function(result) {
        alert('form successfully submitted');
    }
});

Finally, make sure you have installed FireBug to better analyze what's happening under the covers.