Foros del Web » Programando para Internet » PHP » CodeIgniter »

Recoger en la vista el post ajax

Estas en el tema de Recoger en la vista el post ajax en el foro de CodeIgniter en Foros del Web. Quiero recoger en una vista php lo que obtiene el post del ajax Este es mi controlador Código PHP: <?php  if ( !  defined ( 'BASEPATH' )) exit( ...
  #1 (permalink)  
Antiguo 01/03/2016, 16:49
 
Fecha de Ingreso: marzo-2016
Ubicación: San Luis Potosi
Mensajes: 1
Antigüedad: 8 años, 1 mes
Puntos: 0
Recoger en la vista el post ajax

Quiero recoger en una vista php lo que obtiene el post del ajax

Este es mi controlador
Código PHP:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Sales extends CI_Controller {
    function 
__construct(){
        
parent::__construct();
        
$this->load->helper('form');
        
$this->load->model('sales_m');
    }


    function 
index(){
             
$saleidsel $this->input->post('selectsaleid',TRUE);
             
$datefromsel $this->input->post('datesalefrom',TRUE);
             
$datetosel $this->input->post('datesaleto',TRUE);
             
$partnersel $this->input->post('PartnerSelect',TRUE);
             
$linersel $this->input->post('LinerSelect',TRUE);
             
$closersel $this->input->post('Closerseleccionar',TRUE);
             
$totalF $this->input->post('totalFrom',TRUE);
             
$totalT $this->input->post('totalTo',TRUE);
             
$data['sales'] = $this->sales_m->getSalesReports($saleidsel); 
             
$data['partner'] = $this->sales_m->getPartners();
             
$data['sales_agent_commision_liner'] = $this->sales_m->getLinerAgents();
             
$data['sales_agent_commision_closer'] = $this->sales_m->getCloserAgents();
             
$data['sales_id'] = $this->sales_m->getSalesIds();
             
$this->load->view('salesv'$data$saleidsel $this->input->post('selectsaleid',TRUE));  
            
//echo $saleidsel, $datefromsel, $datetosel, $partnersel, $linersel, $closersel, $totalF, $totalT;
    
//end index
    
function save(){
       
$saleidsel $this->input->post('selectsaleid',TRUE);
       
$datefromsel $this->input->post('datesalefrom',TRUE);
       
$datetosel $this->input->post('datesaleto',TRUE);
       
$partnersel $this->input->post('PartnerSelect',TRUE);
       
$linersel $this->input->post('LinerSelect',TRUE);
       
$closersel $this->input->post('Closerseleccionar',TRUE);
       
$totalF $this->input->post('totalFrom',TRUE);
       
$totalT $this->input->post('totalTo',TRUE);
       
$data['sales'] = $this->sales_m->getSalesReports($saleidsel); 
       
$data['partner'] = $this->sales_m->getPartners();
       
$data['sales_agent_commision_liner'] = $this->sales_m->getLinerAgents();
       
$data['sales_agent_commision_closer'] = $this->sales_m->getCloserAgents();
       
$data['sales_id'] = $this->sales_m->getSalesIds();
       
$this->load->view('salesv'$data);      
    }
// end save
 
// end controller
Está es mi vista
Código PHP:
<!DOCTYPE html>
<html>
   <head>  
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
      <!-- Optional theme -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">   
      <!-- jQuery -->
      <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>   
      <!-- Latest compiled and minified JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
      <!-- Ajax -->
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <title>Sales Reports</title>
      <script type="text/javascript">
         $(document).ready(function(){
            $("#consult").click(function(){
                  $.ajax({
                  url: '<?=base_url();?>' + 'index.php/sales/save',
                  type: "POST",
                  data: $('#form').serialize(),
                  success: function(data){
                     alert(data);

                     document.location=('sales/save/');
                  }
                  });
                  return false;
            })
           });
      </script>
   </head>
   <body>
      <table class="table table-striped " border="1" >
         <tr>
            <td>
               Sales id
            </td>
            <td>
               Date
            </td>
            <td>
               Partener id
            </td>
            <td>
               Liner id
            </td>
            <td>
               Closer id
            </td>
            <td>
               Total
            </td>
         </tr>
         <tr>
            <td>
            <form id="form">
                  <select class="form-control form-group-margin" id="selectsaleid" name = "selectsaleid"> 
               <option selected = "selected">2</option>
                  <?php foreach ($sales_id->result() as $sale ) { ?>
                     <option value = "<?= $sale->id?>"><?= $sale->id?></option>
                  <?php ?> 
               </select>  
            </td>
            <td>
                  From: <input size="20" type="date" id="datesalefrom" name="datesalefrom" min="2000-01-01">
                  <br>To: &nbsp; &nbsp; <input size="20" type="date" id="datesaleto" name="datesaleto" min="2000-01-01">
            </td>
            <td>
            
                  <select class="form-control form-group-margin"  id="PartnerSelect" name = "PartnerSelect"> 
                  <option selected = "selected">Partener</option>
                  <?php foreach ($partner->result() as $part ) { ?>
                     <option value = "<?= $part->id?>"><?= $part->id?></option>
                  <?php ?> 
               </select>
            </td>
            <td>   
                  <select class="form-control form-group-margin" name="LinerSelect" id="LinerSelect">
                  <option selected="selected">Liner</option>
                     <?php foreach ($sales_agent_commision_liner->result() as $liner ) { ?>          
                        <option value = "<?= $liner->sales_agent_id?>"><?= $liner->sales_agent_id?></option> 
                     <?php ?>
               </select>
            </td>
            <td>
                  <select class="form-control form-group-margin"id="Closerseleccionar"  aria-haspopup="true"name ="Closerseleccionar">
                     <option selected="selected">Closer</option>
                     <?php foreach ($sales_agent_commision_closer->result() as $closer ) { ?>          
                        <option value = "<?= $closer->sales_agent_id?>"><?= $closer->sales_agent_id?></option> 
                     <?php ?> 
                  </select>      
            </td>
            <td>
               From: <input id="totalFrom" name="totalFrom" type="number"></input> <br>
               To: &nbsp; &nbsp; <input id="totalTo" name="totalTo" size="6" type="number"></input>
           <input type="button"  value="consult" id="consult"></input> 
            </td>
            </form>
         </tr>
          <?php foreach ($sales->result() as $sale ) { ?>
         <tr>       
            <td>
               <?= $sale->id?>
            </td>
            <td>
               <?= $sale->date?>
            </td>
            <td>
               <?= $sale->partener_id?>
            </td>
            <td>
               <?= $sale->sales_agent_liner_id?>
            </td>
            <td>
               <?= $sale->sales_agent_closer_id?>
            </td>
            <td>
               <?= $sale->total?>
            </td>
         </tr>
         <?php ?>
      </table>
   </body>
</html>
Y este es mi modelo
Código PHP:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class 
Sales_m extends CI_Model {
    function 
__construct(){
        
parent::__construct();
        
$this->load->database();
    }
    
    function 
getSalesReports($saleidsel){
      
//  echo $saleidsel;
        
$this -> db -> select ('id, partener_id, total, sales_agent_liner_id, sales_agent_closer_id, date');
        
$this -> db -> from('sales');
        
$this -> db -> where('sales.id'$saleidsel);
        
$query $this -> db -> get();
        if(
$query->num_rows() > 0) return $query;
        else return 
false;
    }

    function 
getSalesIds(){
        
$this -> db -> select('id');
         
$this -> db -> from('sales');         
         
$query $this -> db -> get();
         if(
$query->num_rows() > 0) return $query;
        else return 
false;   
    }
    function 
getPartners(){
        
$query $this->db->get('partner');
        if(
$query->num_rows() > 0) return $query;
        else return 
false;
    }

    function 
getLinerAgents(){
        
$this -> db -> select('sales_agent_id');
         
$this -> db -> from('sales_agent_commision');         
      
$this -> db -> where('commision_id'1);
         
$query $this -> db -> get();
         if(
$query->num_rows() > 0) return $query;
        else return 
false;   
    }

    function 
getCloserAgents(){
        
$this -> db -> select('sales_agent_id');
         
$this -> db -> from('sales_agent_commision');         
      
$this -> db -> where('commision_id'2);
         
$query $this -> db -> get();
         if(
$query->num_rows() > 0) return $query;
        else return 
false;   
    }

}

?>

Lo que obtengo en el alert del post al hacer click en consult es justo la vista que deseo ver, pero al mandar llamar a la función save, está no me regresa los valores del post y marca erronea la consulta y por tanto no muestra la tabla

Etiquetas: ajax, post, recoger, vista
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:24.