-1

I have this code in default.php:

jQuery(document).ready(function() {
  jQuery(function($) {
    var $from = jQuery("#from"),
      $to = jQuery("#to");

    $from.datepicker({
      numberOfMonths: 1,
      minDate: 1,
      dateFormat: 'dd-mm-yy',
      onClose: function(selectedDate) {
        $to.datepicker("option", "minDate", selectedDate);
      }
    });

    $from.change(function() {

      jQuery.ajax({
        url: 'index.php?option=com_vehiclemanager&task=default2',
        type: 'POST',
        data: {
          datepicker_value: $(this).val()
        },
        success: function(data) {
          $('#result').html(data);
        }
      });
    });

    ...

This is the php script:

<?php

$database = JFactory::getDBO();

$datepicker_value = isset($_POST['datepicker_value']) ? $_POST['datepicker_value'] : NULL;  
$month = date("m",strtotime($datepicker_value));

  $selectstring = "SELECT * FROM #__vehiclemanager_rent_sal WHERE fk_vehiclesid = 128 AND monthW = '$month'";
  $database->setQuery($selectstring);
  $string = $database->loadObject(); 


header('Content-Type: application/json');
$array[] = array(
'deposito'=>$string->deposit, 
'spese'=>$string->cfee,
);
echo json_encode($array);
?>

I have a jquery datepicker in a table and i want to pick the variable from the php script via a database query with an ajax call, i can see the json object [{"deposito":"300","spese":"100"}] but the single php variabile shows me undefined. I inserted the task in the ajax url because the filename sends me back to the index.php, the homepage, as this is an external plugin. I hope I explained myself and thanks for your help.

8
  • This is not necessary: jQuery(document).ready(function () { jQuery(function ($) { - THIS - $(function() {... is enough Commented Mar 30, 2017 at 16:51
  • @mplungjan Unless $ is overwritten by another library, it is effectively the same. Commented Mar 30, 2017 at 16:53
  • It is almost never and if it is, then the method is api.jquery.com/jquery.noconflict Commented Mar 30, 2017 at 16:53
  • $(this) reffers to the event, document ready. Do datepicker_value: [from, to] instead and remove $ from $from and $to, prepend var instead. Commented Mar 30, 2017 at 16:57
  • 1
    You need to use onSelect: function(dateText) { instead of $from.change and then you have the value in dateText Commented Mar 30, 2017 at 17:04

1 Answer 1

0

Instead of $(this).val(), try referencing the actual element. Something like this: $('#from').val().

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.