-1

i want to use jquery datepicker to pick a date then when select it to do select query from the database according to the selected date i've seek about that and found this question jQuery datepicker with php and mysql they say i should use ajax to do it but i can't understand the implementation they do! can anybody help please i try to do something like that

    <script>

$(function() {

    $( "#datepicker" ).datepicker({minDate: 0, showAnim: "bounce",
     onSelect: function(dateText, inst) {<?php mysql_select_db($database_conn, $conn);
$query_time = "SELECT reservation.R_time FROM reservation WHERE reservation.R_date='"?>dateText<?php "'" ;
$time = mysql_query($query_time, $conn) or die(mysql_error());</script>
1
  • When PHP is used like that, it runs on the server when creating the page, it can't react to what the user does in the browser. You have to use AJAX -- look at $.ajax and $.get. Commented Apr 24, 2013 at 18:39

2 Answers 2

0

Change your JS to send an ajax call in the onSelect, passing the dateText.

$( "#datepicker" ).datepicker({
    minDate: 0, 
    showAnim: "bounce",
    onSelect: function(dateText, inst) {
        $.ajax({
            type: 'POST',
            url: 'select_time.php',
            data: {date : dateText},
            success: function(resp){
                if(false !== resp){
                    alert(resp);
                }
            }
        });
    }
});

Then in select_time.php

<?php
    if(!empty($_POST['date'])):
        $mysqli = new mysqli('connection info'); //mysqli connection
        $stmt= $mysqli->prepare('SELECT reservation.R_time FROM reservation WHERE reservation.R_date = ?'); //prepare the statement
        $stmt->bind_param('s', $_POST['date']); //bind our parameters
        $stmt->execute(); //execute our query
        $stmt->store_result(); // store result so we can check rows
        $stmt->bind_result($resTime); //we only want the reserveration time
        $stmt->fetch(); //fetch the rows
        if($stmt->num_rows() > 0):
            echo $resTime;  //return the time from the database
            $stmt->close(); //close the statement
        else:
            return false; //row doesn't exist, return false
        endif;
    endif;    
?>

This is untested, but I don't see any reason why it shouldn't work.

Sign up to request clarification or add additional context in comments.

Comments

-1

I think you're confusing a couple of things:

First, the PHP will always run before the page is loaded (and therefor the jQuery), so calling the PHP on the jQuery selector will never work.

What you need to do is create an ajax function that will fire on the jQuery's onSelect (and I generally use the onClose event if it is a popup calendar).

Javascript:

$( "#datepicker" ).datepicker({
minDate: 0, 
showAnim: "bounce",
onClose: function(dateText, inst) {
   $.post('select_time.php?dateText=' + dateText, function(queryResult){
         // do something with the return query
   });
}
});

PHP page:

$connection = mysqli_connect("localhost","username","password","db");

$sql = "SELECT reservation.R_time FROM reservation WHERE reservation.R_date={$_post['dateText']}";

if ($result = mysqli_query($connection , $sql))
  {
     while ($getData = mysqli_fetch_field($result))
       {
          echo $getData->R_time;
       }
     mysqli_free_result($result);
  }

mysqli_close($connection);

9 Comments

thanks a lot, i have one more question, what if i want to get the $time variable from that page, is there any statement required or just that variable will be defined in the original page with the datepicker?
Please don't use the MySQL provided in this answer. It's deprecated and full of flaws. This is unsafe. Instead, use MySQLI
Sorry, I'm a little confused by your question. The $time variable on the PHP page will be echoed back to the javascript page and become available in the response variable queryResult (in the code above). From there you can do what you like with it. @Ohgodwhy good point. I just tried to copy/pase what the OP had written for clarity.
Please update post using MySQLI and I will restore the negative point!
which post? the question or the answer? and how to do that? ? ? ?
|

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.