I have a js file and js variable named myData in array. I want to store all component of myData into mysql dbase. I tried using ajax (first of all, parse it into json file), but still fail. I'm new in json and ajax. It's my javascript. is it wrong?
var myData = [];
var arrayOfMarker = [];
function myFunctions(maps) {
var count = 1;
var arr = new Array();
for(var i=0;i<myData.length;i++){
var typef = '';
if (document.getElementById('optionsRadios1').checked == true) {
typef = 'Building';
} else {
typef = 'House';
}
var ob = {0 : count, 1 : myData[i][1], 2 : myData[i][2], 3 : typef};
arr.push(ob);
count += 1;
}
var myJString = JSON.stringify(arr);
console.log(myJString);
$.ajax({
url: 'inputdata.php',
type:"POST",
dataType:'json',
data: {data: myJString},
cache: false,
success: function(data){
console.log(data);
},
error: function(request, status, error){}
});
}
</script>
And it is my php file: inputdata.php
<?php
$con = mysql_connect("root","","")or die('Could not connect: '.mysql_error());
mysql_select_db("fpgis",$con);
$data = json_decode($_POST['data']);
$sql = "INSERT INTO pointproperty(noPoint,latPoint,lngPoint,tipePoint)VALUES('$data[0]','$data[1]',' $data[2]','$data[3]')";
if(!mysql_query($sql,$con)){
die('Error : '.mysql_error());
}
?>
Please tell me how solve this problem. I really need solution. Your solution is helpful for me. Thanks a lot :)