UPDATE with AJAX

Asked

Viewed 476 times

1

I am doing an AJAX which, after the user select a checkbox and set a status (the status is select) AJAX take the information and send to a page called status.php where the status change box, different query are executed that will change the status in the database.

The problem: Even if I set the id and status, the query simply isn’t executed and I don’t understand the reason why.

This is the ajax code that, after the user clicks to change, captures the data and sends to the page php status.

function altera_status()
{

//caso  seja selecionado mais de uma checkbox ( e consequente, mais de um id) agrupa eles

var checkboxValues = $.map($('.checkped:checked'), function(e){
  return $(e).val();
  }).join();
//dados a enviar, vai buscar os valores dos campos que queremos enviar para a BD
var dadosajax = {
    'changePedi' : $("#changePed").val(),
    'checkbox' : checkboxValues,

};
console.log(dadosajax), 
pageurl = 'status.php';
$.ajax({

    //url da pagina
    url: pageurl,
    //parametros a passar
    data: dadosajax,
    //tipo: POST ou GET
    type: 'POST',
    //cache
    cache: false,
    //se ocorrer um erro na chamada ajax, retorna este alerta
    //possiveis erros: pagina nao existe, erro de codigo na pagina, falha de comunicacao/internet, etc etc etc
    error: function(){
        alert('Erro: Inserir Registo!!');
    },
    //retorna o resultado da pagina para onde enviamos os dados
    success: function(response)
    { 

    }
});
}

And this is the php status., that receives the data and should update the comic

<?php
$statusPed = $_REQUEST['changePedi'];
$checkStatus = $_REQUEST['checkbox'] ; 


$connect = mysqli_connect("localhost","root","", "notas-34ca74") or die ("Forninho fall"); 



switch($statusPed){
case 'separacao': 
if(isset($checkStatus)){
    $sql = 'UPDATE pedidos SET status="Em separação" WHERE id="$checkStatus" ';
    $result = mysqli_query($connect,$sql);
}
break;
case 'cancelado':
function filter( $dados ){
    $arr = Array();
    foreach( $dados AS $dado ) $arr[] = (int)$dado;
    return $arr;
}     
if(isset($checkStatus)){
    $arr = filter( $checkStatus);
    $sql = 'UPDATE pedidos SET status="Cancelado" WHERE id IN('.implode( ',', $arr ).')';
    $result = mysqli_query($connect,$sql);
}
break;
  • You are receiving the data correctly, as the friend mentioned below the problem may be in the quotes, you tbm can use string interpolation in php. $bar = "I am {$foo}!";

1 answer

0

The problem in your queries refers to quotes, will work if you exchange your updates as follows in the separation query:

  $sql = "UPDATE pedidos SET status = 'Em separação' WHERE id='$checkStatus' ";

Already in the condition of canceled so I realized the ids will be passed this way 345,345,... then the foreach will not work if that’s right and I did not understand wrong, in this case I would put a explode as follows below (should also be changed the quotes of the query):

function filter( $dados ){
    $dados = explode(',',$dados);
    $arr = Array();
    foreach( $dados as $dado ) $arr[] = (int)$dado;
    return $arr;
}     
if(isset($checkStatus)){
    $arr = filter( $checkStatus);
    $sql = "UPDATE pedidos SET status='Cancelado' WHERE id IN('".implode( ',', $arr )."')";
    $result = mysqli_query($connect,$sql);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.