Pulling PHP information via AJAX with jQuery in json format

5

I have a jQuery code that creates a calendar that lets you leave reminders on the date. The code is as follows:

 var calendar = $('#calendar').fullCalendar({
        slotDuration: '00:15:00', /* If we want to split day time each 15minutes */
        minTime: '08:00:00',
        maxTime: '19:00:00',          
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        events: [
        {
            title: 'teste!',
            start: new Date(y, m, 3),
            className: 'bg-purple'
        }, 
        {
            title: 'teste 2!',
            start: new Date(y, m, 1),
            className: 'bg-red'
        }, 
        {
            title: 'See John',
            start: '2014-05-05 10:00:00',
            start: '2014-05-05 11:00:00',
            className: 'bg-red'
        }
        ],

The party that creates the reminders is "events:" , I would like to pull events from a PHP page and perform the same code creation process as events:

events: [
    {
        title: 'teste!',
        start: new Date(y, m, 3),
        className: 'bg-purple'
    }, 

That is, I have a PHP that has a echo and in it has the code above, jQuery has to pull and form this type of code. The problem is that I can not do this.

    
asked by anonymous 23.06.2014 / 02:59

2 answers

5

If it is to generate JSON, PHP has the json_encode() function:

$pessoa = array(
    "nome" => "Pessoa 1",
    "sobrenome" => "da Silva"
);

echo json_encode($pessoa); // Vai gerar {"nome":"Pessoa 1", "sobrenome":"da Silva"}

// Se vc quiser um array de pessoas ...

$pessoas = array(
    array("nome"=>"Pessoa 1", "sobrenome"=>"da Silva"),
    array("nome"=>"Pessoa 2", "sobrenome"=>"dos Santos"),
    array("nome"=>"Pessoa 3", "sobrenome"=>"dos Gists")
);

echo json_encode($pessoas);

/* vai gerar 
[
    {"nome":"Pessosa 1", "sobrenome":"da Silva"},
    {"nome":"Pessosa 2", "sobrenome":"dos Santos"},
    {"nome":"Pessosa 3", "sobrenome":"dos Gists"}
]
    
23.06.2014 / 03:05
2

If the problem is just receiving js what you are sending from PHP, just make a call with $ .ajax, passing the PHP page and the dataType: "json".

link

    
29.06.2014 / 22:34