1

I appreciate all the help I might get.

I'm trying to save some user settings on focusout with serialize method, but I can't get the variable into the PHP.

<form id ="form1">
<input type="text" id="content" value="myvalue">
</form>


<script>
$("#form1").focusout(function(){

        $.ajax({
            type: "POST",  
            url: "save.php",  
            data: $("#form1").serialize(),
            success: function(){
            }  
        });
});
</script>

save.php:

$content = $_POST['content'];

$sql = "UPDATE test_table SET color = '$content' WHERE id = 0";
$execute = mysqli_query($mysqli_connect,$sql);

If I set the $content manually ($content = 'bbb';) it writes it to DB normally. So I guess I'm not retrieving variables in the right way?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
sasos
  • 11
  • 2

1 Answers1

1

Your form needs an element named 'content':

<form id ="form1" method="post">
    <input type="text" id="content" name="content" value="myvalue">
</form>

Named elements appear in the post ($_POST) array. In addition, the default method for form posting is GET, so you would need to change it to POST as done here. Either that or you could find your values in the get ($_GET) array.

In addition, your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119