CodeIgniter Forums
getJSON in CI problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: getJSON in CI problem (/thread-11726.html)

Pages: 1 2


getJSON in CI problem - El Forum - 09-21-2008

[eluser]Mitja B.[/eluser]
Code:
$izb = $_GET['izb'];
switch($izb)
{
    
case $izb :    
    $sql = $db -> select("SELECT * FROM " . DB_PREDPONA . "lastminute_mesta WHERE drzava= '".$_GET['izb']."'");
    while ($row = $sql ->fetchrow())
    {    
        $id = $row['id'];
        $ime = $row['mesto'];
        $array[$id] = $ime;
    }
    break;
}

echo json_encode($array);


Code:
/* getJSON */
function spremeniVrednost()
{
    $.getJSON("ajax.php",
      { izb: $("#select1").val()},
      function(json){
          $("#select2").removeOption(/./);
        $("#select2").addOption(json);
        }
    );
}


how can i import this in CI. I have no idea how tu use getJSON in CI.


getJSON in CI problem - El Forum - 09-21-2008

[eluser]Mitja B.[/eluser]
i look this link
http://codeigniter.com/wiki/JSON_Helper/

but not helping me


getJSON in CI problem - El Forum - 09-22-2008

[eluser]Mitja B.[/eluser]
anyone please?


getJSON in CI problem - El Forum - 09-22-2008

[eluser]Crimp[/eluser]
The $GET global is destroyed in CI. Use the higher level $.ajax function in jQuery with $POST and set the datatype to JSON.

Also, the PHP json_encode function requires a newer version, 5.2. You can check if the function exists and parse "manually" if it doesn't.


getJSON in CI problem - El Forum - 09-23-2008

[eluser]Mitja B.[/eluser]
do you have maybe any example of how to make all this?

regards


getJSON in CI problem - El Forum - 09-30-2008

[eluser]Mitja[/eluser]
I also trying with getJSON in CI.

I try jquery getJson http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback but nothing works.

Can someone tell em how can i use getJson in CI?


getJSON in CI problem - El Forum - 09-30-2008

[eluser]narkaT[/eluser]
the first thing you should do, is cleanup your php-code Wink

the switch/case-statement is totally useless, you could just drop it.

every variable you plan to use (especially if it is an array) should
be declared first, to prevent unexpected data to be injected into
your application.
in combination with register_globals such "errors" would allow "evil-people"
to run xss-attacks.


concerning the JS I would use jquery's post-funktion.
there is also a exmaple for a simple wrapper functionin the documentation:
Code:
$.postJSON = function(url, data, callback) {
    $.post(url, data, callback, "json");
};

and remeber to change $_GET to $_POST Smile

Greetings
Jan


getJSON in CI problem - El Forum - 09-30-2008

[eluser]Mitja[/eluser]
narkaT thx you.

Do you maybe have any example of it. How to auto-populating Select Boxes in CI.


getJSON in CI problem - El Forum - 09-30-2008

[eluser]narkaT[/eluser]
there's nothing special about implementing such a
functionality with CI.
Just create a controller that handles the data fetching:

Code:
class Fetchselectboxes extends Controller {
    /*
     * .....
     */

    function fetchjson($id) {
        $data = array();
        //fetch data from DB an prepare it...

        echo json_encode($data);
    }
}

you could then access the data with the following url:
Code:
www.yourdomain.com/fetchselectboxes/fetchjson/<id>

the most work will be done on the client-side, so the server-side
implementation is quite simple Wink

Greetings
Jan


getJSON in CI problem - El Forum - 10-09-2008

[eluser]Mitja B.[/eluser]
How to call fetchjson function in first <select> that will work on second <select>.

Like onChange="www.yourdomain.com/fetchselectboxes/fetchjson/<id>" or somehowe else.

Before i use JS function

/* getJSON */
function spremeniVrednost()
{
$.getJSON("ajax.php",
{ izb: $("#select1").val()},
function(json){
$("#select2").removeOption(/./);
$("#select2").addOption(json);
}
);
}

<select name="select1" id="select1" style="width: 250px;">
I make fnction like you said and use echo json_encode($data); but do not know what next