如何将数据从 KnockOut 发送到 Php for MySql?
我可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那里有一些有用的答案 - 大多数都归结为“嘿,你为什么不阅读文档?”
对于我尝试通过 JSON 将数据发送到 PHP 的第一部分,我发现此链接很有帮助:
[factsandpeople.com]
所以对我有用的代码(非常适合测试)是: HTML
接下来是 java 脚本:
现在是 PHP:
就像我说的,这真的很简单,但我现在至少可以遵循我得到的所有伟大建议......并阅读文档。
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
Next was the java script:
And now the PHP:
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.
在 PHP 中,您需要:
json_decode
解码 JSONINSERT
:http://www.w3schools.com/php/php_mysql_insert.aspIn your PHP you need to:
json_decode
INSERT
on your MySql Database: http://www.w3schools.com/php/php_mysql_insert.asp