Welcome Guest, Not a member yet? Register   Sign In
better way to do ajax reponse call?
#1

[eluser]Angkor[/eluser]
My CI app use dropdown like this:
http://www.codeassembly.com/examples/jqu.../test.html

Code:
$(function()
{
    var url = <?= json_encode(base_url()); ?>;
    var response_url = url+'system/application/helpers/combobox.php';
    $('#company').chainSelect('#position',response_url,
    {
        before:function (target) //before request hide the target combobox and display the loading message
        {
            $("#loading").css("display","block");
            $(target).css("display","none");
        },
        after:function (target) //after request show the target combobox and hide the loading message
        {
            $("#loading").css("display","none");
            $(target).css("display","inline");
        }
    });
    
    
});

combobox.php
++++++++++++

Code:
<?php

$dbconn = pg_connect("host=xxx.xxx.xxx.xxx dbname=xxx user=postgres password=xxxxx");

$com_filter = $_GET['_value'];
$result = pg_query($dbconn, "SELECT position_id, title FROM position WHERE company_id = $com_filter"); // Sample of SQL QUERY
if (!$result) {
  echo "An error occured.\n";
  exit;
}
$no_result = array(array(''=>'No position'));
$arr_pos = array();
$array_json =array();
while ($row = pg_fetch_array($result)) {

    $array_json[] = array($row['position_id'] => $row["title"]);
}


if(count($array_json)>0){
    echo json_encode( $array_json  );
}
else{
    echo json_encode( $no_result  );
}

?>

Now it works But any better ways to implement the commbox.php

by using codeIgniter Frameworks.
for example with connect to database ..I use php

that I put username ,pass directly ..How can we got from the config file of IC?
or any good ways to do it?

thanks
#2

[eluser]vitoco[/eluser]
Steps :

1.- download codeigniter DOWNLOAD
2.- install codeigniter
INSTALLATION GUIDE
3.- Watch the tutorial videos
INTRO
CREATING A BLOG IN 20 MINUTES
4.- Create the controller / function
5.- Create, configure database
DB GUIDE
6.- Checkout the json helper
WIKI : JSON HELPER
7.- make it work....

Saludos
#3

[eluser]whobutsb[/eluser]
What I like to do is create a AJAX controller that is devoted to receiving and sending back JSON responses with echos.

My guess is you already have CI installed. Create a new controller call it AJAX controller, create a function that will take the jQuery AJAX submit, do the processing with your database using models and then encode your data into JSON and echo it back out. jQuery will pick up the response with out a problem.
#4

[eluser]Myles Wakeham[/eluser]
Yep, we do this too. Have a separate controller for all AJAX calls as the overall interface for this. Also you will need to have a strategy for security, and cross-domain AJAX communications if you are going to share the CI app with multiple domains. We use a PHP proxy code base on the client and have jQuery work with it locally, and then have that communicate with our CI apps.

But like everything in PHP, think security, think security, think security....

Myles




Theme © iAndrew 2016 - Forum software by © MyBB