Skip to content Skip to sidebar Skip to footer

How To Do Validation Inside Foreach

I have a form which has some text fields. Text fields may increase or decrease according to database table. If there are 3 warehouses in database it will show 3 text boxes with ava

Solution 1:

Try to use html5 required attribute. This set of code may help you.

<inputtype="number"min="1"max="<?phpecho$data->wh_product_qty; ?>"name="<?phpecho$data->wh_warehouse_id;?>"required="required"placeholder="<?phpecho$data->wh_product_qty; ?> Available" />

Solution 2:

You cannot use an id multiple times in the document, it needs to be unique. Either use classes to identify/group elements, or enumerate the ids:

<?php$i = 0;
    if (!empty($ware_details)) {
         foreach ($ware_detailsas$data) {
              ?><divclass="form-group"><labelfor="exampleInputEmail1"><?phpecho$data->warehouse_name; ?></label><inputtype="number"id="return_pro<?phpecho$i; ?>"class="form-control"base_url="<?phpecho site_url(); ?>"name="<?phpecho$data->wh_warehouse_id;?>"placeholder="<?phpecho$data->wh_product_qty; ?> Available"><divclass="dataDrop"></div><inputtype="hidden"id="quantity<?phpecho$i; ?>"value="<?phpecho$data->wh_product_qty; ?>"/></div><?php$i++;
        } }?><script>functioncheck(i) {
    var stock = document.getElementById('quantity'+i).value;
        var return_stock = document.getElementById('return_pro'+i).value;
        if (parseFloat(stock) < parseFloat(return_stock))
        {
            alert("Return product can't be more than total stock");
            document.getElementById('return_pro'+i).focus();
            returnfalse;
        }
        //alert(return_stock);returnfalse;
}
// then in the validation trigger:for (var i=0; i< <?phpecho$i; ?>; i++)
         check(i);

Post a Comment for "How To Do Validation Inside Foreach"