1

i have a php file with this code:

  <?php
dateIndaysoff($_GET['date']);
function dateIndaysoff($mydate){
 if(!$mydate)return false;


                $host = "localhost";
                $user = "user";
                $pass = "pass";
                $databaseName = "db";
                $con = new mysqli($host,$user,$pass,$databaseName);
                if ($con->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                    }

                $query = "SELECT date FROM table WHERE date = '$mydate'";
                $result = $con->query($query);
                 if ($result->num_rows < 1) {$exists =  "FALSE";} else {$exists = "TRUE";}
                 return $exists;
            }                               
        ?>

Now in my .js file i use this

jQuery(".datepicker").change(function() {  
   var val = jQuery(this).datepicker().val();
   var finalval = jQuery.get("url-to-phpfile/daysoff.php",{date:val});
  if (finalval) {

but its like i always have true as result. php by its own works perfect. i am trying in jquery to call php and pass through url the value from datepicker and if result is true do something. I have tried the jquery with a fixed value and its working like a charm. The mistake i do should be or inside php receiving the value with GET or in jquery receiving the result.

What do i do wrong?

1
  • what's the date format you are passing to $_GET['date']? How is it stored in the db? Commented Apr 30, 2016 at 19:06

2 Answers 2

3

I could be wrong, but it seems like you're assuming finalval to be your response, but it will actually be your XHR call. Try this in your listener:

jQuery(".datepicker").change(function() {  
    var val = $(this).datepicker().val();       // or $(this).val(); ?
    $.get( "ajax/test.html?date=" + val, function( data ) {
        alert( "Result is: " + data );
    });
});  

And, as an aside, don't forget to escape your injections, or use PDO binding.

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

2 Comments

yes but i want all this to do on change condition of .datepicker as posted at first place
Ya, so put it all into your .change listener. I'll update it to show you.
-1

Updated solution. In php file i use this:

<?php

$conversion = $_GET['date'];
$newDate = date("Y-m-d", strtotime($conversion));
dateIndaysoff($newDate);
$hello = dateIndaysoff($newDate);
echo 'Result:'.$hello . '<br>';
echo 'Converted date:'.$newDate. '<br>';
echo 'Initial date:'.$conversion. '<br>';
function dateIndaysoff($mydate){
        if(!$mydate)return false;

        //Connection
        $host = "localhost";
        $user = "user";
        $pass = "pass";
        $databaseName = "db";
        $con = new mysqli($host,$user,$pass,$databaseName);
        if ($con->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            }

        //Select data   
        $query = "SELECT date FROM table WHERE date = '$mydate'";
        $result = $con->query($query);
       // $db=JFactory::getDbo();
         if ($result->num_rows < 1) {$exists =  "FALSE";} else {$exists = "TRUE";}
         return $exists;
    }                               
?>

and in .js i use this:

jQuery(".datepicker").change(function() {
var val = jQuery(this).datepicker().val();
console.log(val);
jQuery.get("/daysoff.php",{date:val},function(dateIndaysoff){ // send back 0 or 1
      if (dateIndaysoff) {
       console.log("hide");
        jQuery('.chzn-drop li:nth-child(9)').hide();
        jQuery('.chzn-drop li:nth-child(10)').hide();
     } else {
       console.log("show");
         jQuery('.chzn-drop li:nth-child(9)').show();
         jQuery('.chzn-drop li:nth-child(10)').show();
     }
});
});

As you can see i have placed logs at php and js files in order to see whats going on. I get correct results if i use only php file with ?date at end of url and i see inside js that i get my date variable and it goes to URL.

But for some reason i get always HIDE condition from .js IF. I should get SHOW condition when date is equal to 30.04.2016 but it seems that either i have a mistake in js (possibly not) either i have something wrong between connection of those 2 files.

Any ideaS?

1 Comment

You're using an answer to ask a question? Anyway, it's easy to tell where your error is: console.log(dateIndaysoff);. You have a lot of problems with your PHP here, like quotes around true/false and echoing other stuff than your result. Did you try my answer above?

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.