从.js文件中的MySQL中获取数据

时间:2014-07-18 09:57:07

标签: javascript php mysql ajax

我正在寻找近3天的解决方案,但我无法弄明白。这是我的问题:

我有一个名为: index.php

的文件
<html>
    <head>
        <title>AngularJS-FlowChart</title>

        <!-- 
        LiveReload support.
        http://livereload.com/
        -->
        <script src="http://localhost:35729/livereload.js?snipver=1"></script>

    </head> 
    <body 
        ng-app="app" 
        ng-controller="AppCtrl"
        mouse-capture
        ng-keydown="keyDown($event)"
        ng-keyup="keyUp($event)"
        >

        <div style="width: 100%; overflow: hidden;">
            <!--<div style="width: 600px; float: left;"> 
                <textarea 
                    style="width: 100%; height: 100%;"
                    chart-json-edit
                    view-model="chartViewModel"
                    >
                </textarea>
            </div>-->
            <div style="margin-left: 0px;">
                <button
                    ng-click="addNewNode()"
                    title="Add a new node to the chart"
                    >
                    Add Node
                </button>
                <button
                    ng-click="addNewInputConnector()"
                    ng-disabled="chartViewModel.getSelectedNodes().length == 0"
                    title="Add a new input connector to the selected node"
                    >
                    Add Input Connector
                </button>
                <button
                    ng-click="addNewOutputConnector()"
                    ng-disabled="chartViewModel.getSelectedNodes().length == 0"
                    title="Add a new output connector to the selected node"
                    >
                    Add Output Connector
                </button>
                <button
                    ng-click="deleteSelected()"
                    ng-disabled="chartViewModel.getSelectedNodes().length == 0 && chartViewModel.getSelectedConnections().length == 0"
                    title="Delete selected nodes and connections"
                    >
                    Delete Selected
                </button>

                <!--
                This custom element defines the flowchart.
                -->
                <flow-chart
                    style="margin: 5px; width: 40%; height: 70%; float: right"
                    chart="chartViewModel"
                    >
                </flow-chart>

                            <flow-chart
                    style="margin: 5px; width: 40%; height: 70%; float: left"
                    chart="chartViewModel"
                    >
                </flow-chart>
            </div>
        </div>

        <link rel="stylesheet" type="text/css" href="app.css">

        <!-- Library code. -->
        <script src="lib/jquery-2.0.2.js" type="text/javascript"></script>
        <script src="lib/angular.js" type="text/javascript"></script>

        <!-- Flowchart code. -->
        <script src="debug.js" type="text/javascript"></script>
        <script src="flowchart/svg_class.js" type="text/javascript"></script>
        <script src="flowchart/mouse_capture_service.js" type="text/javascript"></script>
        <script src="flowchart/dragging_service.js" type="text/javascript"></script>
        <script src="flowchart/flowchart_viewmodel.js" type="text/javascript"></script>
        <script src="flowchart/flowchart_directive.js" type="text/javascript"></script>

        <!-- App code. -->
        <script src="app.js" type="text/javascript"></script>
    </body>
</html>

我还有一个名为 app.js

的文件
//
    // Define the 'app' module.
    //
    angular.module('app', ['flowChart', ])

    //
    // Simple service to create a prompt.
    //
    .factory('prompt', function () {

        /* Uncomment the following to test that the prompt service is working as expected.
        return function () {
            return "Test!";
        }
        */

        // Return the browsers prompt function.
        return prompt;
    })

    //
    // Application controller.
    //
    .controller('AppCtrl', ['$scope', 'prompt', function AppCtrl ($scope, prompt) {

        //
        // Code for the delete key.
        //
        var deleteKeyCode = 46;

        //
        // Code for control key.
        //
        var ctrlKeyCode = 65;

        //
        // Set to true when the ctrl key is down.
        //
        var ctrlDown = false;

        //
        // Code for A key.
        //
        var aKeyCode = 17;

        //
        // Code for esc key.
        //
        var escKeyCode = 27;

        //
        // Selects the next node id.
        //
        var nextNodeID = 10;

        //
        // Setup the data-model for the chart.
        //
        var chartDataModel = {};

        //
        // Event handler for key-down on the flowchart.
        //
        $scope.keyDown = function (evt) {

            if (evt.keyCode === ctrlKeyCode) {

                ctrlDown = true;
                evt.stopPropagation();
                evt.preventDefault();
            }
        };

        //
        // Event handler for key-up on the flowchart.
        //
        $scope.keyUp = function (evt) {

            if (evt.keyCode === deleteKeyCode) {
                //
                // Delete key.
                //
                $scope.chartViewModel.deleteSelected();
            }

            if (evt.keyCode == aKeyCode && ctrlDown) {
                // 
                // Ctrl + A
                //
                $scope.chartViewModel.selectAll();
            }

            if (evt.keyCode == escKeyCode) {
                // Escape.
                $scope.chartViewModel.deselectAll();
            }

            if (evt.keyCode === ctrlKeyCode) {
                ctrlDown = false;

                evt.stopPropagation();
                evt.preventDefault();
            }
        };

        //
        // Add a new node to the chart.
        //
        $scope.addNewNode = function () {

            var nodeName = prompt("Enter new node!", "New node");
            if (!nodeName) {
                return;
            }

            //
            // Template for a new node.
            //
            var newNodeDataModel = {
                name: nodeName,
                id: nextNodeID++,
                x: 50,
                y: 50
            };

            $scope.chartViewModel.addNode(newNodeDataModel);
        };

        //
        // Add an input connector to selected nodes.
        //
        $scope.addNewInputConnector = function () {
            var connectorName = prompt("Enter a connector name:");
            if (confirm(connectorName)) {
                var selectedNodes = $scope.chartViewModel.getSelectedNodes();
                for (var i = 0; i < selectedNodes.length; ++i) {
                    var node = selectedNodes[i];
                    node.addInputConnector({
                        name: connectorName
                    });
                }
            } else return;
        };

        //
        // Add an output connector to selected nodes.
        //
        $scope.addNewOutputConnector = function () {
            var connectorName = prompt("Enter a connector name:");
            if (confirm(connectorName)) {
                var selectedNodes = $scope.chartViewModel.getSelectedNodes();
                for (var i = 0; i < selectedNodes.length; ++i) {
                    var node = selectedNodes[i];
                    node.addOutputConnector({
                        name: connectorName,
                    });
                }
            } else return;
        };

        //
        // Delete selected nodes and connections.
        //
        $scope.deleteSelected = function () {

            $scope.chartViewModel.deleteSelected();
        };

        //
        // Create the view-model for the chart and attach to the scope.
        //
        $scope.chartViewModel = new flowchart.ChartViewModel(chartDataModel);
    }])
    ;

我想要实现的是从MySQL读取数据并在 app.js 文件中使用相同的数据。我真的需要任何帮助。 我在前5个谷歌页面上阅读了关于这个thema的所有文章但没有任何成功。我尝试了各种各样的&#34;教程&#34;但我可以找到解决方案。

1 个答案:

答案 0 :(得分:0)

使用jQuerys AJAX函数将数据发送到PHP服务:

$.ajax({
        type: 'POST',
        url: path + '/php/yourService.php',
        data: 'var=' + var,
        success: function (response) {
           // Do smth with the response
        }
    });

在PHP服务中读出数据库的数据:

// connect to your database first

$username = $_POST["var"];  
$sql="SELECT * FROM users WHERE anything = '$var'";
$result = mysql_query($sql);

if($result === FALSE) {
    die(mysql_error());
}

while($row = mysql_fetch_array($result)){ 
    return $row["variable"];
}

应该做的伎俩。无论角度与否都无关紧要。返回的数据可通过ajax onSuccess函数中的响应变量访问。希望这有帮助