2

I have coded an ajax request and the data is returned as an array to be used in a timepicker.

Ajax Call:

<script type="text/javascript">
$.ajaxSetup({ cache: false });
var consultdate;
var userid;
var cat;
var timezone;
var consultstart;
var consultend;
$('#consultdate').change(function() {
consultdate = $('#consultdate').val();
userid= '<?php echo $user_id;?>';
cat = '<?php echo $category;?>';
timezone = '<?php echo $time_zone;?>';
consultstart = '<?php echo $consultation_start;?>';
consultend = '<?php echo $consultation_end;?>';
//alert(consultdate);
$.ajax({
type: "POST",
url: 'user_date-time_qry.php',
cache: false,
dataType : "text",
data: {consultdate : consultdate, userid : userid, cat : cat, timezone : timezone, consultstart : consultstart, consultend: consultend },
success: function(data)
{
if (!$('input#consulttime').data("set")) {
alert(data);
var result = $.parseJSON(data);
$("input#consulttime").val('');
$('input#consulttime').prop("disabled", false);
$('input#consulttime').timepicker('remove').timepicker({'timeFormat': 'h:i A','disableTextInput': true, 'minTime':result[0] ,'maxTime': '<?php echo $consultation_end; ?>','step': 15,'disableTimeRanges':result[1]});
   }
},
error : function() {
alert("Error while loading data");
       }
   });
});
</script>

I expect var result to be an array of json encoded values, which is used in minTime as result[0] and disableTimeRanges as result[1]

The relevant portion of user_date-time_qry.php is as follows:

UPDATED: user_date-time_qry.php

 $consultation_start = '"'.$consultation_start. '"';
    $consult_time_UTC = '['.implode(',',$consult_time_UTC).']'; 
    $prebooked_time_UTC = $consult_time_UTC ; //require format as ['8.30 AM', '8.45 AM'], ['12:15 PM', '12:30 PM']

    echo $result = array($consultation_start, $prebooked_time_UTC);
$result = array($consultation_start, $prebooked_time_UTC);
foreach ($result as $results) {
echo $results; 
}

In console Getting value as "06:00 PM"[] "06:00 PM" is value 1 && [] is value 2. How to get it as separate values in ajax response ?

Update:

Since i am unable to comprehend it, trying another solution with 2 ajax calls. Thanks for all the feedback anyway.

3
  • dont compile the json yourself, use json_encode. Commented Nov 22, 2017 at 5:39
  • look your network response check if it has an error Commented Nov 22, 2017 at 5:56
  • @Lawrence Cherone I need to json encode time oicker in this format.['8.30 AM', '8.45 AM'], ['12:15 PM', '12:30 PM']. Hence i opted for manual encode. Commented Nov 22, 2017 at 6:11

4 Answers 4

1

Change this line

echo $result = array($consultation_start, $prebooked_time_UTC);

to this

$result = array($consultation_start, $prebooked_time_UTC);
echo json_encode($result);

Actually to get data in JSON format in PHP you can just store all the data in an array and use json_encode function to convert them.

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

1 Comment

But i have already data in json encoded format: $consultation_start = '"'.$consultation_start. '"';
0

The issue is here:

echo $result = array($consultation_start, $prebooked_time_UTC);

you cannot echo an array, instead you have to use json_encode like:

echo json_encode(array($consultation_start, $prebooked_time_UTC));

and do not wrap the values in quotes like:

'"'.$consultation_start. '"'

4 Comments

I have formated so that i need the datepicker format to be in: ['8.30 AM', '8.45 AM'], ['12:15 PM', '12:30 PM']. It was working fine without array.
cant we use foreach to echo array ??
Yes, you can. But that's not work with the ajax call
No you cannot loop, just use json_encode and parse it in js and use it accordingly.
0

simple way:--

The data is coming back as the string representation of the JSON and you aren't converting it back to a JavaScript object. Set the dataType to just 'json' to have it converted automatically.

Example Like:--

    $.ajax({
    type: "POST",
    url: 'user_date-time_qry.php',
    cache: false,
    dataType : "json",
    data: {consultdate : consultdate, userid : userid, cat : cat, timezone : timezone, consultstart : consultstart, consultend: consultend },
    success: function(data)
    {
    //ENTER CODE

    },
    error : function() {
           alert("Error while loading data");
       }

});

2 Comments

Response payload is none.
okay and using Anoter example i have enter new solution (solution title):-- Anoter example using this way
0

Anoter example using this way:--

user_date-time_qry.php file all convert into json format using below example all array convert into json format using this way:--

<?php
         $marks = array( 
            "mohammad" => array (
               "physics" => 35,
               "maths" => 30,   
               "chemistry" => 39
            ),

            "qadir" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),

            "zara" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );
         echo json_encode($marks);
      ?>

These respone provide (ALL DATA CONVERT INTO JSON FORMAT):--

{"mohammad":{"physics":35,"maths":30,"chemistry":39},"qadir":{"physics":30,"maths":32,"chemistry":29},"zara":{"physics":31,"maths":22,"chemistry":39}}

Comments

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.