-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.

gramel87
  • 1
  • 2
  • This is not necessary: `jQuery(document).ready(function () { jQuery(function ($) {` - THIS - `$(function() {...` is enough – mplungjan Mar 30 '17 at 16:51
  • @mplungjan Unless `$` is overwritten by another library, it is effectively the same. – Xorifelse Mar 30 '17 at 16:53
  • It is almost never and if it is, then the method is https://api.jquery.com/jquery.noconflict/ – mplungjan Mar 30 '17 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. – Xorifelse Mar 30 '17 at 16:57
  • @mplungjan Take a good look at `jQuery(function ($) {`, by definition the `$` already overwritten in the function, but yes you are correct but I've learned never to assume. – Xorifelse Mar 30 '17 at 16:58
  • @Xorifelse notice the use of jQuery inside the supposedly protected $ thing. OP is just copypasting stuff - possibly from w3fools – mplungjan Mar 30 '17 at 17:01
  • 1
    You need to use `onSelect: function(dateText) {` instead of `$from.change` and then you have the value in dateText – mplungjan Mar 30 '17 at 17:04
  • Probably I didn't explain well, i want to take the "deposito" value or "spese" value and put it in div "result". I tried with data.deposito or data[0].deposito but it shows as undefined. – gramel87 Mar 30 '17 at 17:24

1 Answers1

0

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

Kirill Simin
  • 354
  • 2
  • 12