doufan6544 2013-09-21 00:36
浏览 19
已采纳

使用带有PHP的ajax发送$ _POST数组?

I have multiple input fields with the name 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? I want to do this without jQuery.

The HTML:

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

Retrieving the info in PHP:

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

The JavaScript:

Using request.send(...), this is where I'm not sure how to send the data.

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 + "]"); 
    …
}
  • 写回答

1条回答 默认 最新

  • draxq02664 2013-09-21 00:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?