简体   繁体   中英

jquery ajax php autosave function

im currently working on a online ad website for a client and i have a big problem im stcuk! so the system i have setup for the ad posting goes like this, a user goes to the page and began selecting from three drop down menus and then following up with a input box to put there exact location. but as a user types i want to auto save what they are typing into a cookie... but i need the info only to replace the cookie data if it is new other wise do nothing... now im not throwing any errors or anything but , i cant get the content to update with out replacing it html markup:

<?
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 script:

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;

if you are just looking to save form details client side just try a plugin like this;

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

no need for php to do anything, just have jquery restore the form on certain conditions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM