简体   繁体   English

如何使用php将javascript数组发送到数据库

[英]how to send javascript array to database using php

I have an array of strings in javascript, I want to send it to a database using PHP. 我在javascript中有一个字符串数组,我想使用PHP将其发送到数据库。 I have been searching everywhere trying to convert the array to PHP array and then send it. 我一直在到处搜索试图将数组转换为PHP数组然后发送它。 I am already connected to the database just need to send this array. 我已经连接到数据库,只需要发送该数组即可。

Assuming you're talking about client-side Javascript (JS) and not something like NodeJS, you can't just convert a JS array into a PHP array, since the PHP array lives on the server side. 假设您是在谈论客户端Javascript(JS),而不是像NodeJS之类的东西,那么您不能仅将JS数组转换为PHP数组,因为PHP数组位于服务器端。 So, what you can do, is send it to the server. 因此,您可以做的就是将其发送到服务器。 You can either do this through a standard POST request, or, what I'm assuming would be the better option for you, through an Asycronous (AJAX) request. 您可以通过标准的POST请求执行此操作,也可以通过异步(AJAX)请求为您提供更好的选择。

There are several tutorials on how to use AJAX, and depending on any existing libraries that you already have in place, you may choose to follow a jQuery or Angular specific one. 关于如何使用AJAX的教程有几本,根据您已经拥有的任何现有库,您可以选择遵循jQuery或Angular特定的库。 https://www.tutorialspoint.com/ajax/index.htm https://www.tutorialspoint.com/ajax/index.htm

Once you send it to the server, you're likely reading it from the PHP $_POST array. 将其发送到服务器后,您很可能会从PHP $_POST数组中读取它。 Then you would acccess the posted array (sanitize/validate it, because we NEVER trust data from the client) and then you can save that data to the database that you've already setup the connection for. 然后,您将对发布的数组进行访问(清理/验证它,因为我们从不信任来自客户端的数据),然后您可以将该数据保存到已经为其建立连接的数据库中。

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

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