UPTADE with JSON and Php

Asked

Viewed 45 times

1

I’m trying to do an uptade with json and php, however, is giving error and do not know what is the problem. If someone can help me I appreciate.

function uptade_cat() {
    
    var valor=$("#valor").val();
    var id_cat=$("#id_cat").val();
    var nome_categoria=$("#nome_categoria").val();
 

        $.ajax({
            type: "POST",
            url: "caixa/categoria_uptade.php",
            data: {nome_categoria:nome_categoria, valor:valor, id_cat:id_cat},
            dataType: "JSON",
            success: function (data) {
			$('#categoria_uptade').load(); 
			 $('#alert_message3').html('<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Sucesso!</strong> Categoria Atualizado.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');

				
            },
            error: function() {
				$('#erro').html('<div class="alert alert-danger alert-dismissible fade show" role="alert"><strong>Erro!</strong> Ocorreu algum problema ao atualizar categoria.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
           
            }
        });
  }
<div class="modal fade" id="cotegoria_<?php echo $mostra['id_cat'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Categoria: <?php echo $mostra['nome_categoria'] ?> (<?php echo $mostra['categoria'] ?>)</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
		  <form id="categoria_uptade" name="categoria_uptade">
			  <div class="form-row">
				  <input name="id_cat" id="id_cat" value="<?php echo $mostra['id_cat'] ?>" > 
				<div class="form-group col-md-6">
				  <label for="inputEmail4">Nome Categoria</label>
				  <input type="text" class="form-control" name="nome_categoria" id="nome_categoria" value="<?php echo $mostra['nome_categoria'] ?>">
				</div>
				<div class="form-group col-md-6">
				  <label for="inputEmail4">Valor</label>
				  <input type="text" class="form-control" name="valor" id="valor" value="<?php echo $mostra['valor'] ?>">
				</div>

		</div>
</form>
        <div id="alert_message3"></div>
        <div id="erro"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
		<button type="button" class="btn btn-success" name="insert-data" id="insert-data" onclick="uptade_cat()">Atualizar</button>

      </div>
    </div>
  </div>
</div>

categoria_uptade.php

  <?php 
    include ('../../conexao/conecta.php');


    $nome_categoria= $_POST['nome_categoria'];
    $id_cat= $_POST['id_cat'];
    $valor= $_POST['valor'];

    $stmt = $conexao->prepare("UPTADE caixa_categoria SET nome_categoria=:nome_categoria, valor=:valor WHERE id_cat=:id_cat");

    $stmt->bindparam(':nome_categoria', $nome_categoria);
    $stmt->bindparam(':valor', $valor);

    if($stmt->execute())
    {
      $res="Data Inserted Successfully:";
      echo json_encode($res);
    }
    else {
      $error="Not Inserted,Some Probelm occur.";
      echo json_encode($error);
    }

    ?>
  • What is the error message?

  • Appears only [Object][Object]

  • lacked the $stmt->bindparam(':id_cat', $id_cat);

  • I added $stmt->bindparam(':id_cat', $id_cat); it still points to [Object Object]

  • the UPDATE was spelled wrong.

1 answer

0

Already tried to send the post using serialize?

would look like this:

function uptade_cat() {
    var dados = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "caixa/categoria_uptade.php",
            data: dados,
            dataType: "json",
            success: function (data) {
            $('#categoria_uptade').load(); 
             $('#alert_message3').html('<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Sucesso!</strong> Categoria Atualizado.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');

            },
            error: function() {
                $('#erro').html('<div class="alert alert-danger alert-dismissible fade show" role="alert"><strong>Erro!</strong> Ocorreu algum problema ao atualizar categoria.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');

            }
        });
  }

In your php:

<?php
$post = filter_input_array(INPUT_POST, FILTER_DEFAULT);

prepare("UPTADE caixa_categoria SET nome_categoria=:nome_categoria, valor=:valor WHERE id_cat=:id_cat"); 
$stmt->bindparam(':nome_categoria', $post['nome_categoria']); 
$stmt->bindparam(':valor', $post['valor']); 
$stmt->bindparam(':id_cat', $post['id_cat']); 
if($stmt->execute()) { 
$res="Data Inserted Successfully:"; 
echo json_encode($res); 
} else { 
$error="Not Inserted,Some Probelm occur."; echo json_encode($error); 
} ?>

I have not tested but test there.

Browser other questions tagged

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