Welcome Guest, Not a member yet? Register   Sign In
Fetch and Update Data in same page CodeIgniter
#4

Short answer: you need AJAX

Long answer: Since i cant see your full script (especially your javascript). and i never use  form helper yet. i cant guarantee this will work.

modify my script if this not work. i add comment to help you.
try this

PHP Code:
<?php echo form_close(); ?> //your last line
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
    $('form').submit(function(e){
        var formdata = $(this).serializeArray(); //serialize all your data in <form>
        
        //because ckeditor is create new dom, we cant get original textarea, so serializeArray cant catch them. so we need to push the value into formdata array
        //i assume you use ckeditor 4.x.x , 
        formdata.push({'name':'website_first_ad', 'value':CKEDITOR.instances.website_first_ad.getData()}); 
        formdata.push({'name':'website_second_ad', 'value':CKEDITOR.instances.website_second_ad.getData()}); 
        formdata.push({'name':'website_third_ad', 'value':CKEDITOR.instances.website_third_ad.getData()}); 
        var btn = $(this).find('input[name="mysubmit"]');
        var btnText = btn.val();
        $.ajax({
            url: '<?php echo site_url('admin/settings/index/'$item->id);?>', //change with your submit url, if im wrong
            type: 'post', //i hope your data is post, otherwise change it to get if you use GET
            data: formdata,
            dataType: 'json', //i recommend output as json not plain text
            beforeSend: function(){ //prevent double click when your script in process. and add styling on the button
                btn.prop('disabled', true).addClass('btn-warning').removeClass('btn-primary').val('wait');
            },
            success: function(d){
                if(d['status']=='success'){
                    alert(d['msg']);
                    btn.addClass('btn-success').removeClass('btn-warning').val('success');
                }else{
                    btn.addClass('btn-danger').removeClass('btn-warning').val('failed');
                    alert('Update data failer'); 
                    console.log(d); //for debug in console
                }
                setTimeout(function(){
                    btn.prop('disabled', false).addClass('btn-primary').removeClass('btn-success btn-danger').val(btnText);
                }, 2000);
            },
            error: function(d){
                btn.addClass('btn-danger').removeClass('btn-warning').val('error');
                alert('Something wrong happen'); 
                console.log(d.responseText); //for debug in console
                setTimeout(function(){
                    btn.prop('disabled', false).addClass('btn-primary').removeClass('btn-success btn-danger').val(btnText);
                }, 2000);
            }
        });
    });
});
</script> 

and your controller

PHP Code:
function index()
{
    ...
snippet...
    
//Create Message
    //$this->session->set_flashdata('success', 'Website setting has been updated'); comment this
    
$result = array('status'=>'success''msg'=>'Website setting has been updated');
    echo 
json_encode($result); //we throw response as json (javascript array)
    //Redirect to Users
    //redirect('admin/settings'); no need this anymore. you will not reload your page

Reply


Messages In This Thread
RE: Fetch and Update Data in same page CodeIgniter - by plonknimbuzz - 12-16-2017, 11:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB