0

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
  • Psst: it’s just <input> or <input />, not <input></input>. Commented Sep 21, 2013 at 0:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You should also call encodeURIComponent on Change[name]=, as the brackets should be encoded.
Well, that was a silly mistake I made. Thanks for your help. Now I now know to use encode as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.