简体   繁体   English

通过AJAX发送输入数组

[英]Sending input array via AJAX

I've dealt with javascript in the past, but I'm trying to relearn it. 过去我已经处理过javascript,但是我想重新学习它。 For that reason I'd like to come up with a javascript solution without the use of libraries like JQuery. 因此,我想提出一种JavaScript解决方案,而不使用JQuery之类的库。

I've come across several threads, and they have been close but not a perfect match to my situation. 我遇到了几个线程,它们虽然接近但并不完美。

I have a form generated dynamically by PHP, it grabs info from the database, and echoes out all the form inputs. 我有一个由PHP动态生成的表单,它从数据库中获取信息,并回显所有表单输入。 They are all checkboxes representing the entry in the database. 它们都是代表数据库中条目的复选框。 Normally I would use name="id[value]" where value is the id of the current entry being looped through. 通常我会使用name =“ id [value]”,其中value是正在循环通过的当前条目的ID。

So I would have something like this: 所以我会有这样的事情:

<input type="checkbox" name="del[1]"/>
<input type="checkbox" name="del[2]"/>
<input type="checkbox" name="del[3]"/>

When I would post this on form submit, I would just get the value of $_POST['del'] and call it a day. 当我将其发布到表单提交时,我只会得到$ _POST ['del']的值并将其命名为一天。 However, I'm trying to send it to a PHP page with an AJAX function so i can perform the functions without changing the page or refreshing. 但是,我试图将其发送到具有AJAX函数的PHP页面,以便我可以执行这些功能而无需更改页面或刷新。

My question is: How do I get all of the checkboxes(that are checked) and send them to the PHP page so it can understand which ones were checked. 我的问题是:如何获取所有选中的复选框,并将其发送到PHP页面,以便它可以了解选中了哪些复选框。 My understanding is that this will require some kind of conversion. 我的理解是,这将需要某种转换。 My first though was to loop through each checked box, and grab its index (or give them all Ids and grab the id) and put it into a string that I could then explode PHP side. 我的第一个方法是遍历每个复选框,并获取其索引(或为它们提供所有ID并获取ID),然后将其放入字符串中,然后可以展开PHP端。

Any thoughts? 有什么想法吗? Thanks, sharf. 谢谢,码头。

var i, child, str='', arr = Array(),
    form = document.getElementById('form')

for(i=0; i<form.childNodes.length; i++) {
    // grab the element
    var child = form.childNodes[i];
    // make sure it is an input and a checkbox
    if(child.tagName!='INPUT' || child.getAttribute('type')!='checkbox') continue
    // if the checkbox is checked, add the name and value to a url string
    if(child.checked)
        arr.push(child.getAttribute('name')+'='+encodeURIComponent(child.value))
    }
}

// make the URL string
str = arr.join('&')

// now you can use this as a url: 
SomeAjaxUrl + str

Then, in PHP: 然后,在PHP中:

<?php
    $checked_options = $_REQUEST['del'];
    print_r($checked_options);
?>

You can use jquery like you said and loop though 你可以像你说的那样使用jQuery并循环

http://api.jquery.com/attribute-starts-with-selector/ http://api.jquery.com/attribute-starts-with-selector/

But for ease i would look at something similar to backbone forms https://github.com/powmedia/backbone-forms 但是为了简便起见,我会看一些类似于主干形式的内容https://github.com/powmedia/backbone-forms

which manages your model for you and lets you send by ajax easily. 它可以为您管理模型,并让您轻松地通过ajax发送。

Their github has much more info on there along with some examples. 他们的github上有更多信息以及一些示例。 This puts the checkboes in an array which you can handle php side easily 这将检查框放置在可以轻松处理php端的数组中

edit: pure javascript 编辑:纯javascript

var pairs = [];
var form = document.getelementbyid("form");
for ( var i = 0; i < form.elements.length; i++ ) {
   var e = form.elements[i];
   pairs.push(encodeURIComponent(e.name) + "=" + encodeURIComponent(e.value));
}
var output= pairs.join("&");

You should be able to modify that pretty easy 您应该能够轻松修改

Use JSON.stringify() ( MDN ) to fix your problem. 使用JSON.stringify()MDN )解决您的问题。 You can convert an Array or an Object to a valid JSON Object which can be sent through AJAX. 您可以将数组或对象转换为可以通过AJAX发送的有效JSON对象

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

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