2016-05-31 101 views
2

我想用ajax函數向mysql提交一個表單,但是我的嘗試給了我mysql表中的NULL結果。javascript函數成功返回數據

這裏是我的javascript:

function pesan() 
 
    { 
 
     email = $("#email").val(); 
 
     from_nama = $("#from_nama").val(); 
 
     from_phone = $("#from_phone").val(); 
 

 
     $.ajax 
 
     ({ 
 
      url : "<?php echo site_url('kirim/undangan')?>/", 
 
      type: "POST", 
 
      dataType: "JSON", 
 
      success: function(data) 
 
      {  
 
       $('#alert').show(); 
 
       $('#email'+data).html(data.email); 
 
       $('#from_nama'+data).html(data.from_nama); 
 
       $('#from_phone'+data).html(data.from_phone); 
 
      }, 
 
      error: function (jqXHR, textStatus, errorThrown) 
 
      { 
 
       alert('Error upload data'); 
 
      } 
 

 
     }); 
 
    }

而且形式:

 <h4 id="form">Data Personal</h4> 
 

 
          <div class="col-sm-4"> 
 
           <input type="email" class="form-control input-lg" id="email" name="email" placeholder="Email" required> 
 
          </div> 
 

 
          <div class="col-sm-4"> 
 
           <input type="text" class="form-control input-lg" id="from_nama" name="from_nama" placeholder="Nama" required> 
 
          </div> 
 

 
          <div class="col-sm-4"> 
 
           <input type="number" class="form-control input-lg" id="from_phone" name="from_phone" placeholder="Phone" required> 
 
          </div> 
 
          <div> 
 
           <input type="hidden" name="id" id="id" /> 
 
          </div> 
 
          <br> 
 
          <div class="row" align="center"> 
 
           
 
            <button id="pesan" type="button" class="btn btn-download btn-md" onclick=pesan()> 
 
            <span class="glyphicon glyphicon-send" aria-hidden="true" ></span> 
 
            Pesan 
 
            </button>

這裏的控制器:

function undangan() 
 
\t { \t 
 
\t \t \t $email   = $this->input->post('email'); 
 
\t \t \t $from_nama \t \t = $this->input->post('from_nama'); 
 
\t \t \t $from_phone \t \t = $this->input->post('from_phone'); 
 

 
\t \t \t $data_user = array(
 

 
\t \t \t \t 'email' \t \t \t \t \t \t \t => $email, 
 
\t \t \t \t 'name'  \t \t \t \t \t => $from_nama, 
 
\t \t \t \t 'phone' \t \t \t \t \t \t => $from_phone, 
 
\t \t \t \t 'status'   \t \t \t \t => '0', 
 
\t \t \t \t 'unique_id' \t   \t \t => uniqid() 
 

 
\t \t \t ); 
 

 
      $this->load->model('excel'); 
 
      
 
\t \t \t $this->excel->tambahuser($data_user); 
 
     
 
\t \t \t 
 
\t \t $this->load->view('kirimundangan.php',$data); 
 
\t }

型號:

function tambahuser($data_user) 
 
     { 
 
      $this->db->insert('request', $data_user); 
 
      $this->db->insert_id(); 
 
      
 
      foreach ($data_user as $key)  
 
      { 
 
       $data = array(
 
        
 
        'from_name'   => $this->input->post('from_nama'), 
 
        'from_phone'   => $this->input->post('from_phone') 
 
       ); 
 
      } 
 
       
 
     }

我想我在寫代碼的錯誤的成功裏面:功能(數據),任何幫助嗎?

+1

你不發送電子郵件,from_nama,from_phone到ajax?這就是爲什麼什麼都沒有保存在數據庫中,並添加您的服務器端代碼 – Thorin

+0

我嘗試上面的代碼,但它給了我NULL到表中的每個字段:( – may

+0

你的意思是電子郵件= $(「#email」)。val()eq to當你在java腳本上添加警報並添加服務器端代碼 – Thorin

回答

1

變化peson功能

function pesan() 
    { 
     var email = $("#email").val(), 
      from_nama = $("#from_nama").val(), 
      from_phone = $("#from_phone").val(); 

     $.ajax 
     ({ 
      url : "<?php echo site_url('kirim/undangan')?>/", 
      type: "POST", 
      dataType: "JSON", 
      data:{from_nama: from_nama, email: email, from_phone: from_phone}, 
      success: function(data) 
      {  
       $('#alert').show(); 
       $('#email'+data).html(data.email); 
       $('#from_nama'+data).html(data.from_nama); 
       $('#from_phone'+data).html(data.from_phone); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) 
      { 
       alert('Error upload data'); 
      } 

     }); 
    } 

,你期待一個JSON響應,所以你必須改變你的服務器端代碼,並從那裏,你必須使用JSON不是一個視圖返回數據。 我希望這能爲你工作

+0

現在數據上傳到數據庫,但爲什麼我得到錯誤警報? – may

+0

你必須使用json返回數據,例如$ json = json_encode($ data);然後echo $ json; – Thorin

+0

裏面的成功函數?或控制器? – may