简体   繁体   English

JavaScript,PHP和MySQL之间的通信

[英]Communication between JavaScript, PHP and MySQL

I'm trying to learn JavaScript to code for Cordova. 我正在尝试学习JavaScript为Cordova编码。 I read many tutorials, but none of them helped me with the folowing problem. 我读了许多教程,但是没有一个帮助我解决以下问题。

My cordova app is for testing very simple. 我的cordova应用程序用于测试非常简单。 Just a textbox and 2 buttons. 只是一个文本框和2个按钮。 Both Buttons calls a PHP script on my server. 两个按钮都在我的服务器上调用一个PHP脚本。 One button sends data to the PHP script to insert the value of the textfield in a MySQL database, the second button calls the same script and should write the values of the database to my cordova app. 一个按钮将数据发送到PHP脚本,以在MySQL数据库中插入文本字段的值,第二个按钮调用相同的脚本,并将数据库的值写入我的cordova应用程序。

Here is my 这是我的

<?PHP
$response = array();

require_once __DIR__ . '/db_config.php';

$db_link = mysqli_connect (
                 DB_SERVER, 
                 DB_USER, 
                 DB_PASSWORD, 
                 DB_DATABASE
                );
mysqli_set_charset($db_link, 'utf8');
if (!$db_link)
{
    die ('keine Verbindung '.mysqli_error());
}

if(isset($_POST['action']) && $_POST['action'] == 'insert'){
    $name = $_POST['name'];
    $sql = "INSERT INTO test.testTable (name) VALUES ('$name')";
    $db_erg = mysqli_query($db_link, $sql);
    if (!$db_erg){
        echo "error";
    }else{
        echo "ok";
    }
}

if(isset($_POST['action']) && $_POST['action']=='read'){
    $sql = "SELECT * FROM testTable";

    $db_erg = mysqli_query( $db_link, $sql );
    if (!$db_erg )
    {
        $response["success"] = 0;
        $response["message"] = "Oops!";
        echo json_encode($response);
        die('Ungültige Abfrage: ' . mysqli_error());
    }

    while ($zeile = mysqli_fetch_array( $db_erg, MYSQL_ASSOC))
    {
        //$response["success"] = $zeile['pid'];
        //$response["message"] = $zeile['name'];
        $response[]=$zeile;
    }
    echo json_encode($response);
    mysqli_free_result( $db_erg );
}
?>

and here are my 2 functions in the cordova app: 这是我在cordova应用程序中的2个功能:

function getNameFromServer() {
    var url = "appcon.php";
    var action = 'read';
    $.getJSON(url, function (returnedData) {
        $.each(returnedData, function (key, value) {
            var id = value.pid;
            var name = value.name;
            $("#listview").append("<li>" + id + " - " + name) + "</li>";
        });
    });            
}

function sendNameToServer() {
    console.log("sendNameToServer aufgerufen");
    var url2send = "appcon.php";
    var name = $("#Name").val()
    var dataString = name;
    console.log(dataString);
    if ($.trim(name).length>0) {
        $.ajax({
            type: "POST",
            url: url2send,
            data: { action: 'insert', name: dataString },
            crossDomain: true,
            cache: false,
            beforeSend: function () {
                console.log("sendNameToServer beforeSend wurde aufgerufen");
            },
            success: function (data) {
                if (data == "ok") {
                    alert("Daten eingefuegt");
                }
                if (data == "error") {
                    alert("Da ging was schief");
                }
            }
        });
    }        
}

My Questions/Problems: 我的问题/问题:

  1. The sendNameToServer funtion works in that case, that the data will be inserted in my Database. 在这种情况下,sendNameToServer函数起作用,该数据将插入到我的数据库中。 But I never get the alert (the success: never called). 但是我从来没有得到过戒备(成功:从未打电话过)。

  2. How can I pass "action = read" to the PHP script in the getNameFromServer() function? 如何在getNameFromServer()函数getNameFromServer() "action = read"传递给PHP脚本?

The third question is a bit off topic, but is this art of code "save" or is it simple to manipulate the data between the cordova app and the server? 第三个问题离主题有点远,但是这种编码艺术是“保存”的吗,还是在cordova应用程序和服务器之间操纵数据是否简单? What's the better way or how can I encrypt the transmission? 有什么更好的方法或如何加密传输?

Here is one part answer to your question. 这是您问题的一部分答案。

$.getJSON has a second optional parameter data that can be an object of information you want to pass to your script. $ .getJSON具有第二个可选参数data ,该data可以是您想要传递给脚本的信息的对象。

function getNameFromServer() {
    $.getJSON("appcon.php", { action: 'read' }, function (returnedData) {
        $.each(returnedData, function (key, value) {
            var id = value.pid;
            var name = value.name;
            $("#listview").append("<li>" + id + " - " + name) + "</li>";
        });
    });            
}

Edit: Since you are using $.getJSON() , the request method is a GET , which means you have to use $_GET in your third if statement in your PHP script. 编辑:由于您使用的是$.getJSON() ,因此请求方法是GET ,这意味着您必须在PHP脚本的第三个if语句中使用$_GET

if(isset($_GET['action']) && $_GET['action'] == 'read'){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM