如何将数据从 KnockOut 发送到 Php for MySql?

发布于 2024-12-29 22:24:19 字数 590 浏览 0 评论 0原文

我可以从 KnockOut 中正确显示我的数据;但是,我无法找到一种好方法来获取 PHP 并让 PHP 为 mySQL 准备数据:

从我的表单页面,我可以触发数据在提交时显示,没问题:

var json =   ko.utils.stringifyJson( this.Stuff );     
alert("This is what I wanna save: \n" + json );

但是,假设我想将其发送到一个 php 文件将其写入 mySql——接下来我该怎么做?

ko.utils.postJson( $("form")[0], this.Stuff );
// or
$.get( "_php/savetodb.php" , json );

...而当 PHP 页面接收到 JSON 格式的数据时,然后呢?

我在网上看到的每个解决方案基本上都在说“你可以做到”。但不是我要做的事情。

我正在尝试的路径(从我读到的内容来看,这是首选路径)是: 表格数据->淘汰赛 ->编码 JSON -> PHP->解码 JSON ->数据库管理系统

I can get my data to show properly from KnockOut; however, I cannot figure out a good way to get it the the PHP and have PHP prepare the data for mySQL:

From my form page I can trigger the data to display on submit, no problem:

var json =   ko.utils.stringifyJson( this.Stuff );     
alert("This is what I wanna save: \n" + json );

But, say I want to send it to a php file to write it to mySql--what do I do next?

ko.utils.postJson( $("form")[0], this.Stuff );
// or
$.get( "_php/savetodb.php" , json );

...And when the PHP page receives the JSON formatted data, then what?

Every solution I have seen online has basically said "you can do it." but not what it is that I would be doing.

The path I am trying ( from what I have read, this is the preferred path) is:
FORM DATA -> KNOCKOUT -> ENCODE JSON -> PHP -> DECODE JSON -> MYSQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2025-01-05 22:24:19

那里有一些有用的答案 - 大多数都归结为“嘿,你为什么不阅读文档?”

对于我尝试通过 JSON 将数据发送到 PHP 的第一部分,我发现此链接很有帮助:
[factsandpeople.com]

所以对我有用的代码(非常适合测试)是: HTML

<form name="info" action="" method="get">
    First : <input type="text" name="first" value="Sandra" size="23"><br>
    Last : <input type="text" name="last" value="Grastlath"  size="23"><br>

<input type="button" name="valid" value="Validate" onclick="validate()">

<div id="fullresponse"><span/></div>
<div id="sales1Lastname"><span/></div>

接下来是 java 脚本:

var data = 
    { 
         "sales": [ 
             { "firstname" : "John", "lastname" : "Brown" },
             { "firstname" : "Marc", "lastname" : "Johnson" }
          ] // end of sales array
    }
function validate()
    {
        var p = document.forms['info'];

        data['sales'].push( { "firstname" : document.forms['info']['first'].value , "lastname" : document.forms['info']['last'].value } );

        var dataString = JSON.stringify(data);
        $.get('parser.php', { data: dataString}, showResult, "text");
    }

function showResult(res)
    {
        $("#fullresponse").html("<br><b>Full response: </b><br>" +res);
        var obj = JSON.parse(res);
        var l = obj.sales.length - 1;
        $("#sales1Lastname").html("<br><b>Lastname of sales[1]: </b><br>" +obj.sales[l].lastname);
    }

现在是 PHP:

    $logFile = 'logFile';
    $req = $_REQUEST['data'];

    $res = json_decode(stripslashes( $req ), true);

    error_log("result: ".$req.", res=".json_encode($res), 3, $logFile);

    error_log(", sales1_lastname: ".$res['sales'][1]['lastname'], 3, $logFile);

    error_log("\n", 3, $logFile);

    header("Content-type: text/plain");

    echo json_encode($res);

    ?>

就像我说的,这真的很简单,但我现在至少可以遵循我得到的所有伟大建议......并阅读文档。

There were some helpful answers out there-- mostly they boiled down to "Hey, why don't you read the documentation?"

For the first part where I am trying to send data to PHP via JSON I found this link helpful:
[factsandpeople.com]

so the code that is working for me (well for testing) is: HTML

<form name="info" action="" method="get">
    First : <input type="text" name="first" value="Sandra" size="23"><br>
    Last : <input type="text" name="last" value="Grastlath"  size="23"><br>

<input type="button" name="valid" value="Validate" onclick="validate()">

<div id="fullresponse"><span/></div>
<div id="sales1Lastname"><span/></div>

Next was the java script:

var data = 
    { 
         "sales": [ 
             { "firstname" : "John", "lastname" : "Brown" },
             { "firstname" : "Marc", "lastname" : "Johnson" }
          ] // end of sales array
    }
function validate()
    {
        var p = document.forms['info'];

        data['sales'].push( { "firstname" : document.forms['info']['first'].value , "lastname" : document.forms['info']['last'].value } );

        var dataString = JSON.stringify(data);
        $.get('parser.php', { data: dataString}, showResult, "text");
    }

function showResult(res)
    {
        $("#fullresponse").html("<br><b>Full response: </b><br>" +res);
        var obj = JSON.parse(res);
        var l = obj.sales.length - 1;
        $("#sales1Lastname").html("<br><b>Lastname of sales[1]: </b><br>" +obj.sales[l].lastname);
    }

And now the PHP:

    $logFile = 'logFile';
    $req = $_REQUEST['data'];

    $res = json_decode(stripslashes( $req ), true);

    error_log("result: ".$req.", res=".json_encode($res), 3, $logFile);

    error_log(", sales1_lastname: ".$res['sales'][1]['lastname'], 3, $logFile);

    error_log("\n", 3, $logFile);

    header("Content-type: text/plain");

    echo json_encode($res);

    ?>

Like I said, this was really simple, but I am at a place now where I can at least follow all the great advice I was given... and read the documentation.

鯉魚旗 2025-01-05 22:24:19

在 PHP 中,您需要:

  1. 使用 json_decode 解码 JSON
  2. 在 MySql 数据库上执行 INSERThttp://www.w3schools.com/php/php_mysql_insert.asp

In your PHP you need to:

  1. Decode the JSON using json_decode
  2. Execute an INSERT on your MySql Database: http://www.w3schools.com/php/php_mysql_insert.asp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文