2017-09-28 48 views
0

需要显示图像,而我的函数“Mailcontroller/sendMail”正在发送电子邮件。我通过表单中的提交按钮从我的视图中将值传递给此函数。我尝试过在这里看到的其他例子,但我无法得到我需要的东西。当我的控制器执行功能时显示正在载入的图像

我的观点:

<div class="container" style="margin-right: 100px;"> 
 
    <div class="row"> 
 
     <div class="col-md-9 col-md-offset-3"> 
 
      <form class="form-horizontal" enctype='multipart/form-data' method="POST" action="<?=base_url();?>Cpersona/sendMail"> 
 
       <fieldset> 
 
        <div class="form-group"> 
 
         <div class="col-md-6"> 
 
          <label style="font-size: 15px; margin-left: 215px;">Destinatarios</label><br /> 
 
          <input type="button" name="agregardes" id="agregardes" onclick="llenarDestino();correos();" style="font-size: 15px; margin-left: 215px;" value="Agregar Destinatarios"> 
 
          <input style="margin-left: 215px;" id="email" name="email" type="text" class="form-control" required> 
 
          <label style="font-size: 15px; margin-left: 215px;">Asunto</label><br /> 
 
          <input style="margin-left: 215px;" type="text" class="form-control" name="asunto" id="asunto"> 
 
          <label style="font-size: 15px; margin-left: 215px;">Mensaje</label><br /> 
 
         </div> 
 
        
 
         <div class="col-md-9" style="float: right;"> 
 
          <textarea id="editor" name="mensaje"></textarea> 
 
         </div> 
 
        </div> 
 
        <section> 
 
         <div style="margin-left: 215px;"> 
 
         <p id="msg"></p> 
 
         <input type="file" id="file" name="file" /> 
 
         </div> 
 
        </section> 
 

 
        <div> 
 
         <br /> 
 
         <input style="margin-left: 215px;" class="btn btn-primary" type="submit" id="enviar" name="enviar" value="Enviar"> 
 
        </div> 
 
       </fieldset> 
 
      </form> 
 
     </div> 
 
    </div> 
 
</div>    
 

 
    <script type="text/javascript"> 
 
$(document).ready(function() {  
 
$('#enviar').click(function(){ 
 

 
    //Añadimos la imagen de carga en el contenedor 
 
    $('#content').html('<div><img src="../assets/images/loading.gif"/></div>'); 
 

 
    var page = $(this).attr('data');   
 
    var dataString = 'page='+page; 
 

 

 
    $.ajax({ 
 
     type: "GET", 
 
     url: "http://localhost/empresa/Cpersona/sendMail", 
 
     data: dataString, 
 
     success: function(data) { 
 
      //Cargamos finalmente el contenido deseado 
 
      $('#content').fadeIn(1000).html(data); 
 
     } 
 
    }); 
 
});    
 
});  
 
</script>

我控制器的摘录:

 public function sendMail{ 

       $data = array(
       'id_usuario' => $usuario, 
       'fecha' => $fecha, 
       'id' => $cliente 
       ); 

      $this->Modelo_datos->historial($data); 

      $this->email->set_newline("\r\n"); 
      $this->email->from('[email protected]'); 
      $this->email->to($value); 
      $this->email->subject($asunto); 
      $this->email->message($enviar); 

      //$path = set_realpath('./uploads/'); 
      //$adjunto = $path.$archivo; 

      if($this->email->send()) 
      { 

       $this->load->helper("file"); 
       delete_files('./uploads/'); 
       echo ("<SCRIPT LANGUAGE='JavaScript'> 
       window.alert('Correo Enviado!!') 
       window.location.href='verLista'; 
       </SCRIPT>"); 

      } 
      else 
      { 
       show_error($this->email->print_debugger()); 
       $this->load->helper("file"); 
       delete_files('./uploads/'); 
      } 

     } 

}

+0

表现出一定的代码! –

+0

请加你的代码 – Observer

+0

对不起,我忘记了,现在好了 –

回答

1

先添加图像(要被显示)与id加载器,并使用style =“displ隐藏它AY:无“;

loader = document.querySelector("#loader"); 
function sendMail() { 
    loader.style.display = "block"; 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     loader.style.display="none"; 
     // some thing here when email is sent 
    } 
}; 
xhttp.open("GET", "your_send_mail_file.php?send=true", true); 
xhttp.send(); 

}

和your_send_mail_file.php

<?php 
if (isset($_GET["send"]) && $_GET["send"] === "true") { 
    $test = new your_class(); // that_contain_the_sendMail function; 
    $test->sendMail(); 
} 
+0

谢谢你的回答,但在xhttp.open(“POST”,“yoursendmail.php?some_variables_in _post”,true);我没有变量在我的控制器在这里使用,只需$ this-> email-> sendmail()我希望在正在执行时观看并添加加载图像的功能,所以我需要这样做:xhttp.open(“POST”,“Mailcontroller/sendMail”,true); –

+0

编号ease添加您的php代码没有敏感变量 –

+0

一个例子pls,我新手上php –

相关问题