Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajaxForm not working

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

like image 422
X10nD Avatar asked Dec 17 '22 18:12

X10nD


1 Answers

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.

like image 75
Darin Dimitrov Avatar answered Dec 19 '22 06:12

Darin Dimitrov