[英]Compare form inputs with database values
I have a form for adding attributes to products. 我有一个用于向产品添加属性的表格。 The attributes are added by appending to form table from checkboxes with jquery. 通过使用jquery从复选框将其附加到表单表中来添加属性。 Everithing is working great except when i want to remove an attribute from a product. 一切都很好,除非我想从产品中删除属性。
if(isset($_POST['product_attribute'])){
foreach ($_POST['product_attribute'] as $product_attribute) {
if($product_attribute['attribute_id']) {
$query = sprintf("DELETE FROM product_attribute WHERE product_id ='%d' AND attribute_id='%d'", (int)$product_id, (int)$product_attribute['attribute_id']);
$database->delete($query);
foreach ($product_attribute['product_attribute_description'] as $product_attribute_description) {
$query = sprintf("INSERT INTO product_attribute SET product_id='%d', attribute_id='%d', text='%s'", (int)$product_id, (int)$product_attribute['attribute_id'], $product_attribute_description['text']);
$database->insert($query);
}
}
}
}else{
$query = sprintf("DELETE FROM product_attribute WHERE product_id = '%d'", $product_id);
$database->delete($query);
}
Here are my js functions 这是我的js函数
function setAttributes() {
$s('div.attributes .form_attribute_name').each(function() {
if (this.checked == true) {
var val = $s(this).val().split("|");
var id_att = $s(this).attr('id').split("_");
var identic = 0;
$s('tr.attrClass').each(function() {
var tr_id_att = $s(this).attr('id').split('_');
if (tr_id_att[1] == id_att[1]) {
identic++;
}
})
if (identic == 0) {
$s('.table tr.information').remove();
addAttribute(val[0], val[1], id_att[1]);
}
} else {
var id_att = $s(this).attr('id').split('_');
$s('#attribute-row_' + id_att[1]).remove();
}
});
}
var attribute_row = 0;
function addAttribute(name, value, id) {
if (id == undefined) {
id = "";
}
if (name == undefined) {
name = "";
}
if (value == undefined) {
value = "";
}
html = '<tr id="attribute-row_' + id + '" class="attrClass">'
html += '<td>';
html += '<input type="text" value="' + name + '" name="product_attribute[' + id + '][name]" />';
html += '<input type="hidden" value="' + id + '" name="product_attribute[' + id + '][attribute_id]" />';
html += '</td>';
html += '<td><textarea name="product_attribute[' + id + '][product_attribute_description][2][text]"></textarea>';
html += '<td><a class="bluebtn" id="delrow" onclick="$s(\'#attribute-row_' + id + '\').remove();">Inlatura</a></td>';
html += '</tr>';
$s('tbody').append(html);
attribute_row++;
}
Html table HTML表格
<form onsubmit="return false;" enctype="multipart/form-data" name="product-update" id="product-update" method="post">
<div class="attributes-to-product">
<a class="bluebtn" id="set-attribute" onclick="setAttributes();">Seteaza</a>
<table class="table">
<thead>
<tr>
<th>Atributul</th>
<th>Text</th>
<th></th>
</tr>
</thead>
<tbody>
{if $atribute}
{section name=li loop=$atribute.attribute_id}
<tr id="attribute-row_{$atribute.attribute_id[li]}" class="attrClass">
<td><input type="text" name="product_attribute[{$atribute.attribute_id[li]}][name]" value="{$atribute.name[li]}" autocomplete="off" /><input type="hidden" name="product_attribute[{$atribute.attribute_id[li]}][attribute_id]" value="{$atribute.attribute_id[li]}" /></td>
<td><textarea name="product_attribute[{$atribute.attribute_id[li]}][product_attribute_description][2][text]">{$atribute.text[li]}</textarea></td>
<td><a class="bluebtn" id="delrow" onclick="$s('#attribute-row_{$atribute.attribute_id[li]}').remove();">Inlatura</a></td>
</tr>
{/section}
{else}
<tr class="information">
<td colspan="3" align="center">Nu aveti setat niciun atribut</td>
</tr>
{/if}
</tbody>
</table>
</div>
</form>
How can I compare the database attribute entries for the specific product i want to modify with form inputs? 如何将要修改的特定产品的数据库属性条目与表单输入进行比较?
Not understand what you want or where is the mistake, but i like gamble and i try to answer at your question. 不明白您想要什么或错误在哪里,但是我喜欢赌博,并且尝试回答您的问题。 But, first, i wanna try to explain that debug/correct/help user without code is not so simple. 但是,首先,我想解释一下没有代码的调试/纠正/帮助用户并不是那么简单。 You use jQuery tag, but i didn't see jQuery code, and maybe the problem is in that part of code. 您使用jQuery标签,但我没有看到jQuery代码,也许问题出在那部分代码中。
Now i'll come back to my gamble, if i understand the problem, you wanna remove an attribute of an html tag, so why not try with jQuery.removeAttr() function? 现在,我将重新开始赌博,如果我理解问题所在,就想删除html标签的属性,那么为什么不尝试使用jQuery.removeAttr()函数呢?
Solved with 解决了
if(isset($_POST['product_attribute'])){
$att_id = array();
foreach ($_POST['product_attribute'] as $product_attribute) {
$att_id[] = $product_attribute['attribute_id'];
if($product_attribute['attribute_id']) {
$query = sprintf("DELETE FROM product_attribute WHERE product_id ='%d' AND attribute_id='%d'", (int)$product_id, (int)$product_attribute['attribute_id']);
$database->delete($query);
foreach ($product_attribute['product_attribute_description'] as $product_attribute_description) {
$query = sprintf("INSERT INTO product_attribute SET product_id='%d', attribute_id='%d', text='%s'", (int)$product_id, (int)$product_attribute['attribute_id'], $product_attribute_description['text']);
$database->insert($query);
}
}
}
$vals = implode(",", $att_id);
$query = "DELETE FROM product_attribute WHERE product_id = $product_id AND attribute_id NOT IN($vals)";
$database->delete($query);
}else{
$query = sprintf("DELETE FROM product_attribute WHERE product_id = '%d'", $product_id);
$database->delete($query);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.