【问题标题】:Ajax call isn't update my rowsAjax 调用不会更新我的行
【发布时间】:2013-10-12 03:11:27
【问题描述】:

我想用字符的 id 更新我的数据库,但是当我将它们放入插槽时,它不会更新我希望它更新的行。我的问题是你能指出我如何正确编码或修复错误的正确方向吗?

function updateTeam(){

var team = '', slot = [];
if($('input[name=s0]').val()!=""){
    slot.push($('input[name=s0]').val());
}
if($('input[name=s1]').val()!=""){
    slot.push($('input[name=s1]').val());
}
if($('input[name=s2]').val()!=""){
    slot.push($('input[name=s2]').val());
}
$.each(slot, function(i,e){
    if(i == 0) team = e;
    else team = team + ',' + e;
});
$.ajax({

       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'setTeam', i: team},
       dataType : 'json',
       success :  function(data) {
        if(data.error){
            errorMessage('Error: ' + data.error, data.error, data.error);
        }
    }
});
}

php

function clean($content) {
    $content = mysql_real_escape_string(htmlspecialchars($content));
    return $content;
}
//Update the user team.
if (isset($_POST['f']) && $_POST['f'] == 'updateTeam')  {

if (isset($_POST['s0'])) {
        $cid1 = $secure->clean($_POST['s0']);
    } else {
        $cid1 = '1';
    }

if (isset($_POST['s1'])) {
        $cid2 = $secure->clean($_POST['s1']);
    } else {
        $cid2 = '2';
    }

if (isset($_POST['s2'])) {
        $cid1 = $secure->clean($_POST['s2']);
    } else {
        $cid1 = '3';
    }

$updateTeam = $db->query("UPDATE accounts SET cid1 = '$cid1', cid2 = '$cid2', cid3 = '$cid3' WHERE id = '$id'");
}

当我使用 Google Chrome 检查元素时,它显示 i:1,5,2。显示我将如何更新我的行,以便“i”中的 1=$cid1、5=$cid2 和 2=cid3,是我的 php 代码错误吗? html:

 <div id="droppable_slots" class="current_team">
                    <div class="slot 1">1</div>
                    <input type="hidden" name="s0" value="10">
                    <div class="slot 2">2</div>
                    <input type="hidden" name="s1" value="7">
                    <div class="slot 3">3</div>
                    <input type="hidden" name="s2" value="3">
                </div>

【问题讨论】:

    标签: php jquery html ajax


    【解决方案1】:

    由于您要发送带有 i 键的 csv,因此您需要通过在 , 上展开从 $_POST['i'] 获取值。所以你的代码可以更新为类似-

    //Update the user team.
    if (isset($_POST['f']) && $_POST['f'] == 'updateTeam')  {
    
    //Explode the i post
    if (isset($_POST['i'])) { 
            $vals = explode("," , $_POST['i'] );
        }
    
    if (isset($vals[0])) {
            $cid1 = $secure->clean($vals[0]);
        } else {
            $cid1 = '1';
        }
    
    if (isset($vals[1])) {
            $cid2 = $secure->clean($vals[1]);
        } else {
            $cid2 = '2';
        }
    
    if (isset($vals[2])) {
            $cid1 = $secure->clean($vals[2]);
        } else {
            $cid1 = '3';
        }
    
    $updateTeam = $db->query("UPDATE accounts SET cid1 = '$cid1', cid2 = '$cid2', cid3 = '$cid3' WHERE id = '$id'");
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      相关资源
      最近更新 更多