Pull PHP information via AJAX with jQuery in json format

Asked

Viewed 579 times

5

I have a code in jQuery that performs the creation of a calendar that you can 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 part that makes the creation of reminders is "Events:" , I would like to pull the events from a PHP page and carry out the same code creation process as Events, that:

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

I mean, 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 I can’t do it.

  • Look for jQuery ajax, I do not understand much ajax with jQuery so I will not give an answer.

  • pull ajax until I know, but I need to know how to form the code of that guy with the appropriate keys and brackets ([ and {)

2 answers

5


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

$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"}
]
  • But returns with { and [ ?

  • yes, if your array has non-numeric indexes it will return an object { }, if it only has integer numeric indexes, it will generate an array [ ... ]

  • and if you have numbers and letters? how to generate { e [

  • It does work, yes..

  • $arr = array('Word 1', 'a1d3', '8dz@'); echo json_encode($arr); With this it displays [{word1, word2}] ... ?

  • In this case he returns ["Word 1", "aid3", "8dz@"];

  • if your array is $person = array("name"=>"Vagner", "email"=>"[email protected]"); it returns {"name":"Vagner","email":"[email protected]"}

  • So the [ ] I have to add manually?

  • look at the example I made: Github Gist

  • Thank you! It worked

  • Vagner, I suggest you incorporate the extra explanations and code into the Gist within the answer, so the next ones who see this post don’t need to read all the reviews or visit another site to understand.

  • Vlw @brasofilo, I just did that, but I still don’t know how to leave the colored code ...

  • 1

    Strange, the question tags should enable syntax highlighting... But to force this, check out http://meta.pt.stackoverflow.com/q/33/201

  • 1

    The problem was the <pre> that does not produce syntax highlighting. Check out the details of markdown used in the stack.

  • @brasofilo and after the user select how this selected data can be sent to mysql. My case is as http://answall.com/questions/181981/form-din%C3%A2mico-e-send-to-mysql-via-jquery-e-ajax? noredirect=1#comment376494_181981

Show 10 more comments

2

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

http://api.jquery.com/jquery.ajax/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.