Background - I have a webpage that contains a bunch of buttons (think POS system). I want the user to be able to edit the name of the button (used to put them in a particular order) and the text of the button which contains 2 parts which are the item and the cost. Presently I have it working by passing the data from a PHP page (where the edits are done) to another PHP page (where I write it back to the db) but I want to use more of a AJAX method and pass it to a js function to update a when the edit is saved. As the number of buttons can very I don't know the exact number of buttons to read into the script. Currently I have something like this...
echo "<td><input type='text' name='btn[]' size='5' value='" . $row['button'] . "'/></td>";
echo "<td><input type='text' name='itm[]' size='5' value='" . $row['item'] . "'/></td>";
echo "<td><input type='text' name='prc[]' size='5' value='" . $row['price'] . "'/></td>";
which is sent to a PHP page where I have...
$buttonArray = $_POST['btn'];
$itemArray = $_POST['itm'];
$priceArray = $_POST['prc'];
$numberofItems = count($itemArray);
for ($i=0; $i<$numberofItems; $i++)
{
$sql = "UPDATE concessions SET button = '".$buttonArray[$i]."', item = '".$itemArray[$i]."', price = '".$priceArray[$i]."'";
mysql_query($sql);
}
I want to do something similar in js looking at document.form.elements but can't figure out how to send as something (like and array) I can use a for loop to loop through. Any suggestion are welcome.
for(i = 0; i<document.forms.tabs1.elements.length ; i++){ document.forms.tabs1.elements[i].name = newNames[i] ; // tabs1 is the name of your form // document.forms.tabs1.elements[i].property // property can be name, value , id ... }
– Mehdi Karamosly Dec 12 '12 at 18:22$.each($('#yourform').serializeArray(), function() { console.log(" <" +this.name+ '>' + this.value + "</" + this.name + "> " ); });
– Mehdi Karamosly Dec 12 '12 at 20:08<btn[]>button01</btn[]>
<itm[]>iitem1</itm[]>
<prc[]>price</prc[]>
<btn[]>button02</btn[]>
<itm[]>item2</itm[]>
– Jason Templeman Dec 13 '12 at 15:05