Skip to content
Advertisement

Cannot Get Values from my Function to my update.php

So i cant receive the values from my function to update my database, the code is working if i manually put the values.

My function

   function changeStatus(id, ativo) {
        $.post(baseUrl + "article_type/update.php",
            {
                id: id,
                ativo: ativo
            },
    
            function (data) {
                console.log(data)
            });
    }

My update api

<?php
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: PUT');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');

  include_once '../../config/Database.php';
  include_once '../../models/Articles_type.php';
  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();

$data = json_decode(file_get_contents("php://input"));
  // Set ID to UPDATE
$data = [
    'ativo' => $data->ativo,
    'id' => $data->id,
];
$sql = "UPDATE tipo_artigos SET ativo=:ativo WHERE id=:id";
$stmt= $db->prepare($sql);
$stmt->execute($data);
 
?>

Advertisement

Answer

Below code worked me, if you think it is right format you can follow:

 header('Access-Control-Allow-Origin: *');
  //header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: PUT');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With'); 

$data = json_decode(file_get_contents("php://input"));
  // Set ID to UPDATE
$data = [
    'ativo' => $_POST['ativo'],
    'id' => $_POST['id'],
];
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement