views:

223

answers:

1

hi there, i have this form:

<form name="myForm" action="#">
     <input type="text" name="firstField" />
     <input type="text" name="secondField" />
     <input type="submit" name="submitButton" />
</form>

and i have an ajax request:

$('input[type="submit"]').click(function(){
       var serialized = $('form').serialize();
       //ajax request
       $.ajax({
                type : "POST",
                url : "takeAction.php",
                data : serialized,
                succes : function(){
                    alert('done');
                }
       }); 
});

the problem is that if any of my fields value contains "'", like (who's the boss) my ajax request fails to complete (i'm trying to update an mysql row but the code fails, i get no error just that my row is not updated). i know that it's something about quotes but i don;t know how to do it. thanks

+4  A: 

The problem is in your PHP code on the server, not in your HTML or JavaScript.

You're probably writing the submitted data values directly into an SQL query, yes?:

$query = sprintf("SELECT * FROM users WHERE user='%s'", $user);

You need to either use prepared statements, or use mysql_real_escape_string() to escape your values:

$query = sprintf("SELECT * FROM users WHERE user='%s'",
                  mysql_real_escape_string($user));
RichieHindle
and if i have $var1 = $_POST['firstField'] $var2 = $_POST['secondField'] $rowId = $_POST['id'] and i want to update my row how should my code look? something like this? $sql = sprintf("UPDATE table SET mysqlField_1='%s',mysqlField_2='%s' where id='%s'",$var1,$var2,$rowId);
kmunky
sorry..like this? $sql = sprintf("UPDATE table SET mysqlField_1='%s',mysqlField_2='%s' where id='%s'",mysql_real_escape_string($var1,$var2,$rowId));
kmunky
got it, $sql = sprintf("UPDATE table SET mysqlField_1='%s',mysqlField_2='%s' where id='%s'",mysql_real_escape_string($var1),mysql_real_escape_string($var2),mysql_real_escape_string($rowId));, THANKS ;)
kmunky