CodeIgniter Forums

Full Version: getJSON in CI problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[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.

El Forum

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

but not helping me

El Forum

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

El Forum

[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.

El Forum

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

regards

El Forum

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

I try jquery getJson http://docs.jquery.com/Ajax/jQuery.getJS...tacallback but nothing works.

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

El Forum

[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

El Forum

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

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

El Forum

[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

El Forum

[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
Pages: 1 2