-1

I'm sending an ajax request through JavaScript on clicking a button. When button gets clicked a function is called from where ajax request is performed.

Here is the html code where function is called:

echo "
<form > ";
if ($status == 'regular') {
    echo "<input type='hidden' value='".$id."' name='id'>";
     echo "<input type='hidden' value='official' name='status'>";
    echo "<td><button class='btn btn-info' onclick='UpdateStatus(".$id.",'official')'>UPDATE TO OFFICIAL</button><br><br>";
}
if ($status == 'official') {
     echo "<input type='hidden' value='".$id."' name='id'>";
     echo "<input type='hidden' value='regular' name='status'>";
echo "<td><button class='btn btn-success' onclick='UpdateStatus(".$id.",'regular')'>UPDATE TO REGULAR</button><br><br>";
    }
echo "</form>";

UpdateStatus() is the function in which there is ajax request. From here I'm sending $id which is user ID and the status which is to be updated.

Here is the UpdateStatus() function:

<script>
function UpdateStatus(str,str1) {
if (str.length == 0) {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      //ready
    };
    xmlhttp.open("GET", "update_status.php?id=" + str + "&status=" + str1, true);
    xmlhttp.send();
}
}
</script>

The str and str1 are the Id and status respectively. Here is the update_status.php:

<?php
$id = $_REQUEST["id"];
$status = $_REQUEST["status"];
$server_name = "localhost";
$user_name = "root";
$password = "";
$db = "diwan";
$conect = new mysqli($server_name, $user_name, $password, $db);
if($conect->connect_error)
{ die("Connection failed ".$conect->connect_error); }
$sql = "UPDATE user SET status = '$status' WHERE UserID = $id";
if(!$conect->query($sql))
      {echo "error in adding record ".$conect->error;}
$result = $conect->query($sql);
?>

And when I click on button I get url of this format:

http://localhost/diwan_web/manageusers.php?id=2&status=official

But it's not updating the data in database. Please guide me where I'm wrong or if anything is missing. Any suggestion will be highly appreciated.

Murtaza Ahmad
  • 267
  • 1
  • 5
  • 16
  • What is the purpose of "txtHint"? and when you click on update button, do you get the values inside function UpdateStatus(str,str1)? You may use console.log(str,str1); inside function. – Sayem Oct 13 '19 at 10:32
  • Yes the values are coming to the function and going to next page, I've also posted the url, you can check it out – Murtaza Ahmad Oct 13 '19 at 10:52
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 13 '19 at 10:52
  • It's just a sample code for testing few things, I'll avoid these things in projects – Murtaza Ahmad Oct 13 '19 at 10:55
  • You are running your update query twice. Once in the `if` and then again to assign the return value to `$result`. – Dave Oct 13 '19 at 10:57
  • @Dave that's not the issue, still I removed it – Murtaza Ahmad Oct 13 '19 at 10:59

3 Answers3

0

Looks like your syntax is wrong. Try $sql = "UPDATE user SET status = ".$status." WHERE UserID = ".$id";

0

Code looks good. There is probably an error you are not seeing.

Add this to top:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
0

There were some issues:

  1. You need to ensure the button does not reload the page (with type='button' as mentioned in another post).
  2. We should always use double-quote for HTML attributes to prevent such mistakes (use onclick=\"UpdateStatus('param-here')\" instead of onclick='...')
  3. If you use PHP's double-quote features you do not need to concatenate manually (If PHP finds $ in double-quotes like echo "Variable is: $x" it tries to find and concatenate the $x variable automatically).

If you apply above mentioned changes your code should look like:

echo "<form >";
if ($status == 'regular') {
  echo "<input type='hidden' value='$id' name='id'>";
  echo "<input type='hidden' value='official' name='status'>";
  echo "<td><button type='button' class='btn btn-info' onclick=\"UpdateStatus('$id','official')\">UPDATE TO OFFICIAL</button><br><br>";
}
if ($status == 'official') {
  echo "<input type='hidden' value='$id' name='id'>";
  echo "<input type='hidden' value='regular' name='status'>";
  echo "<td><button type='button' class='btn btn-success' onclick=\"UpdateStatus('$id','regular')\">UPDATE TO REGULAR</button><br><br>";
}
echo "</form>";
Top-Master
  • 7,611
  • 5
  • 39
  • 71