GET JQuery Variable

Discussion in 'JavaScript' started by Indo Dev, Jun 17, 2014.

  1. #1
    Hi..
    i have promo.html like :
    <html>
    <head></head>
    <body>
    <link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" type="text/css" rel="stylesheet" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
    $(document).ready(function() {
        $('#result').html('Proses pengecekan promo . . .');
        $.ajax({
            type : "GET",
            url:'http://localhost/1/promo.php',
            cache: false,
        }).done(function(data){
            if(data==''){
                $('#result').empty();
                $('#result').html('Maaf, sedang tidak ada promo.');
            }else{
                $('#result').empty();
                var div = $("#result");
                data = JSON.parse(data);
                for(var i=0; i<data.promos.result.length;i++){                      
                    div.append("Flight ID : "+data.promos.result[i].flight_id+"<br>");
                    div.append("Date : "+data.promos.result[i].flight_date+"<br>");
                    div.append("Nama Airlines : "+data.promos.result[i].airlines_name+"<br>");
                    div.append("No. Penerbangan : "+data.promos.result[i].flight_number+"<br>");
                    div.append("Harga Tiket : "+data.promos.result[i].price_value+"<br>");
                    div.append("Jam Berangkat : "+data.promos.result[i].simple_departure_time+"<br>");
                    div.append("Jam Tiba : "+data.promos.result[i].simple_arrival_time+"<br>");
                    div.append("Durasi Perjalanan : "+data.promos.result[i].duration+"<br>");
                    div.append("Gambar : <img src='"+data.promos.result[i].image.replace('https','http')+"' /><br>");
                    div.append("<a href='select.php'>Test link</a> <br>");
                    div.append("<hr>");          
                } 
            }
        })
    });
    </script>
    <div id="result">
    </div>
    </body>
    </html>
    HTML:
    and promo.php like:
    <?php
    require_once('var.php');
    $url = "http://api.xxx.com/general_api/get_flight_promo?token=".$Token."&output=json";
    // create a new cURL resource
    $ch = curl_init();
    
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    // grab URL and pass it to the browser
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //bypass the ssl certificate
    
    $Data = curl_exec($ch); //get contents
    
    $error = curl_error($ch); //get errors
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    ?>
    PHP:
    how to get search result ?
    thx b4
     
    Indo Dev, Jun 17, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You need to return the result from the PHP-file - right now you're not doing that.
    
    echo json_encode(array("content"=>"$Data","error"=>"$error")); // something like that
    
    PHP:
    that on the end of the script will get you started (perhaps). It all depends on what the callback-function in the ajax expect.
    I suggest you use Firebug in Firefox to test POST and return data when working with Ajax - it will give you errors if there are any, and then you have a bit more data to work with
     
    PoPSiCLe, Jun 17, 2014 IP