2

I have tried answers from various questions here and also from examples on google, however, none seem to be workings not sure what I am doing wrong.

I have the following code

<form>
<input type="text" id="regoffice_1" value="<?php echo $result['regoffice_1'];?>">
<input type="hidden" name="companyid" value="1">
</form>
<script>
$("#regoffice_1").on("change", function() {
  var itemVal = $("#regoffice_1").val();
  var dataObj = {companyid: $("#companyid").val(), regoffice_1: $("#regoffice_1").val()};

  processChange(dataObj);
});

function processChange(dataObj){

  $.ajax({
    type: "POST",
    url: "inc/dataforms/test.php",
    data: dataObj,
    dataType: "text", // If you expect a json as a response
    complete: function(data) {
      var Resp = data.responseText;
      console.log(Resp);
    }
  });
};
</script>

In the PHP file just a simple query

<?php
include('../config.php');
mysqli_query($dbc,"UPDATE `comp_companies`  SET `regoffice_1` = '$_POST[regoffice_1]' WHERE `company_id` = '$_POST[companyid]'");
?>

Nice and simple .. however I'm getting no errors shown or anything shown in the console and no data being updated

What am I missing ??

2
  • 1
    I'm no PHP expert, but it looks like you need to concatenate the $_POST values in to the SQL string. You also need to quote the property names, eg $_POST['regoffice_1']. You should also really look in to using prepared statements as your code is completely insecure; it's just asking for a SQL injection attack Commented Jul 25, 2018 at 8:50
  • Bobby table is real. Look for how to sanitize your inputs Commented Jul 25, 2018 at 8:52

2 Answers 2

3

You must add an id to your input :

<input id="companyid" type="hidden" name="companyid" value="1">
_______^^^^^^^^^^^^^^

Since you're selecting by id in :

{companyid: $("#companyid").val(), regoffice_1: $("#regoffice_1").val()};
_____________^^^^^^^^^^^^^^

Else you could use name selector instead like :

{companyid:  $('input[name="companyid"]').val(), regoffice_1: $("#regoffice_1").val()};

NOTE : You need to add quotes to your $_POST[]:

mysqli_query($dbc,"UPDATE comp_companies  SET regoffice_1 = '".$_POST["regoffice_1"]."' WHERE company_id = '".$_POST["companyid"]."'");
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help, that happen :)
0

Following @Zakaria's answer you also using

...'$_POST[regoffice_1]' and '$_POST[companyid]' 

in your UPDATE. You missed the quotes on $_POST['key'] as it's an associative array

"UPDATE `comp_companies`  SET `regoffice_1` = ' . $_POST['regoffice_1'] . ' WHERE `company_id` = '" . $_POST[companyid] . "'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.