在ajax调用中,2d阵列分崩离析

时间:2013-12-12 12:31:55

标签: javascript php ajax multidimensional-array

我在JS中有一个2D数组。

我在ajax它之前警告数组并且数组完好无损并且一切都很好。

当我ajax它,我得到一个错误,所以我现在ajax'd数组到一个页面,只运行一个var转储和数组尾声中断并失去结构和数据

继承人js

  $.ajax({
       type: "POST",            
       url: "ajax/updateElements.php",          
       data: {outerarray:outerarray},                       
       success: function(data){
          alert("data="+data);
        },
            error: function(data){
               alert("error");
    }
     });    

php

 <?php
   var_dump($_POST['outerarray']);
 ?>

和返回的数据

         data=array(40) {
          [0]=>
          array(20) {
            [0]=>
            string(2) "47"
            [1]=>
            string(2) "46"
            [2]=>
            string(2) "-1"
            [3]=>
            string(3) "100"
            [4]=>
            string(2) "35"
            [5]=>
            string(5) "shape"
            [6]=>
            string(190) "shape"
            [7]=>
            string(1) "0"
            [8]=>
            string(1) "0"
            [9]=>
            string(5) "solid"
            [10]=>
            string(3) "000"
            [11]=>
            string(1) "0"
            [12]=>
            string(0) ""
            [13]=>
            string(1) "1"
            [14]=>
            string(2) "st"
            [15]=>
            string(3) "206"
            [16]=>
            string(2) "54"
            [17]=>
            string(1) "0"
            [18]=>
            string(1) "1"
            [19]=>
            string(1) "F"             
          }...until
          [12]=>
          array(20) {
            [0]=>
            string(2) "33"
            [1]=>
            string(3) "179"
            [2]=>
            string(3) "376"
            [3]=>
            string(3) "578"
            [4]=>
            string(3) "203"
            [5]=>
            string(4) "text"
            [6]=>
            string(933) "text"
            [7]=>
            string(2) "11"
            [8]=>
            string(1) "1"
            [9]=>
            string(5) "solid"
            [10]=>
            string(3) "000"
            [11]=>
            string(1) "0"
            [12]=>
            string(0) ""
            [13]=>
            string(1) "1"
            [14]=>
            string(0) ""
            [15]=>
            string(3) "206"
            [16]=>
            stri

那就是它的尾声

任何线索? 在这个

上,我一直在墙上撞了一个星期

继承PDO代码

      $arraycount=count($_POST['outerarray']);
            for($m=0; $m<$arraycount; $m++){            

                $box_id = $_POST['outerarray'][$m][0];
                $top= $_POST['outerarray'][$m][1];
                $left  = $_POST['outerarray'][$m][2];
                $width = $_POST['outerarray'][$m][3];
                $height = $_POST['outerarray'][$m][4];
                $type = $_POST['outerarray'][$m][5];
                $content = str_replace("\n", "<br>",$_POST['outerarray'][$m][6]);   
                $zindex = $_POST['outerarray'][$m][7];
                $borderwidth = $_POST['outerarray'][$m][8];
                $borderstyle = $_POST['outerarray'][$m][9];
                $bordercolor = $_POST['outerarray'][$m][10];
                $padding = $_POST['outerarray'][$m][11];
                $weblink = $_POST['outerarray'][$m][12];
                $opacity = $_POST['outerarray'][$m][13];
                $shapetype = $_POST['outerarray'][$m][14];
                $page_ref = $_POST['outerarray'][$m][15];
                $site_ref = $_POST['outerarray'][$m][16];
                $deletedelement = $_POST['outerarray'][$m][17];
                $allpages = $_POST['outerarray'][$m][18];
                $pageSection = $_POST['outerarray'][$m][19];    

                $stmt = $db->prepare("SELECT * FROM site_content WHERE site_ref=:site_ref AND page_ref=:page_ref AND box_id=:box_id");
                $stmt->execute(array(':site_ref' => $site_ref, ':page_ref' => $page_ref, ':box_id' => $box_id));
                $row_count = $stmt->rowCount(); 
                if($row_count==0){  
                $stmt = $db->prepare("INSERT INTO site_content(page_ref, site_ref, box_type, box_id, box_top, box_left, box_width, box_height, content, weblink , zindex, border_width, border_colour, border_style, padding, image_opacity, shapetype, allPages, pageSection) VALUES(:field1,:field2,:field3,:field4,:field5,:field6,:field7,:field8,:field9,:field10,:field11,:field12,:field13,:field14,:field15,:field16,:field17,:field18,:field19)");             
                    $stmt->execute(array(':field1' => $page_ref, ':field2' => $site_ref, ':field3' => $type, ':field4' => $box_id, ':field5' => $top, ':field6' => $left, ':field7' => $width, ':field8' => $height, ':field9' => $content, ':field10' => $weblink, ':field11' => $zindex, ':field12' => $borderwidth, ':field13' => $bordercolor, ':field14' => $borderstyle, ':field15' => $padding, ':field16' => $opacity, ':field17' => $shapetype, ':field18' => $allpages, ':field19' => $pageSection));
                    $affected_rows = $stmt->rowCount();                 

                    if($affected_rows !=0){         
                        echo "<br>content added succesfully";
                    }else{
                        echo "<br>no content added";
                    }
                }else{  
                    if($deletedelement=="1"){
                        $stmt = $db->prepare("DELETE FROM site_content WHERE site_ref=:site_ref AND page_ref=:page_ref AND box_id=:box_id");
                        $stmt->execute(array(':site_ref' => $site_ref, ':page_ref' => $page_ref, ':box_id' => $box_id));
                        $stmt->execute();
                        $affected_rows = $stmt->rowCount();

                        if($affected_rows !=0){         
                            echo "<br>content deleted succesfully";
                        }else{
                            echo "<br>no content deleted";
                        }   
                    }else{
                        if($pageSection==''){ $pageSection="C";}     
                        $stmt = $db->prepare("UPDATE site_content SET box_type=:box_type, box_top=:box_top, box_left=:box_left, box_height=:box_height, box_width=:box_width, content=:content, weblink=:weblink, zindex=:zindex, image_opacity=:image_opacity, border_width=:border_width, border_colour=:border_colour, border_style=:border_style, padding=:padding, shapetype=:shapetype, allPages=:allPages, pageSection=:pageSection WHERE site_ref=:site_ref AND page_ref=:page_ref AND box_id=:box_id");
                        $stmt->execute(array(':box_type' => $type, ':box_top' => $top, ':box_left' => $left, ':box_height' => $height, ':box_width' => $width, ':content' => $content, ':weblink' => $weblink, ':zindex' => $zindex, 'image_opacity' => $opacity, ':border_width' => $borderwidth, ':border_colour' => $bordercolor, 'border_style' => $borderstyle, 'padding' => $padding, 'shapetype' => $shapetype, 'allPages' => $allpages, 'pageSection' =>$pageSection, ':site_ref' => $site_ref, ':page_ref' => $page_ref, ':box_id' => $box_id));
                        $affected_rows = $stmt->rowCount();                        
                        if($affected_rows !=0){         
                            echo "<br>content updated succesfully";
                        }else{
                            echo "<br>no content updated";
                        }
                    }
                }                       

            }

2 个答案:

答案 0 :(得分:1)

好的,显然这是outerarray调用中$.ajax()数据对象序列化的问题(可能是一个旧的jQuery版本?)。为了避免任何问题,您可以将数据编码为JSON字符串并在PHP脚本中对其进行解码,基本上如下所示:

JS:

$.ajax({
    type: "POST",
    url: "ajax/updateElements.php",
    data: {outerarray: JSON.stringify(outerarray)},
    success: function(data) {
        alert("data="+data);
    },
    error: function(data){
        alert("error");
    }
});

PHP:

$outerarray = json_decode($_POST['outerarray']);
...
$arraycount = count($outerarray);
for ($m=0; $m<$arraycount; $m++) {            
    $box_id = $outerarray[$m][0];
    $top = $outerarray[$m][1];
    ...

答案 1 :(得分:0)

检查php.ini设置中的post_max_size,并根据POST的大小检查它