2014-01-23 41 views
0

我在使用ajax post方法提交帖子時遇到問題。它沒有給我任何錯誤消息似乎發佈請求,但模型沒有響應,所以它似乎插入數據庫中的行。CodeIgniter - Ajax Post

我不是最好的jQuery/JavaScript,但即時通訊絕望卡住,無法找到任何有關我的情況在stackoverflow上的任何東西。

控制器:spelers.php

public function historie() 
{ 
    if(isset($_SESSION['validated'])) 
    { 
     // Models 
     $this->Spelers_model->insertHistorie(); 
    } 
    else 
    { 
     redirect('/login'); 
    } 
} 

型號:spelers_model.php

public function insertHistorie() 
{ 
    // Variable  
    $spelerid = $this->input->post('spelerid'); 
$speler_id = $this->input->post('speler_id'); 
$speler_naam = $this->input->post('speler_naam'); 
$speler_slug = url_title($this->input->post('speler_naam'), 'dash', TRUE); 
$sum = strtotime ('+$actie day' , strtotime ($datum_ban)) ; 
$datum_unban = date ('Y-m-d H:i:s' , $sum); 
$datum_ban = $this->input->post('datum_ban'); 
$actie = $this->input->post('actie'); 
$reden = $this->input->post('reden'); 
$bewijs = $this->input->post('bewijs'); 
$extrainfo = $this->input->post('extrainfo'); 
$kayako_ticket = $this->input->post('kayako_ticket');  
$moderator = $this->input->post('moderator'); 

// Query Historie DB 
    $data = array(
    'spelerid' => $spelerid, 
     'speler_id' => $speler_id, 
     'speler_naam' => $speler_naam, 
    'speler_slug' => $speler_slug, 
    'datum_unban' => $datum_unban, 
    'datum_ban' => $datum_ban, 
     'actie' => $actie, 
     'reden' => $reden, 
     'bewijs' => $bewijs, 
     'extrainfo' => $extrainfo, 
     'kayako_ticket' => $kayako_ticket, 
     'moderator' => $moderator 
    ); 

$this->db->insert('speler_historie', $data); 

if($datum_unban === '') { 

} 
else 
{ 
// Query Banlist DB 
$data2 = array(
    'spelerid' => $spelerid, 
    'speler_id' => $speler_id, 
    'speler_naam' => $speler_naam, 
    'speler_slug' => $speler_slug, 
    'datum_unban' => $datum_unban, 
    'datum_ban' => $datum_ban, 
    'actie' => $actie, 
    'reden' => $reden, 
    'bewijs' => $bewijs, 
    'extrainfo' => $extrainfo, 
    'kayako_ticket' => $kayako_ticket, 
     'moderator' => $moderator, 
    'status' => '0' 
); 

$this->db->insert('banlist', $data2); 
} 
} 

查看:view.php

<!-- historie --> 
    <script type="text/javascript"> 
     $(function() { 

      var 
      spelerid = $("#spelerid"), 
      speler_id = $("#speler_id"), 
      speler_naam = $("#speler_naam"), 
      datum_ban = $("#datum_ban"), 
      actie = $("#actie"), 
      reden = $("#reden"), 
      bewijs = $("#bewijs"), 
      extrainfo = $("#extrainfo"), 
      kayako_ticket = $("#kayako_ticket"), 
      moderator = $("#moderator"), 
      allFields = $([]).add(spelerid).add(speler_id).add(speler_naam).add(datum_ban).add(actie).add(reden).add(bewijs).add(extrainfo).add(kayako_ticket).add (moderator), 
      tips = $(".validateTips"); 

      function updateTips(t) { 
       tips 
        .text(t) 
        .addClass("ui-state-highlight"); 
       setTimeout(function() { 
        tips.removeClass("ui-state-highlight", 1500); 
       }, 500); 
      } 

      function checkLength(o, n, min, max) { 
       if (o.val().length > max || o.val().length < min) { 
        o.addClass("ui-state-error"); 
        updateTips("Kan geen lege " + n + " toevoegen."); 
        return false; 
       } else { 
        return true; 
       } 
      } 

      $("#dialog-form").dialog({ 
       resizable: false, 
       autoOpen: false, 
       height: 350, 
       width: 600, 
       modal: true, 
       buttons: { 
        "Toevoegen": function() { 
         var bValid = true; 
         allFields.removeClass("ui-state-error"); 

         bValid = bValid && checkLength(bewijs, "Bewijs", 3, 1020); 
         bValid = bValid && checkLength(kayako_ticket, "Kayako Ticket", 3, 1020); 

         if (bValid) { 

          $.ajax({ 
           type: "POST", 
           url: "<?php echo base_url(); ?>index.php/spelers/<?php echo $speler['slug']; ?>/historie", 
           data: { 
            spelerid: $('#spelerid').val(), 
            speler_id: $('#speler_id').val(), 
            speler_naam: $('#speler_naam').val(), 
            datum_ban: $('#datum_ban').val(), 
            actie: $('#actie').val(), 
            reden: $('#reden').val(), 
            bewijs: $('#bewijs').val(), 
            extrainfo: $('#extrainfo').val(), 
            kayako_ticket: $('#kayako_ticket').val(), 
            moderator: $('#moderator').val() 
           }, 
           success: function(data){ 
            location.reload(); 
           } 
          }); 
         } 
        }, 
        Cancel: function() { 
         $(this).dialog("close"); 
        } 
       }, 
       close: function() { 
       allFields.val("").removeClass("ui-state-error"); 
       } 
      }); 

      $("#create-historie") 
       .click(function() { 
        $("#dialog-form").dialog("open"); 
       }); 

     }); 
    </script> 
      <?php 
    $moderator = $_SESSION['username']; 
    $datum_ban = date("Y-m-d H:i:s"); 
    ?> 

    <div id="dialog-form" title="Ban <?php echo $speler['speler_naam']; ?> | Toevoegen"> 
     <div class="clear"></div> 
     <form> 

      <table width="100%"> 
      <tr> 
      <td width="15%"><?php echo form_label('Dagen: *', 'actie'); ?></td> 
      <td width="50%"> 
      <?php echo form_dropdown('actie', $acties, NULL, 'id="actie" onchange="load_dropdown_content($(\'#actie\'), this.value)"'); ?> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo form_label('Reden: *', 'reden'); ?></td> 
      <td> 
      <?php echo form_dropdown('reden', $redenen, NULL, 'id="reden" onchange="load_dropdown_content($(\'#reden\'), this.value)"'); ?> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo form_label('Extra Info: *', 'extrainfo'); ?></td> 
      <td> 
      <?php echo form_dropdown('extrainfo', $extrainfos, NULL, 'id="extrainfo" onchange="load_dropdown_content($(\'#extrainfo\'), this.value)"'); ?> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo form_label('Bewijs: *', 'bewijs'); ?></td> 
      <td><input type="text" name="bewijs" id="bewijs" value="" class="text ui-widget-content ui-corner-all" style="width: 300px; height: 15px;" /></td> 
      </tr> 
      <tr> 
      <td><?php echo form_label('Kayako Ticket: *', 'kayako_ticket'); ?></td> 
      <td><input type="text" name="kayako_ticket" id="kayako_ticket" value="" class="text ui-widget-content ui-corner-all" style="width: 100px; height: 15px;" /></td> 
      </tr> 
      <tr> 
      <td colspan="2"> 
      <!-- hidden --> 
      <input type="hidden" name="spelerid" id="spelerid" value="<?php echo $speler['id']; ?>" class="text ui-widget-content ui-corner-all" /> 
      <input type="hidden" name="speler_id" id="speler_id" value="<?php echo $speler['speler_id']; ?>" class="text ui-widget-content ui-corner-all" /> 
      <input type="hidden" name="speler_naam" id="speler_naam" value="<?php echo $speler['speler_naam']; ?>" class="text ui-widget-content ui-corner-all" /> 
      <input type="hidden" name="datum_ban" id="datum_ban" value="<?php echo $datum_ban; ?>" class="text ui-widget-content ui-corner-all" /> 
      <input type="hidden" name="moderator" id="moderator" value="<?php echo $moderator; ?>" class="text ui-widget-content ui-corner-all" /> 
      <!-- /hidden --> 
      </td> 
      </tr> 
      </table> 

     </form> 
     <p class="validateTips">* Verplicht veld.</p> 
    </div> 
    <!-- /historie --> 
+0

不相關的問題,但你應該使用'$此 - > input-> post('some_data');'而不是'mysql_real_escape_string($ _ POST [])' – Guicara

+0

你說:'模型沒有迴應'。任何調試?你確定'if(isset($ _ SESSION ['validated']))'返回true嗎? – Guicara

+0

你可以在你的historie()函數的頂部放一些調試代碼來查看它是否被調用? - 也許var_dump($ _ SESSION ['validated'])可能是一個好的開始。另外,ajax發佈什麼網址?我認爲你們之間的slu//和/歷史可能會造成問題。除非你在你的routes.php配置文件中處理這個。 – charliefortune

回答

0

我懷疑ajax url無效。你已經把它作爲

<?php echo base_url(); ?>index.php/spelers/<?php echo $speler['slug']; ?>/historie" 

,這將給像

yourdomain.com/index.php/spelers/your-slug/historie

笨路由會嘗試和路由這(這很好),然後your-slug()方法(它不好,因爲它可能不存在。)。

如果你想調用你的spelers控制器中的historie()方法,你需要你的url出來這樣的東西;

yourdomain.com/index.php/spelers/historie

如果你想塞在去另一個參數,你需要或者把它放在一個AJAX網址的結尾,和訪問通過URI class的uri-> segment()方法,或者將它作爲ajax請求的數據部分中的POSTed參數傳遞。

+0

謝謝你解決了這個問題! – Steven4Hawkes

0

可以波紋管嘗試的例子:

在控制器

public function historie() 
{ 
    if(isset($_SESSION['validated'])) 
    { 
     // Models 
     $this->Spelers_model->insertHistorie(); 
     echo array('error_code' => 0); 
    } 
    else 
    { 
     echo array('error_code' => 1); 
    } 
} 

鑑於

$.ajax({ 
    type: "POST", 
    dataType: "json", 
    beforeSend: function() {}, 
    complete: function() {}, 
    url: "abc.php", 
    data: {}, 
    success: function(data){ 
     console.log(data); // debug data 
      if (data.error_code === 0){ 
       location.reload(); 
      } else { 
       window.location.href="/login"; 
      } 
    }, 
    error: function(error){ 
     alert('Fail ' + error); 
    } 
});