2

I'm trying to send values of a table by arrays to server. On server side php file putting a echo, I can see that the value has passed in a form like - [value1, value2] etc. But on serverside php file, when I'm trying to retrieve the values using Php array it's not working.

Here's my client side JS function -

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);
}

Server side 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;
    }

The table on the client size is dynamic. So, I cannot even put the values in a single variable.

Need help badly :(

1 Answer 1

3

In your javascript, you should join the arrays into a regular comma-delimited string, and then when received by PHP you can explode() them back into an array:

// 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');
Sign up to request clarification or add additional context in comments.

2 Comments

Are there no json libraries for php to avoid... this?
@Blindy sure, PHP has json_decode() but the OP's code is already dealing with it as strings from arrays in a simple GET request. JSON is more useful for handling javascript objects and not totally necessary in this instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.