0

I want to load event calendar from Database, I got this code on line and is working perfectly.

My javascript:

$('#mycalendar').monthly({
    mode: 'event',
    jsonUrl: 'http://localhost/acadasuite_mobile/www/calendar.php',
    dataType: 'json'
});

My PHP

   $startdate = "2016-10-6";
   echo '{
    "monthly": [
       {
         "id": 2,
         "name": "This is a JSON event",
         "startdate": "2016-10-6",
         "enddate": "",
         "starttime": "12:00",
         "endtime": "2:00",
         "color": "#EF44EF",
         "url": ""
      }
    ]
  }';

ABOVE WORK WELL. But if I change the "2016-10-6" to '.$startdate.', it will not work again

ie. "startdate": '.$startdate.',

Please, what can be the problem; Is it the the javascript does not interpret the variable $startdate or PHP?

2
  • 1
    Read up on json_encode() Never try and build a JSONString yourself Commented Oct 18, 2016 at 18:33
  • You should create that object as an associative array and then use json_encode() to format the response. Commented Oct 18, 2016 at 18:33

2 Answers 2

2

PHP will not interpret variables inside strings denoted with single quotes. Switch to double quotes on the outside and escape your double quotes on the inside.

Alternatively, use string concatenation:

$startdate = "2016-10-6";
echo '{
"monthly": [
   {
     "id": 2,
     "name": "This is a JSON event",
     "startdate": "'.$stardate.'",
     "enddate": "",
     "starttime": "12:00",
     "endtime": "2:00",
     "color": "#EF44EF",
     "url": ""
  }
]
}';
Sign up to request clarification or add additional context in comments.

1 Comment

BUT Read up on json_encode() Never try and build a JSONString yourself
0

You forgot quotes:

"startdate": "2016-10-6", etc...

becomes

"startdate": ' . $stardate . ', etc..

which generates

 "startdate":2016-10-6, etc...

which is NOT valid JSON. Json can only represent VALUES, not expressions. And even if expressions were supported, that'd be a mathematical subtraction, and you'd be putting the value 2000 into startdate.

You need quotes:

     "startdate": "' . $startdate . '",
                  ^------------------^

To show where the (j)son/(p)hp delineation is:

     "startdate": "' . $startdate . '",
     jjjjjjjjjjjjjjppppppppppppppppppjj

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.