Register Data in Mysql using Ajax and PHP

Asked

Viewed 102 times

0

I am trying to register a date field for a service registration system and the queries are not being sent to the database. I tried using only PHP and it worked, but when I use the same code with AJAX does not work.

Remember that in my database, the field is with the DATE type.

This is my PHP (queries.php):

if($ordem == 8){
$data = $_POST['date'];
$sql = "INSERT INTO datetest (data)
VALUES ('$data');";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "Data: " .$data. " foi cadastrado com sucesso, com a ID: ". $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}}

In my HTML I have the date field:

    <label>Data do Serviço</label>
    <input class='form-control' type="date" id='date'>

And using Ajax for the query:

function FormSubmit(){
ordem = 8;
date = document.getElementById('date').value;
console.log(date);
$.ajax({
                url:'../config/queries.php',
                method:'POST',
                data:{
                    ordem:ordem,
                    date:date
                },
               success:function(data){
                   alert(data);
               }
            });

This is my full HTML:

<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="../css/main.css">
    <link rel="stylesheet" href="../css/vendas.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://kit.fontawesome.com/9b1c918fe2.js" crossorigin="anonymous"></script>
    <script src="anime.min.js"></script>
    <title>Cadastro de Clientes</title>
</head>
<body>

<div class='container-fluid' id='bannertop'>
    <h2><i class="fab fa-amazon"></i></h2>
</div>

<div class='row'>
<div class='container-fluid' id='content'>

<div class='row'>


<div class='col-xl-2 col-lg-2 col-md-2 col-sm-12' id='sidemenu'>

<?php

include_once('../phpmenu.php');

echo $menu;

?>

</div>

<div class='col-xl-9 col-lg-9 col-md-9' id='contentbox'>

    <div class='container-fluid' id='workbox'>
    <div class='row'>

    <div class='col-lg-12 col-xs-12'>
    <h4 style='text-align: center;'>Gerar Ordem de Serviço</h4>


    <div class='row'>

        <div class='container-fluid' id='generateorder'>
        <br>
        <label style='text-align: left;'>Tipo de Serviço</label>
        <select class='form-control' id='servicetype'>
            <option value="1">Dedetização</option>
            <option value="1">Estofado</option>
        </select>

        <label>Cliente</label>
        <select class='form-control' id='cliselect'>
            <option value="">Marcos Alexandria de Oliveira</option>
        </select>

        <label style='text-align: left;'>Produto a utilizar</label>
        <select class='form-control' id='servicetype'>
        <option value="1">Exemplo de Produto</option>
        <option value="1">Estofado</option>
        </select>

        <p></p>
        <label class='form-control' style='background-color: #ced4da;'>Infos do Produto</label>
        <p></p>
        <button class='form-control' id='btnadd'>Adicionar esse produto</button>

        <label>Valor do Serviço</label>
        <input class='form-control' type="text">

        <label>Data do Serviço</label>
        <input class='form-control' type="date" id='date'>

        <label style='text-align: left;'>Funcionário responsável</label>
        <select class='form-control' id='servicetype'>
            <option value="1">Definir mais tarde</option>
            <option value="1">Luis Carlos</option>
            <option value="1">Marcos Alexandria</option>
        </select>

        <br>

        <input class='form-control' value='Cadastrar' onclick='FormSubmit()' type="submit" name="submit" id="submitbtn">
        </div>

    </div>
    </div>



    </div>

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

<div class='errors' id='errors'></div>


<script>

$(document).ready(function() {
    ClientList();
});

function ClientList(){
    ordem = 12;
    $.ajax({
                    url:'../config/queries.php',
                    method:'POST',
                    data:{
                        ordem:ordem
                    },
                   success:function(data){
                       //alert(data);
                       $("#cliselect").html(data);
                   }
                });
}

function FormSubmit(){
    ordem = 8;
    date = document.getElementById('date').value;
    console.log(date);
    $.ajax({
                    url:'../config/queries.php',
                    method:'POST',
                    data:{
                        ordem:ordem,
                        date:date
                    },
                   success:function(data){
                       alert(data);
                   }
                });
}
</script>

</div>


</body>
</html>
  • post your full html and explain what happens when you try to submit your form

  • I edited it and added my full HTML, man. When I give Submit appears an Alert that is there in the AJAX code but this Alert does not return anything, comes empty. I’ve tried putting the return to a div and using console.log(), but both attempts also return nothing, literally, if you want a NULL or Undefined value.

  • the server is not giving error or in your browser console?

  • 0 errors expensive. At first I was seeing an error in MYSQL syntax, when I was using with PHP without AJAX. Fixed and worked. Using this same code without AJAX is working. The.log() console of the date returns a value recognized by PHP, which I have tested as well. Ex: 2020-03-25

  • I gave a general overview of the code and found the problem. Inside the queries.php there are several If’s and accidentally put this if inside the keys of another if. Now it’s working. I apologize for the lack of attention but I leave this report only in case another user has any similar problem.

No answers

Browser other questions tagged

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