从客户端javascript页面接收数组

时间:2011-06-26 01:10:53

标签: php javascript html ajax

我正在尝试将数组的值发送到服务器。在服务器端php文件放回一个echo,我可以看到该值已经以 - [value1,value2]等形式传递。但是在服务器端的php文件中,当我试图使用Php数组检索值时,它无法正常工作。

这是我的客户端JS功能 -

function saveData(){

var req;
try{
// Opera 8.0+, Firefox, Safari
    req = new XMLHttpRequest();
    //document.write("2");
    }catch (e){
    // Internet Explorer Browsers
        try{
            req = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try{
               req = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
           // Something went wrong
               alert("Your browser broke!");
               return false;
           }
        }
   }

    req.onreadystatechange = function() {
   //Is request finished? Does the requested page exist
   if(req.readyState==4 && req.status==200) {   
        alert(req.responseText);
        //document.getElementById('editgamediv').innerHTML = req.responseText;
    }
}

var table = document.getElementById('POITable');    
var rowCount = table.rows.length; 
var latArray = []
var lngArray = []
var id = []
var id2=[]
var levelname = document.getElementById('leveltext');
//alert(rowCount);

for(var i=1; i< rowCount; i++){
    latArray[i] = table.rows[i].getElementsByTagName('input')[0].value;
    lngArray[i] = table.rows[i].getElementsByTagName('input')[1].value;
    id[i] = levelname+i;
    id2[i] = levelname+i+i; 

    //alert(lngArray[i]);
}


var queryString= "?latArray="+latArray;
queryString+="&lngArray="+lngArray+"&id="+id+"&id2="+id2+"&levelname="+levelname;
req.open("GET","http://aiworker2.usask.ca/PasswARGUI1/addLevel_server.php"+queryString,true)  //true indicates ASYNCHRONOUS
req.send(null);
}

服务器端Php -

$dbhost = myHost; 
$dbuser = my User name;
$dbpass = my Password;
$dbname = "testGame";

    //Connect to MySQL Server
$link= mysql_connect($dbhost, $dbuser, $dbpass);


if (!$link) {
    die('Could not connect: ' . mysql_error());
    echo 'Go n Die';
}
else{
    echo 'Connected successfully';
}
    mysql_select_db($dbname);
    //$latArray = array();
    $latArray=$_GET['latArray'];
    $lngArray[]=$_GET['lngArray'];
    $id[]=$_GET['id'];
    $id2[]=$_GET['id2'];
    $levelname = $_GET['levelname'];

    echo $latArray;
    $printlatArray[0] = $latArray[0];

    echo $printlatArray;
    for($num = 0; $num< count($latArray) ; $num++){

        $sql_zones1 = "INSERT INTO Zones (zone_id, latitude, longitude, tag, relativeAlt, alt, dimension) VALUES
          ('{$id[$num]}', '{$latArray[$num]}', '{$lngArray[$num]}', '{$levelname[$num]}', '0', '10', '3')";

        $sql_zones2 = "INSERT INTO Zones (zone_id, latitude, longitude, tag, relativeAlt, alt,  dimension) VALUES
          ('{$id2[$num]}', '{$latArray[$num]}', '{$lngArray[$num]}', '{$levelname[$num]}', '50', '80','2')";

        $sql_transform1 = "INSERT INTO Transform (poiID, rel, angle, scale) VALUES ('{$id[$num]}', 'true', '0', '3')";
        $sql_transform2 = "INSERT INTO Transform (poiID, rel, angle, scale) VALUES ('{$id2[$num]}', 'true', '0', '4')";

        $sql_object1 = "INSERT INTO Object (poiID, size) VALUES ('{$id[$num]}', '3')";
        $sql_object2 = "INSERT INTO Object (poiID, size) VALUES ('{$id2[$num]}', '3')";


    }

    if (!$result_zones1 || !$result_transform1 || !$result_object1 ||!$result_zones2 || !$result_transform2 || !$result_object2) {
    die('Invalid query: ' . mysql_error());
    }
    else{
        echo "success";
     @mysql_close($link);
     return;
    }

客户端大小的表是动态的。所以,我甚至无法将这些值放在一个变量中。

急需帮助:(

1 个答案:

答案 0 :(得分:3)

在你的javascript中,你应该将数组加入一个以逗号分隔的常规字符串,然后当你收到PHP时,你可以explode()将它们放回一个数组中:

// Javascript
// Join the arrays into comma-delimited strings
var latString = latArray.join(",");
var lngString = lngArray.join(",");

// Later...
// PAss them into the querystring as strings:
var queryString= "?latString="+latString;
queryString+="&lngString="+lngString+"&id="+id+"&id2="+id2+"&levelname="+levelname;

//--------------
// In PHP, explode them back to arrays:
$latArray = explode(",", $_GET['latString');
$lngArray = explode(",", $_GET['lngString');