I’m having difficulties in ajax to return

Asked

Viewed 21 times

-1

To be well explained.. I’m starting now in the world of js and ajax..

I am making a code that create options of available times when click the button.. everything works normal in strings ("1/2/3/4/5"), but when it is string in the $_POST['name'] ///receives the date the client chose from the error when returning data from mysqli_query

HTML

<script type="text/javascript" src="ajax.js"></script>
<input type="date"   id=valor >
<p id="valorDigitado"></p>

AJAX.JS

$(document).ready(function(){


$('#form_consulta').submit(function(){
    event.preventDefault();
    var capturando = "";
    
    capturando = document.getElementById('valor').value;
    var resultado2 = capturando.substring(8, 15);


        var form4 = "";
        $.ajax({
            url:"php/consulta_horario.php",
            method:"POST",
            type:"POST",
            data:form4, 
            success: function(return_consulta) { 
                $( "#resp_consulta" ).html( return_consulta );

            },
            error: function() {
                alert("Erro ao achar caminho do arquivo");
            }
        }); 

        
        $.ajax({
    url: "php/consulta_horario.php",
    type: "POST",
    data: {
        name: resultado2,
    },
    success: function(response){
        console.log(response);
        console.log("Resultado:" + resultado2);
    }
});

    });



});

QUERY time.php

<?php
    $usuario = $_POST['name']; // Quando troco o POST por "1" ou "2" por exemplo o select funciona, mas quando coloco o post volta o error
    include_once("conexao.php");


$sql = "SELECT * FROM horario WHERE id = ".$usuario;
$resultado = mysqli_query($strcon,$sql) or die("<option>Erro ao retornar dados</option>");

 //Obtendo os dados por meio de um loop while
 while ($registro = mysqli_fetch_array($resultado))
 {
    echo $_POST['name'];
    if ($registro['6'] >= "1") {
        echo "<option>6:00</option>";
    }
    if ($registro['6:30'] >= "1") {
        echo "<option>6:30</option>";
    }
}  

?>

  • Put the error you receive...

1 answer

0

Looking at your code there are two things I’ve been able to observe:

1 - here you are assigning an id without "", then it should stay like this:

<input type="date"   id="valor">

2 - in your ajax the data parameter is going with invalid value, you always need to work the date as a key object and value, in your PHP I saw that you are waiting for the parameter name, but in ajax you are not sending it, so your ajax code should look like this:

var form4 = "";
        $.ajax({
            url:"php/consulta_horario.php",
            method:"POST",
            type:"POST",
            data:{ name: form4 }, 
            success: function(return_consulta) { 
                $( "#resp_consulta" ).html( return_consulta );

            },
            error: function() {
                alert("Erro ao achar caminho do arquivo");
            }
        });

This is what I could see in your code, but if you can post the error message you receive, and also if there is an error being displayed in your browser console. This would help a lot in a more accurate diagnosis.

I hope I’ve helped!

Browser other questions tagged

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