0

I'm trying to retrieve a value of a table in MySQL.

I have a table called: "available_places" with two fields: "DATE" and "available_places"

In my php file, I have the datepicker, and what I'm trying to do is when the user pick a date, it automatically checks in the database and throws me: "I HAVE 'X' AVAILABLE PLACES".

Here is my input field where the datepicker puts me the date:

<input type="text" id="datepicker" name="date" onchange="ShowInfo('query.php');

Here is my javascript function:

function MostrarConsulta(datos) {
    divResultado = document.getElementById('result');
    ajax = objetoAjax();
    ajax.open("GET", datos);
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4) {
            divResultado.innerHTML = ajax.responseText
        }
    }
    ajax.send(null)
}

How i should do it, or how to pass the variable of the field to my consulta.php file?


Here is my full code:

<script>

    $.datepicker.regional['es'] = {
        closeText: 'Cerrar',
        prevText: '<Ant',
        nextText: 'Sig>',
        currentText: 'Hoy',
        monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
        monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
        dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
        dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mié', 'Juv', 'Vie', 'Sáb'],
        dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
        weekHeader: 'Sm',
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    $.datepicker.setDefaults($.datepicker.regional['es']);
    $(function () {
        $("#fecha").datepicker();
    });


    $(function () {
        $("#datepicker").datepicker({
            maxDate: new Date(2015, 11, 31),
            numberOfMonths: 1,
            dateFormat: 'yy-mm-dd',
//            dateFormat: 'DD dd MM yy',
            altField: "#altFormat",
            altFormat: "yy-mm-dd",
            minDate: 0,
            beforeShowDay: function (date) {
                var day = date.getDay();
                return [day == 6, ''];
            }
        });
    });


    function objetoAjax() {
        var xmlhttp = false;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }

        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
    }

    function MostrarConsulta(datos) {

        valor = document.getElementById("datepicker").value;
        document.write(valor);
        divResultado = document.getElementById('resultado');
        ajax = objetoAjax();
        ajax.open("GET", datos);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                divResultado.innerHTML = ajax.responseText
            }
        }
        ajax.send(null)
    }


</script>

---PART OF MY FORM---

<div class="form-group">
    <label for="fecha" class="col-sm-2 control-label">Fecha :</label>
    <div class="col-sm-10">
        <input type="text" id="datepicker" name="fecha" required onchange="MostrarConsulta('consulta.php');
                                return false">
        <div id="resultado"></div>
    </div>
</div>

----- AND MY PHP FILE ----

<?php
//Configuracion de la conexion a base de datos
$bd_host = "localhost"; 
$bd_usuario = "root"; 
$bd_password = "root"; 
$bd_base = "pruebas"; 

$con = mysql_connect($bd_host, $bd_usuario, $bd_password); 

mysql_select_db($bd_base, $con); 

//consulta todos los empleados

$sql=mysql_query("SELECT * FROM lugares_disponibles WHERE fecha=",$con);

//muestra los datos consultados
echo "</p>Fecha - Lugares - Tipo</p> \n";
while($row = mysql_fetch_array($sql)){
        echo "<p>".$row['fecha']." - ".$row['lugares']." - ".$row['tipo']."</p> \n";
}
?>
3
  • See if this link helps in adding the AJAX call to PHP using jQuery: stackoverflow.com/questions/20453879/… Commented Apr 30, 2015 at 16:18
  • I am not sure if the code that you have even constructs an AJAX call to your PHP file. Commented Apr 30, 2015 at 16:19
  • you need to show some more code for result div, objetoAjax Commented Apr 30, 2015 at 16:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.