jQuery.Post/Ajax POST 到 PHP 以写入 XML 文件
我在 jquery 中有一个客户端代码,并且在客户端代码中我需要以使用 PHP 编写的 XML 文件的形式在服务器端存储一些数据。我已经完成了编写 PHP 文件的代码。但是,我无法将客户端代码传递到 PHP 文件。 jQuery post 也不起作用。我尝试了查询字符串,但正如我所说,数据太大,因此它不起作用。
有什么建议吗?
感谢您的时间和考虑。:)
一些测试代码:
jQuery POST
$.post("test2.php", { userdata: "test"} );
Ajax POST
$(document).ready(function()
{
$(document).ready(function()
{
$.ajax({
type: "POST",
url: "test2.php",
data: "userdata=" + "test"
});
});
});
PHP 代码
$test = $_POST['userdata']; $document = new DOMDocument();
$document->load("collection.xml");
//Write XML file generation code
I have a client side code in jquery and from client side code I need to store some data on server side in form of XML files that are written using PHP. I'm done with the code for writing PHP files. However, I'm not able to pass client side code to PHP file. jQuery post is also not working. I tried query strings but as I said data is too large thus it doesn't work.
Any suggestions?
Thank you for your time and consideration.:)
Some Test Code:
jQuery POST
$.post("test2.php", { userdata: "test"} );
Ajax POST
$(document).ready(function()
{
$(document).ready(function()
{
$.ajax({
type: "POST",
url: "test2.php",
data: "userdata=" + "test"
});
});
});
PHP Code
$test = $_POST['userdata']; $document = new DOMDocument();
$document->load("collection.xml");
//Write XML file generation code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果是表单数据,则传递serializeArray()。或根据要发送的内容创建一个数组。例如,
在服务器端循环访问该数据以创建 xml。您应该使用数组,这样无论内容的大小是多少,它都会被包装在一个可以在服务器端获取的变量中。
将 js Array() 转换为 JSon 对象以供使用使用 JQuery .ajax
pass serializeArray() in case of form data. or create an array from the contents you want to send. for example
loop through that data on server side for creating xml. You should use array so that no matter what is the size of contents it got wrapped up in a variable which can be fetched on server side.
Convert js Array() to JSon object for use with JQuery .ajax
据我了解,您的问题不是接收和写出数据,而是将该数据写入服务器上存储的文件中。为此,您需要使用类似 php 函数 fopen 的东西:
我不确定为什么您要将数据写入 Web 服务器上的文件而不是将信息存储在数据库中,但我确信您有自己的理由。请务必考虑安全隐患,并确保人们无法上传可执行代码,并且权限设置正确(并且数据得到正确清理)。
祝你好运 :)
As I understand it your problem is not receiving and writing out the data, but writing that data into a file stored on the server. For this you need to use something like the php function fopen:
I'm not sure why you want to write the data to files on the web server rather than store the information in a database but I'm sure you have your reasons. Just be sure to consider the security implications and make sure people can't, for example, upload executable code and that permissions are set properly (and data is sanitised properly).
Good luck :)