简体   繁体   English

如何将数组从javascript传递到php

[英]How to pass an array from javascript to php

它应该如何工作的屏幕截图(抱歉语言)

So i've been asked to remake some registration forms.所以我被要求重新制作一些注册表。 The way its supposed to work is, that an interpreter chooses X amount of languages in the first select box.它的工作方式是,解释器在第一个选择框中选择 X 种语言。 Then based on the selections of languages, the user must specify from which languages they can translate from/to.然后根据语言的选择,用户必须指定他们可以从哪些语言翻译/翻译成哪些语言。

I want to store this data in a key/value array, with the key being "LanguageFrom" and Value being another array, of "LanguagesTo".我想将此数据存储在一个键/值数组中,键是“LanguageFrom”,值是另一个数组,“LanguagesTo”。 This is how i have solved this:这就是我解决这个问题的方法:

function btnTest() {
            var fromArray = $('.freelancerLanguagesFrom').map(function() {
                return $(this).val();
            }).get();

            var toArray = $('.freelancerLanguagesTo').map(function() {
                return $(this).val();
            }).get();

            var tempArray = {};
            tempArray[fromArray] = toArray;
}

This method is being called with an "onclick" function in the html part.正在使用 html 部分中的“onclick”函数调用此方法。 The user should specify which languages he can translate to for each of the chosen languages in the first box, I am aware that this probably isn't the ideal approach, but im still an inexperienced developer, and i'd love to hear your take on another approach.用户应该在第一个框中指定他可以为每种所选语言翻译的语言,我知道这可能不是理想的方法,但我仍然是一个缺乏经验的开发人员,我很想听听您的看法在另一种方法上。 Now comes my problem:现在我的问题来了:

1) How do i make it so the array wont overwrite the existing array with each button click, and instead just add to the array? 1)我如何做到这样数组不会在每次单击按钮时覆盖现有数组,而只是添加到数组中?

2) How do i process this array on the server side (php), so that i can store the values in my database? 2) 我如何在服务器端 (php) 处理这个数组,以便我可以将值存储在我的数据库中?

3) Is it possible to skip the flow where the user has to press the save(gem) button after each language he has chosen? 3)是否可以跳过用户在选择每种语言后必须按保存(宝石)按钮的流程?

edit: Question 1 and 3 are now solved, my only problem is accessing the array i made in js, on the php side编辑:问题 1 和 3 现已解决,我唯一的问题是在 php 端访问我在 js 中创建的数组

1) tempArray exists only in the scope of the btnTest() function. 1) tempArray 只存在于 btnTest() 函数的范围内。 Declare it outside (in the global scope), initialize it as {} and don't reset it every time you click the button.在外部(在全局范围内)声明它,将其初始化为 {} 并且不要在每次单击按钮时重置它。 The way you get the fromArray variable may require some tweaking depending on whether the "from" list can accept a multiple selection or not.获取 fromArray 变量的方式可能需要进行一些调整,具体取决于“from”列表是否可以接受多项选择。

2) Ajax may help. 2) Ajax可能会有所帮助。 Create a php endpoint to receive the request and call it using ajax.创建一个 php 端点来接收请求并使用 ajax 调用它。 You can work on the array using JSON .您可以使用JSON 处理数组。 Send your data using JSON.stringify(tempArray) and read it using json_decode() in your php script, or simply set the request headers as "application/json" to have it done automatically for you.使用 JSON.stringify(tempArray) 发送您的数据并在您的 php 脚本中使用 json_decode() 读取它,或者简单地将请求标头设置为“application/json”以自动为您完成。

3) I personally wouldn't automate this process. 3)我个人不会自动化这个过程。 Let's say I have 4 languages, Italian, English, French and Chinese.假设我有 4 种语言,意大利语、英语、法语和中文。

I have selected a desirable state of languages I can handle:我选择了我可以处理的理想语言状态:

Italian -> English, French意大利语 -> 英语、法语

But I also know how to translate French in Italian so I click, in the from list, French, and I get但我也知道如何将法语翻译成意大利语,所以我在源列表中单击法语,然后我得到

French -> English法语 -> 英语

Which is an undesirable state, for me, because I don't know how to do that.对我来说,这是一种不受欢迎的状态,因为我不知道该怎么做。 Especially if I were to select many languages, I'd get, inbetween 2 states I want to save, an indefinite amount of states I don't want to save.特别是如果我要选择多种语言,我会在我想保存的 2 个状态之间得到无限数量的我不想保存的状态。

If you still want to do so, you need to move the even listener from the button to the list(s), with the onchange event.如果您仍然想这样做,您需要使用 onchange 事件将偶数侦听器从按钮移动到列表中。

I'd also suggest you do your event binding trough jQuery , if you aren't already.如果您还没有,我还建议您通过jQuery 进行事件绑定。

Hope this helped.希望这有帮助。

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

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