越来越可变大小的形式JavaScript和PHP(AJAX) [英] getting variable size form to javascript and PHP (AJAX)
问题描述
背景 - 我有一个包含一系列按钮(想想POS系统)的网页。我希望用户能够编辑按钮(用于把它们以特定的顺序),并含有2份哪些是项目和成本的按钮的文本的名称。 presently我有工作从一个PHP页面(其中编辑完成)传递数据到另一个PHP页面(即我把它写回分贝),但我想使用AJAX方法越来越传递一个js函数更新编辑保存时。作为按钮的数量可以非常不知的按钮的确切数目读取到脚本。目前,我有这样的事情......
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>";
被发送到一个PHP网页,我有...
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);
}
我想在JS类似的东西看着document.form.elements,但无法弄清楚如何发送的东西(像和阵列),我可以通过使用for循环来循环。任何建议,欢迎。
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.
推荐答案
有一个很简单的方法来做到这一点:使用这个简单的JavaScript函数(请确保您有最新的jQuery加载)
There is a very easy way to do this: use this simple javascript function (make sure you have a recent jquery loaded)
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
此功能将序列以正确的顺序,并会输出一个JavaScript对象,你的表单元素。 您需要使用像这样使用Ajax,以解析你的价值观:
This function will serialize your form elements in the right order and will output a javascript object. You need to use it like this with ajax in order to parse your values:
var objects = $("#ID_OF_YOUR_FORM").serializeObject();
$.ajax({
url: "your_ajax_file_location.php",
type: "POST",
data: ({
objects:objects,
otherdata:otherdata
}),
success: function(msg){
/* do whatever here */
}
});
阿贾克斯会做它的魔力,并自动转换的对象在一个PHP数组,并将其张贴到PHP ...试试这个:
Ajax will do it's magic and transform that object in a php array automatically and post it to php... try this:
echo "<pre>".$_POST['objects']."</pre>";
PHP的数组应该是这样的:
The php array should look like:
{
btn= array(
"your btn1 info",
"your btn2 info",
"your btn3 info"
);
itm= array(
"your itm1 info",
"your itm2 info",
"your itm3 info"
);
prc= array(
"your prc1 info",
"your prc2 info",
"your prc3 info"
);
}
从这个类型的数组,这是pretty的容易在PHP操纵,以更新数据库
From this type of array, it's pretty easy to manipulate in php in order to update your database
这篇关于越来越可变大小的形式JavaScript和PHP(AJAX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!