4

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

6
  • What errors do you get? What exactly is not working? Commented Jan 2, 2011 at 17:09
  • 6
    You have a SQL injection vulnerability. Commented Jan 2, 2011 at 17:09
  • @pekka Data is not being inserted @Slaks Could you tell me point me to the correct the error and where the error is exactly, thanks in advance. Commented Jan 2, 2011 at 17:12
  • $q = $_POST['q']; $insert_ann = sprintf("INSERT INTO med_tab (med_title) VALUES ('$q')"); is the vulnerable code, you're inserting based on POST values which could easily be forged. Commented Jan 2, 2011 at 17:14
  • @cyclone Do I put an if? Commented Jan 2, 2011 at 17:18

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

5 Comments

For some reason there is no alert from $.ajax{} also no insert, guess the ajaxform is not being triggered for some reason
@Jean, FireBug will indicate if there are some errors. Are there any?
It was not an issue with the coding error, but a braces closed after the $.ajax.
@Darin.. Thanks for helping out.. Does not chrome have a firebug.
@Jean, you may checkout the Google Chrome Developer Tools.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.