2017-01-25 3 views
0

Ich habe diese funcion die Objekte mit Arrays Liste:

//onload event-- to set the values 
$scope.$on('$stateChangeSuccess', function() { 

    $scope.cart=sharedCartService.cart; 
    $scope.total_qty=sharedCartService.total_qty; 
    $scope.total_amount=sharedCartService.total_amount;  
});  

Ich brauche alle Daten zu erhalten und legen Sie alle (Auffüllen) in einer Datenbank. Ich benutze MySQL und PHP.

Danke.

Antwort

1

Sie erstellen eine Datei lassen sagen seine save.php

In dieser Datei mit dem Namen Sie so etwas wie

header("Access-Control-Allow-Origin: *"); 
Global $db; 
$db = new PDO('mysql:dbname=databasename;host=localhost', 'dbuser', 'dbpassword'); 
if (isset($_SERVER['HTTP_ORIGIN'])) { 
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); 
    header('Access-Control-Allow-Credentials: true'); 
    header('Access-Control-Max-Age: 86400'); // cache for 1 day 
} 

// Access-Control headers are received during OPTIONS requests 
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { 

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) 
     header("Access-Control-Allow-Methods: GET, POST, OPTIONS");   

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) 
     header("Access-Control-Allow-Headers:  {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); 

    exit(0); 
} 
$postdata = file_get_contents("php://input"); 
if (isset($postdata)) { 
    $request = json_decode($postdata); 
    $cart = $request->cart; 
    $total_qty = $request->total_qty; 
    $total_amount = $request->total_amount; 

} 
else { 
    echo "Not called properly!"; 
} 
$query = $db->prepare(" 
       INSERT INTO yourtable 
        (cart, total_qty, total_amount) 
       VALUES 
        (:cart, :total_qty, :total_amount)"); 

$query->execute(array(
       ':cart' => $cart, 
       ':total_qty' => $total_qty, 
       ':total_amount' => $total_amount)); 

Und in Ihrer Funktion in Angular (stateChangeSuccess) haben Sie eine Post-Anforderung machen zum beispiel http://localhost:8080/save.php

$http.post(url, data, config) 
.then(
    function(response){ 
    // success callback 
    }, 
    function(response){ 
    // failure callback 
    } 
); 
+0

Danke! Tolle Lösung! – Ramos

+1

Wenn Sie das suchen, markieren Sie es als beantwortet. – mirzak

+0

Danke @mirzak ..... Gut funktionieren. Groß! – Ramos