更改codeigniter视图内容而不重新加载

时间:2013-08-06 12:08:46

标签: ajax codeigniter jquery codeigniter-2

我创建了一个电子邮件实用程序,用于显示收到电子邮件的电子邮件ID,其主题行和日期。每个电子邮件ID旁边都有一个复选框。无论点击哪个复选框,电子邮件都会发送到该电子邮件ID。我的问题是我想在同一页面上显示“消息发送成功”或错误消息而不重新加载。

我认为,有可能用ajax,但我不知道如何。以下是我的观点:

<?php echo form_open_multipart('accessmail/sendmail'); ?>
    <div>   
            //here I display my emails
            <div>
        <div class="table_wrap1">
                <div>Subject:</div>
                <div><input type="text" required="required" id="subject" name="subject"></div>
            </div>  
            <div class="table_wrap1">       
                <div>Email:</div>
                <div><input type="email" id="replyto" name="replyto"></div>
            </div>
            <div id="txteditor">
                <textarea name="textarea">  
                </textarea>
                <button name="submit" value="process" type="submit">Send</button>
            </div>

        <?php } ?> 

    </div>
</form>

我的控制器名称是“accessmail”,方法名称是“发送邮件”:

public function sendmail()
{
    $this->load->library('email');
    $count=$this->input->post('mailcount'); 
    $subject = $this->input->post('subject');
    $msg = $this->input->post('textarea');
    $replyto = $this->input->post('replyto');
    //variable for checking whether any email id is selected or not
    $status=0;
    //for sending email to checked email id's
    for($i=0;$i<$count;$i++)
    {
        // check whether the checkbox for a particular email id is checked or not
        if(!$this->input->post('emailid'.$i))
        {   

            continue;
        }
        else    
        {   
            //set the values for this email and send it
            $this->email->from('abc@abc.com', 'abc');
            $this->email->subject($subject);
            $this->email->message($msg);
            $idtosend = $this->input->post('emailid'.$i);
            $this->email->to($idtosend);
            if ($this->email->send())
            {
                $status++;
            }
        }
    }
    //for sending email to email id provide in textbox
    if(!empty($replyto))
    {
        $this->email->from('abc@abc.com', 'Abc');
        $this->email->subject($subject);
        $this->email->message($msg);
        $this->email->to($replyto);
        if ($this->email->send())
        {
            $status++;
        }
    }
    if($status>0)
    {
          //this message should be displayed on the same page along with other contents of the page
        echo "Your message has been sent";
    }
    if($status==0)
    {
        //this message should be displayed on the same page along with other contents of the page
        echo "Select any Email-id first";
    }

}   

我的问题是成功消息应该显示在保存页面内容的同一视图上。提前谢谢。

1 个答案:

答案 0 :(得分:0)

是的,这是典型的Ajax事情。但不幸的是,这有点太大了,无法给你答案(或者我太懒了,但你会从中获益最多,通过自己搞清楚)。 我的建议是,你学习jQuery和Ajax(如果你还不知道)。

对于您的问题,您特别希望通过Ajax将邮件发送到您的PHP函数sendmail()。这个函数要么返回一个Success-或Errormessage(周围有json_encode(),我就是这样做的)。然后,Ajax函数显示消息。

既没有测试也不应该是最终的解决方案...只是一个提示:

PHP函数:

<?php
function sendmail()
{
  $mails = $_POST['mails'];
  $countEmails = count($mails);
  $success = 0;
  foreach ($mails as $mail) {
    // Check and send mails here
    if ($this->email->send()) {
      $success++;
    }
  }
  if ($success < $countEmails) {
    echo json_encode('One or more E-Mails were not sent');
    return;
  }
  echo json_encode('E-Mails successfully sent');
}

Ajax函数(不要忘记包含jquery):

$('#id_of_your_send_mails_button').bind('click', function(evt) {
  evt.preventDefault();
  $.ajax({
    type: 'post',
    url: 'path/to/sendmail',
    dataType: 'json',
    data: {
      mails: $('#id_of_your_form').serialize();
    },
    success: function (data) {
      $('#id_of_your_status_div').html(data);
    }
  });
});

同样,这只是伪代码的东西,为了帮助你,但你必须自己学习。

希望有所帮助。

修改 您也可以尝试:

$('#id_of_your_send_mails_button').bind('click', function(evt) {
  evt.preventDefault();
  $.ajax({
    type: 'post',
    url: 'path/to/sendmail',
    dataType: 'json',
    data: {
      name: $('#id_of_your_name_field').val(),
      email: $('#id_of_your_name_field').val() // and so forth
    },
    success: function (data) {
      $('#id_of_your_status_div').html(data);
    }
  });
});