jquery ajax php自动保存功能

时间:2011-12-29 00:52:35

标签: php jquery ajax cookies

我目前正在为一个客户在线广告网站工作,我有一个很大的问题,我很难受! 所以我为广告发布设置的系统是这样的,用户进入页面并开始从三个下拉菜单中选择,然后用输入框跟进以放置确切的位置。但作为一个用户类型,我想自动保存他们正在键入的cookie ...但我只需要信息替换cookie数据,如果它是新的其他明智的做什么...现在我不会抛出任何错误或任何东西但是,我无法更新内容而不更换它 html标记:

<?
if(isset($_COOKIE['adContent'])){
    $AD = explode('/',$_COOKIE['adContent']);
    //print_r($AD); 
}
if(isset($_COOKIE[md5('UniqueAdId')])){

}else{
$path = "/";
$expire =  time() + 7200;
$r = time().'+'.rand().'+'.$_COOKIE[md5('id')];
setcookie(md5('UniqueAdId'),$r,$expire,$path);  
}
?>
<form id="post-ad" style="position:relative;" enctype="multipart/form-data">
    <fieldset>
    <legend>Select Ad Location</legend>
    <table>
    <tr><td><select id="state" name="state"><option value="" disabled>State</option><? states(); ?></select></td></tr>
    <tr><td><select id="city" name="city" style="display:none"><option value="" disabled>Select a state first</option></select></td></tr>
    <tr><td><input id="area" type="text" name="area" value="Select a city first" style="display:none; width:200px;"></td></tr>
    <tr><td><button id="mothn" style="display:none;">Next</button></td></tr>
    </table>
    </fieldset>
    <input id="Apart" type="hidden" value="<? echo md5(1); ?>" /> 
</form>

Jquery的:

<script>
$('button').button();

$('#mothn').click(function(event){
    event.preventDefault();
    var part = $('#Apart').val();
    $('pload').html('<img src="source/image/lbl.gif">');
    var formdata = 'why=paa&p='+part+'&'+$('#post-ad').serialize();
    $.post('source/php/bots/functions/quickie.php',formdata,function(data){
            $('form').html(data);
            $('pload').html('');
        });
});
$('#post-ad fieldset select').change(function(e) {
    var id = $(this).attr('id');
    switch(id){
        case 'state':
            var state = $('#'+id).val();
            get_citys(state);
        break;  
        case 'city':
            $('#area').val('Area (Optional)').slideDown();
        break;
        case 'area':
            $('##post_ad2').slideDown();
        break;

    }

});
$('#area').keypress(function(){
    $('#mothn').slideDown();
    var formdata = 'why=as&'+$('#post-ad').serialize();
    $.post('source/php/bots/functions/quickie.php',formdata,function(data){
        // just stores current form into a cookie for saving purposes
        });
    });
function get_citys(state){
    var data = "why=gc&s="+state;   
    $.post('source/php/bots/functions/quickie.php',data,function(data){
        $('#city').html(data).slideDown();

    });


}
</script>

php脚本:

case 'as':
        // auto save
        if(isset($_COOKIE['adContent'])){
            //$more = $_COOKIE['adContent'].'/';
            $cont = explode('/',$_COOKIE['adContent']);
            print_r($cont);
        echo "<br>";
            foreach($cont as $x){
                foreach($_POST as $key=>$p){
                    if($x == $p){
                        unset($_POST[$key]);
                    }else{
                    $cont[$key] = $_POST[$key]; 
                    }
                }
            }
            $put = implode('/',$cont);
        print_r($cont);
        }else{ 
        $more = implode('/',$_POST);
        $path = "/";
        $expire =  time() + 7200;
        setcookie('adContent',$more,$expire,$path);
        }


    break;

1 个答案:

答案 0 :(得分:0)

如果您只想保存表单详细信息,客户端只需尝试这样的插件;

http://rikrikrik.com/jquery/autosave/

不需要php做任何事情,只需让jquery在某些条件下恢复表单