For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Friday, March 13, 2015

Php json and jquery

How to use PHP, JSON and JQUERY

It is very simple to use PHP, JSON and JQUERY. for this you needed to create two files.
One file is "index.html", second file is "util.php" and this file the class file "DBClient.php" that will responsible for mysql database connection and return result according the sql query.
index.html file call the util.php using jquery ajax metod. and util.php is calling the DBClient.php and create it's object and return the result. the result will parse in index.html and parse it.




index.html

<input type="button" class="button" value="get value" id="btn1" />
<script language="javascript" type="text/javascript">
$("#btn1").click(function() {
    $.ajax({
    type: 'POST',
    url: 'util.php',
    data: { para1: 'city' },
    dataType: 'json',
    success: function (data) {   
        $.each(data.class_info, function(idx, obj) {
            alert(obj.city_name+"===="+obj.city_id);
        });
    }
});  
});
</script>

// you can also use below code,but then you will use different jquery code as below:
 //print  json_encode($rows);
// $.each(data, function(idx, obj) {
//           alert(obj.city_name+"===="+obj.city_id);
//        });util.php

<?php
include_once("DBClient.php");

if(isset($_POST['para1'])) {
$num_rows =0;
$db=new DBClient();
// the "city" table holding the columns "city_id" and "city_name"
$rs_qlist=$db->exeQueryi("select *from city");
$rows = array();
while($r = mysqli_fetch_assoc($rs_qlist)) {
    $rows[] = $r;
}
print ' { "class_info" : ' . json_encode($rows) . ' } ';
}
?>

// you can also use below code,but then you will use different jquery code as below:
 //print  json_encode($rows);
// $.each(data, function(idx, obj) {
//           alert(obj.city_name+"===="+obj.city_id);
//        });


DBClient.php
<?php
//error_reporting(E_ALL ^ E_DEPRECATED);
class DBClient {
var $host,$db,$user,$pass;
var $mysqli,$isDBConnect;
function __construct() {
    $this->host = "localhost";
    $this->db = "mydb";
    $this->user = "root";
    $this->pass = "pass";
    try {
          $this->mysqli = new mysqli($this->host, $this->user, $this->pass,$this->db );
        if ($this->mysqli->connect_error) {
            die('Error : ('. $this->mysqli->connect_errno .') '. $this->mysqli->connect_error);
        }
    } catch(Exception $ee) {
        $this->link=false;
           $this->isDBConnect=false;
    }
}

public function exeQueryi($query) {
    try {
        $result = $this->mysqli->query($query);
        return $result;
    } catch(Exception $ee) {
        return (FALSE);
    }
}

public function autocommit_false() {
    $this->mysqli->autocommit(FALSE);  
}

public function commit() {
    $this->mysqli->commit();  
}

public function rollback() {
    $this->mysqli->rollback();
}
public function close() {
    $this->mysqli->close();
}
}
?>
Share:

0 comments:

Post a Comment

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive