Welcome Guest, Not a member yet? Register   Sign In
getJSON in CI problem
#1

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

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

but not helping me
#3

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

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

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

regards
#6

[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?
#7

[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
#8

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

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

[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
#10

[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




Theme © iAndrew 2016 - Forum software by © MyBB