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 help would be great.

I want to pass 2 values ​​to data: "q="+qx, in ajax js how to do this.

Thanks jean

+4
source share
1 answer

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

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

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

 $.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 submit 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 install FireBug to better analyze what happens under the covers.

+12
source

Source: https://habr.com/ru/post/1333998/


All Articles