2013-01-23 13 views
0

我成功地能夠使用ajax後保存輸入值,但它不會加載下一頁。CodeIgniter:jQuery發佈和加載下一頁

客戶端代碼:

<script type="text/javascript"> 
    function notifyEmail() { 

     var form_data = { 
        'email':$('#inviteEmail').val() 
       }; 
     alert($('#inviteEmail').val()); 
     $.ajax({ 
        url: "<?php echo site_url('main/email_invites'); ?>", 
        type: 'POST', 
        data: form_data, 
        success: function(msg) { 
        return true; 
        } 
      }); 
    } 
    </script> 

<input type='text' id='inviteEmail' placeholder='Enter email'/> 
     <a href='#' name='email' id='invite-all' onclick='notifyEmail();' class='btn'>Notify Me</a> 

控制器代碼:

function email_invites() 
    { 
     $this->load->model('emailInvites'); 
     if($query = $this->emailInvites->saveEmailInvite()) 
     { 
       $this->load->view('emailInvites'); 

     } 
    } 
+1

一件事,你有'$查詢='而不是' $ query =='。那是故意的嗎?另一方面,您通過AJAX發佈,這意味着您必須在POST完成後(在您的「成功」函數中)使用Javascript加載下一頁。現在你只是將一堆HTML返回給你的成功函數,你不會重定向用戶。 – glomad

+0

作業標誌是一個錯字。感謝您指出。如何重定向用戶使用jQuery ajax? –

+0

簡單的方法:取代'$ this-> load-> view(...',只是回顯一些字符串,如'ok',然後在你的成功中,確保'msg' =='' ok''。如果是的話,'window.location.href ='new_url''。如果沒有,提醒用戶不知道該帖子失敗。 – glomad

回答

1

我要做的就是下面這可以幫助你

<script type="text/javascript"> 
    function notifyEmail() { 

     var form_data = { 
        'email':$('#inviteEmail').val() 
       }; 
     alert($('#inviteEmail').val()); 
     $.ajax({ 
        url: "<?php echo site_url('main/email_invites'); ?>", 
        type: 'POST', 
        data: form_data, 
        dataType:'json', 
        success: function(msg) { 
        if(msg.response) 
         //load the content in the body or wherever you want 
         jQuery('body').load("<?php echo site_url('main/email_invites/show'); ?>"); 
        else 
         console.log("Opps, Something wrong happend"); 
        return true; 
        } 
      }); 
    } 
    </script> 


    <input type='text' id='inviteEmail' placeholder='Enter email'/> 
    <a href='#' name='email' id='invite-all' onclick='notifyEmail();' class='btn'>Notify Me</a> 

在控制器

function email_invites($action = 'process') 
    { 
     if($action == 'process') 
     { 
       $this->load->model('emailInvites'); 
       $query['response'] = $this->emailInvites->saveEmailInvite(); 
       echo json_encode($query); 


     } 
     else 
     { 
       $this->load->view('emailInvites'); 
     } 
    } 
0

你不需要爲了呼應URL,只是用它在這樣的Ajax調用,

$.ajax({ 
       url: 'main/email_invites', 
       type: 'POST', 
       data: form_data, 
       success: function(msg) { 
       return true; 
       } 
     }); 

如果site_url方法返回所需的url,那麼ajax調用需要是,

$.ajax({ 
       url: "<?php echo site_url('main/email_invites'); ?>", 
       type: 'POST', 
       data: form_data, 
       success: function(msg) { 
       return true; 
       } 
     });