【发布时间】:2017-03-11 19:58:47
【问题描述】:
感谢阅读:D
我正在创建一个在线订单系统。您将看到 3 个输入。 Partnumber、Quantity 和 Price。您只需填写 Partnumber 和 Quantity。将通过我的数据库检查价格是否合适。我试图创建该功能,但仍然无法正常工作。但无论如何。我包含了一个 jQuery 函数,因此当您想要订购更多产品时,您可以在表单中添加额外的行。
一点点sn-p,这样你就知道我的意思了。
$('#addpart').click(function(){
var loop = $('#loop').val();
var html;
html = '<p>';
html += '<input type="text" name="part[]" style="margin-left:20px;" class="text medium" id="part" placeholder="SP partnumber" />';
html += '<input type="text" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />';
html += '<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly />';
html += '</p>';
for (i = 0; i < loop; i++) {
$("#form").append(html);
}
});
因此,当您发布此表单时,我们不知道它将有多少个字段。有时是 5 个订单行,有时是 10 个。
所以我开始在我的输入字段中使用[]。
输入name 属性将是这样的:"part[]"。
表单将使用Validation 类进行验证。
为了向我的班级展示这个页面会很长 ghehe。
这是一个小sn-p如何使用这个类/函数,以便您了解结构。
if(toxInput::exists()){
if(toxtoken::check(toxInput::get('token'))){
$toxValidate = new toxValidate();
$toxValidation = $toxValidate->check($_POST, array(
'name' => array(
'required' => true,
'min' => 2,
'max' => 50
)
));
if($toxValidation->passed()){
etcetcetc..
所以订单应该是这样的:
$toxValidate = new toxValidate();
$toxValidation = $toxValidate->check('$_POST', array(
'part1' => array('required' => TRUE, 'maxlength' => 14),
'part2' => array('required' => TRUE, 'maxlength' => 14),
'part3' => array('required' => TRUE, 'maxlength' => 14),
'part4' => array('required' => TRUE, 'maxlength' => 14),
'part5' => array('required' => TRUE, 'maxlength' => 14)
));
我如何为每个填写的行打印此行'part1' => array('required' => TRUE, 'maxlength' => 14)。我用foreach 和[] 尝试了几种方法。
一切都没有工作.. :(
【问题讨论】:
-
发布
Validation类文档的链接。 -
这里我们开始:paste.ofcode.org/37W7WePcu57a8mf6QLk6vDX 但它只需要在
Validation->check函数中创建数组。从提交的表格中。 Check 需要检查每个input。part[0]、part[1]等 -
当您提交带有数组输入的表单时,例如
part[],然后$_POST['part']将是一个数组。注意我说的是$_POST['part']而不是$_POST['part[]']。您的课程仅限于非数组输入,因为它正在寻找后者。您需要添加逻辑来处理数组输入。 -
谢谢,但不能将 foreach
part[]转换为part' => TRUE,以便我的validation班级检查它吗?我的validation类将检查数组中的itemsallready。我需要将part数组与我在check函数中使用的forms数组结合起来。 -
或者最好绕过
validation类并以其他方式检查它,这样会更容易?