在PHP变量中使用Ajax数据

Hellow

I want to write data from api in MySql Database, I have api.js file to get the data from the api and a php file to write the data to MySql table;

api.js

$(function()
{
    var $orders = $('#orders');

    $.ajax({
        type:'GET',
        url:'http://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json',
        success: function(orders) {
        dataType:'json', // add json datatype to get json
        data: ({name: orders})
        $.each(orders, function(i, order) {
            $orders.append('<li>id: ' + order.id + ',  ' + order.naam);
        });     
        },
});

Following code is my php.file

<?php
define('DB_HOST', '');
define('DB_NAME', '');
define('DB_USER','');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

$myArray = $_GET['name']; 
echo ($myArray);
print_r ($myArray);
echo ('Mattijs');

foreach ($myArray as $row)
{
    echo($row[0]);
    $query = mysql_query("INSERT INTO `straat`(`ID`, `StraatFeest`) VALUES ('" + $row[0] + "','" + $row[1] + "')") or die(mysql_error());
}
    //$query = mysql_query("INSERT INTO `straat`(`ID`, `StraatFeest`) VALUES ('dd','dd')") or die(mysql_error());
    $row = mysql_fetch_array($query) or die(mysql_error());
?>

myArray is still empty

I don't get data from the ajax File

Thanks

try this

$(function() {
  var $orders = $('#orders');

  $.ajax({
    type: 'GET',
    url: 'http://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json',
    dataType: 'json', // add json datatype to get json
    success: function(orders) {
      $.each(orders, function(i, order) {
        $orders.append('<li>id: <span id="id">' + order.id + '</span>,  <span id="naam">' + order.naam + '</span></li>');
      });
    },
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="orders"></ul>

</div>

You don't need the api.js, you can fetch json using php as well (also, string concatenations in php aren't done using +, so use . instead):

$myArray = json_decode(file_get_contents('http://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json'));
foreach ($myArray as $feest)
{
  $query = mysql_query("INSERT INTO `straat`(`ID`, `StraatFeest`) VALUES (". $feest->id .",'". $feest->naam ."')") or die(mysql_error());
}

You should read this if you need to handle entries with a quote or a special character in it's name or id.