To be honest Im not sure how to frame this question, but here goes.
I have a form with the following input fields:
<input name="key[23]" type="text" />
<input name="key[29]" type="text" />
<input name="key[65]" type="text" />
I can create an array with Jquery to send to the PHP script like so:
var new_keys = new Array();
$('input[name="keys[]"]').each(function() {
new_keys.push($(this).attr('value'));
});
However I dont know how to create the array when the input fields already have a key: input name=”test[45]”
I tried changing my method to using a class selector instead of the input name, but I cant get the keyname.
What I need to do is send the keyname and its value to PHP so I can update a table where the keyname equals the table ID number. This is simple to do if I wasnt using Ajax.
Hope that makes sense.
Edited
<form method="post" id="add-form" class="form" onsubmit="return false;">
<input class="required" type="text" name="name" id="name" />
<input class="required" type="text" name="keys[32]" />
<input class="required" type="text" name="keys[65]" />
<textarea name="Test"></textarea>
<input type="submit" name="submit" value="submit" />
</form>
$(document).ready(function() {
$.fn.submitForm = function() {
var name = $('#name').attr('value');
var new_keys = new Array();
$('input[name="keys[]"]').each(function() {
new_keys.push($(this).attr('value'));
});
$.ajax({
type: 'POST',
data: { 'new_keywords' : new_keywords, 'name' : name },
dataType: 'json',
url: 'article/scripts/article.scripts.php',
success: function($data) {
var result = $data.result;
var msg = $data.msg;
if (result == "success") {
formMessageDisplay(msg, result);
}
else {
formMessageDisplay(msg, result);
}
},
error: function($data) {
formMessageDisplay('<?php echo AJAX_ERROR; ?>', result);
}
})
}
});
This is a cutdown version of the script that I have.
You could do something like this which should grab the names of the fields and then assign the values in the array. You can then process and clean it up in PHP as needed:
For further reading on ajax look @ this for using the post method http://api.jquery.com/jQuery.post/