简体   繁体   English

使用ajax和PHP发送$ _POST数组?

[英]Send $_POST array using ajax with PHP?

I have multiple input fields with the name Change[xxx] . 我有多个输入字段,名称为Change[xxx] The original way is when an update button is clicked, it normally sends the data and updates the database. 原始方式是单击更新按钮时,通常会发送数据并更新数据库。 But how do I pass this Change[xxx] data if I use Ajax? 但是,如果我使用Ajax,如何传递Change[xxx]数据? I want to do this without jQuery. 我想在没有 jQuery的情况下执行此操作。

The HTML: HTML:

<input type='text' name='Change[name]' value='Bob' onblur='updateField($id)'></input>

Retrieving the info in PHP: 检索PHP中的信息:

foreach($_POST['Change'] as $field => $value) {
    if($field == 'name') {
        // update database
    }
}

The JavaScript: JavaScript:

Using request.send(...) , this is where I'm not sure how to send the data. 使用request.send(...) ,这是我不确定如何发送数据的地方。

function updateField(id) {
    …
    var url = 'orders.php?id='+ id;
    request.open('POST', url, true);
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    var val = document.getElementsByName("Change[name]")[0].value;
    request.send("id=" + id + "&Change[" + val + "]"); 
    …
}

Exactly the same as you usually would: 与通常的情况完全相同:

request.send("id=" + id + "&Change[name]=" + encodeURIComponent(val));

The brackets aren't special to HTTP, only to PHP. 括号不是HTTP专用的,只是PHP专用的。

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

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