Using Knockout.js with PHP: Best Practices

I recently had a project that made me temporarily shift from my more native c#/ environment and use php on the backend instead. As by more and more developers these days I have become accustomed to doing just about everything in the front end with the JavaScript library Knockout. Of course knockout.js is completely compatible with php being that it is on the front end while php is on the back end, but in combining php with knockout there are a few things that I have found make the match just a bit smoother.

Use json_encode() to pass PHP arrays to knockout

function getOrders() {
include_once 'mysql_connect.php'; 
$email = $_SESSION['Email'];

$query = sprintf("SELECT * FROM `Order` WHERE `Email` = '%s' order by id desc",
		mysqli_real_escape_string($con, $email));
$result = mysqli_query($con, $query);
$data = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
  $data[] = $row;

return json_encode($data);//json_encode() is the key

Then on on the front-end:

$(document).ready(function () {
    //Pass JSON encoded data directly into javascript variable
    var data = <?php getOrders(); ?> ;
    var vm = new ViewModel(data);	

function ViewModel (data){
	self = this;
	self.Orders = ko.mapping.fromJS(data);

Use ko.toJS() to send data from your ViewModel to PHP

function ViewModel (){
    self = this;
    self.Order = {
		FirstName : ko.observable(),
		LastName : ko.observable(),
		URL : ko.observable(),	
		Comments : ko.observable()	
    self.CreateOrder = function() {
        //Here is where you convert the data to something php can swallow
        var data = ko.toJS({"Data":order});
            url: "CreateOrder.php",
            type: 'post',
            data: data,
            success: function (result) {

And then on the back end:

include_once 'mysql_connect.php'; 
//recieve raw data into php variable
$data = $_POST['Data'];

//extract each field from raw data
$email = $data['Email'];
$firstName = $data['FirstName'];
$lastName = $data['LastName'];
$comments = $data['Comments'];