未定义的索引和表单不能正常工作

时间:2016-04-24 09:33:11

标签: javascript php jquery forms

Hello Friends我的页面上有尺寸选项问题 这是尺寸的形式

<!-- Sizes And Quantity [Onclick Modal]-->
<div class="form-group col-md-3">
    <label>Sizes & Quntity<span class="required">*</span>:</label>
    <button class="btn btn-default form-control btn-info" onclick="jQuery('#sizesModal').modal('toggle'); return false;">Sizes & Quantity</button>
</div>
<!-- Sizes & Quantity Preview -->
<div class="form-group col-md-3">
    <label for="sizes">Size & Quantity Preview:</label>
    <input type="text" name="sizes" id="sizes" class="form-control" value="<?= ((isset($_POST['sizes']))?sanitize($_POST['sizes']):''); ?>" disabled/>
</div>

这是点击按钮时使用的模态

<!-- Modal for sizes & quantity -->
<div class="modal fade" id="sizesModal" tabindex="-1" role="dialog" aria-labelledby="sizesModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="sizesModalLabel">Size & Quantity</h4>
      </div>
      <div class="modal-body">
        <div class="container-fluid">
            <?php for($i=1; $i <= 12; $i++): ?>
                <!-- Size Edit -->
                <div class="form-group col-md-4">
                    <label for="size<?= $i; ?>">Size</label>
                    <input type="text" class="form-control" id="size<?= $i; ?>" name="size<?= $i; ?>" value="<?= ((!empty($sArray[$i-1]))?$sArray[$i-1]:''); ?>" />
                </div>
                <!-- Quantity Edit -->
                <div class="form-group col-md-2">
                    <label for="qty<?= $i; ?>">Quantity</label>
                    <input type="number" class="form-control" id="qty<?= $i; ?>" name="qty<?= $i; ?>" value="<?= ((!empty($qArray[$i-1]))?$qArray[$i-1]:''); ?>" min="0"/>
                </div>
            <?php endfor; ?> 
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" onclick="updateSizes();jQuery('#sizesModal').modal('toggle');return false;">Save changes</button>
      </div>
    </div>
  </div>
</div>

这是更新尺寸的功能

function updateSizes(){
    var sizeString ='';
    for (var i= 1; i<=12; i++){
        if(jQuery('#size'+i).val() != ''){
            sizeString += jQuery('#size'+i).val()+':'+jQuery('#qty'+i).val()+',' ;
        }
    }
    jQuery('#sizes').val(sizeString);
}

我的问题是
1.当表单被提交并且如果它给出错误时,尺寸字段不应该是黑色的,它应该已经输入了值,就像每个场所一样,它应该做同样的事情它不应该删除它们以便用户不要#39;需要重新输入它们
2.无论何时我使用&#39;尺寸&#39;在PHP中它给出了错误,因为大小未定义。

这是我在同一页面上使用它的PHP

if ($_POST) {
if (!empty($_POST['sizes'])) {
                $sizeString = sanitize($_POST['sizes']);
                $sizeString = rtrim($sizeString,',');
                echo $sizeString;
                $sizesArray = explode(',', $sizeString);
                $sArray = array();
                $qArray = array();
                foreach ($sizesArray as $ss) {
                    $s = explode(':', $ss);
                    $sArray[] = $s[0];
                    $qArray[] = $s[1];
                }
            }
            else {
                $sizesArray = array();
            }
$required = array('//Some Arrays' 'sizes');
            foreach ($required as $field) {
                if ($_POST[$field] == '') {
                    $errors[] = 'All Fields with <span class="required">*</span> are required.';
                    break;
                }
            }

我已经重新检查了整个代码5次,但我根本找不到任何东西。 请帮帮我。

0 个答案:

没有答案