Menu

How to make jQuery ajax (JSON) requests

php curl file (manager.php)

<?php
/**
 * Send a POST requst using cURL
 * @param string $url to request
 * @param array $post values to send
 * @param array $options for cURL
 * @return string
 */
function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}

/**
 * Send a GET requst using cURL
 * @param string $url to request
 * @param array $get values to send
 * @param array $options for cURL
 * @return string
 */
function curl_get($url, array $options = array())
{
    $defaults = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 4
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}

$options = array
(
   // CURLOPT_HEADERFUNCTION => 'read_body'
  //  CURLOPT_HEADERFUNCTION => 'read_header'
);

function read_header($ch, $string)
{
    $length = strlen($string);
    echo "Header: $string<br />\n";
    return $length;
}

function read_body($ch, $string)
{
    $length = strlen($string);
    echo "Received $length bytes<br />\n";
    return $length;
}



$appl_user=trim($_GET['name']);
$appl_action=trim($_GET['action']);
$appl_server= "server ip";


$url ='http://'.$appl_server.'/seminario/seminario-svisto.php?action='.$appl_action.'&user='.$appl_user;
$result = curl_get($url,$options);
if ($result){

    echo $result;

}
else{

    $data = array('success'=> false,'user'=>'9:9');

    echo json_encode($data);
}

?>

php file on server seminario-svisto.php

<?php
   $opendbast="seminario-svisto.db";
 $sqlast='
    CREATE TABLE conference (
        user_nr INTEGER PRIMARY KEY,
        user_name TEXT NULL,
        user_name_nr TEXT NULL,
        user_conference_name TEXT NOT NULL
        );';


if(!file_exists($opendbast)){
    try
        {
            $dbast = new SQLite3($opendbast);

        $query = $dbast->query($sqlast);
    }   catch(Exception $e)
        {
                $result = $e->getMessage();
                return $result;
        }
}

//$user_name = $_GET['user'];
   $dbast = new SQLite3($opendbast); // gia 
//$sql="SELECT * FROM conference WHERE user_name = '$user_name' ";
$sql="SELECT * FROM conference ";
                $res_sql = $dbast->query($sql)or die("Error in query: $sql");
                $row = $res_sql->fetchArray();
echo json_encode($row);


/**
* $sql="SELECT * FROM conference WHERE conference = '$conf' and user_tel = '$confuser'";
* $fp = fopen('data.sql', 'w');
* fwrite($fp, $sql);
* fclose($fp);
*        $result = $dbh->query($sql)or die("Error in ytuytutyu query: $sql");
*        $row = $result->fetchArray();
*        if(trim($row['user_tel']) == trim($confuser)){
*                $sql="UPDATE conference set micro = \"$R\" where user_tel = '$confuser' and conference = '$conf'";
*                $result = $dbh->query($sql)or die("Error in query: $sql");;
*        }else{
*                $sql="INSERT INTO conference (user_tel,conference,micro)  VALUES ($confuser,$conf,'$R')";
 *               $result = $dbh->query($sql)or die("Error in query: $sql");;
 *      }
 *       echo json_encode (array('success' =>'success','user'=>$confuser,'mode'=>'request'));
*
*/
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <script type="text/javascript" src="./jquery-1.6.2.min.js"></script>
        <title></title>
        <!-- // phpjs   >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
            <script src="./php.default.min.js" type="text/javascript"></script>
        <!-- // phpjs   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  -->
    <link href="./main.css" rel="stylesheet" type="text/css">

 <script type="text/javascript">


 var GET_ajax_file_rest = "manager.php";
 var name = "test";
  var select = "select";
                                                      var get_data = 'l=de';
                                                      get_data += '&action='+select;
                                                      get_data += '&name='+name;
                                                  $.ajax({
                                                             type: "GET",                                                         
                                                             url: GET_ajax_file_rest,
                                                             async:true,
                                                             dataType: "json",
                                                             data: get_data,
                                                             success:  show
                                                         });                                                 
                                             function show(data) {
                                             alert(data.user_name);
                                             }

</script>
<body>
</body>
</html>
Posted by root apostolos 2012-05-09

Log in to post a comment.