开发者

generate and compress html and send in response to ajax request then decompress with jquery/js

开发者 https://www.devze.com 2023-02-26 05:53 出处:网络
Ok. So I have a ajax request that I send to the server. This response contain the html that I am going to append to the site. My goal is to never reload the page, instead remove and add html with ajax

Ok. So I have a ajax request that I send to the server. This response contain the html that I am going to append to the site. My goal is to never reload the page, instead remove and add html with ajax requests in the background.

First I am not sure how I am going to generate the HTML with php. I want to create a table with previous successful logins. Something like this:

    $html = <<<HTML

    <div class="box">
        <h2>
            <a id="toggle-logins">Previous logins</a>
        </h2>
        <div class="block" id="logins">

        <table>
            <tr>
                <th>Country</th>
                <th>City</th>
                <th>IP address</th>
                <th class="currency">Time</th>
            </tr>   
            <tr>
                <td>Norway</td>
                <td>Oslo</td>
                <td>192.168.1.155</td>
                <td class="currency">2011-04-15 04:00</td>
            </tr>   
        </table>
        </div>
    </div>

HTML;

But how do I add a for loop for example into the mix so I can use arrays from the db to generate tables?

Next I send the json back.

$data = array( "message" => T_gettext("previous logins received"), "html" => $html );
return json_encode( $data );

Then I append it to the site with

var data = jQuery.parseJSON(data); 

    $("x")
    .append(data.html)

This works fine, but I would like to know how to password encrypt and compress the $data or $html variable with php and 开发者_JAVA百科then use jquery to decompress and decrypt it if this is possible. The password encryption might not be necessary in this example, but I would like to password encrypt chat and other features in the future.

Something like:

$html = super_encryption($html, $password); 

then in jquery

data.html = super_decryption(data.html, password);


just gzip the content. Browsers support this so no jQuery needed. Enable this by

<?php
  ob_start("ob_gzhandler");
?> 

As for password-protection. Just use SSL (https). There's no real safe javascript based way of encrypting your data.

0

精彩评论

暂无评论...
验证码 换一张
取 消