没有在$ _post数组中获得价值

时间:2017-06-15 16:50:12

标签: php

这是我的表单代码,当我提交表单时我没有在post数组中获取数据,我尝试了多种方法但是在点击按钮后没有收到任何响应,这里是代码

from bisect import bisect_right

def choose_best_sum(t, k, ls):
    """
    Find the highest sum of `k` values from `ls` such that sum <= `t`
    """
    # enough values passed?
    n = len(ls)
    if n < k:
        return None

    # remove unusable values from consideration
    ls = sorted(ls)
    max_valid_value = t - sum(ls[:k - 1])
    first_invalid_index = bisect_right(ls, max_valid_value)
    if first_invalid_index < n:
        ls = ls[:first_invalid_index]
        # enough valid values remaining?
        n = first_invalid_index   # == len(ls)
        if n < k:
            return None

    # can we still exceed t?
    highest_sum = sum(ls[-k:])
    if highest_sum <= t:
        return highest_sum

    # we have reduced the problem as much as possible
    #   and have not found a trivial solution;
    # we will now brute-force search combinations of (k - 1) values
    #   and binary-search for the best kth value
    best_found = 0
    # n = len(ls)      # already set above
    r = k - 1
    # itertools.combinations code copied from
    #   https://docs.python.org/3/library/itertools.html#itertools.combinations
    indices = list(range(r))
    # Inserted code - evaluate instead of yielding combo
    prefix_sum = sum(ls[i] for i in indices)          #
    kth_index = bisect_right(ls, t - prefix_sum) - 1  # location of largest possible kth value
    if kth_index > indices[-1]:                       # valid with rest of combination?
        total = prefix_sum + ls[kth_index]            #
        if total > best_found:                        #
            if total == t:                            #
                return t                              #
            else:                                     #
                best_found = total                    #
    x = n - r - 1    # set back by one to leave room for the kth item
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + x:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        # Inserted code - evaluate instead of yielding combo
        prefix_sum = sum(ls[i] for i in indices)          #
        kth_index = bisect_right(ls, t - prefix_sum) - 1  # location of largest possible kth value
        if kth_index > indices[-1]:                       # valid with rest of combination?
            total = prefix_sum + ls[kth_index]            #
            if total > best_found:                        #
                if total == t:                            #
                    return t                              #
                else:                                     #
                    best_found = total                    #
        else:
            # short-circuit! skip ahead to next level of combinations
            indices[r - 1] = n - 2

    # highest sum found is < t
    return best_found

4 个答案:

答案 0 :(得分:1)

更改表单操作。并更改按钮类型

  

阻止报价

<form class="form-horizontal form-label-left" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="submit" value="submit" name="submit" class="btn btn-success" value="Submit" />

答案 1 :(得分:0)

立即尝试:

<?php
        if (isset($_POST['submit'])) {
            echo print_r($_POST);
        }
    ?> 

    // other stuff

    <form class="form-horizontal form-label-left" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
      <span class="section">Info</span>

      <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Name <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
          <input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2" name="name" placeholder="both name(s) e.g Jon Doe" required="required" type="text">
        </div>
      </div>
      <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Description <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
          <textarea id="description" required="required" name="description" class="form-control col-md-7 col-xs-12"></textarea>
        </div>
      </div>
      <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Type <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
          <select id="type" name="type" required="required" class="selectpicker form-control col-md-7 col-xs-12">
            <option title="Combo 1">Weekly</option>
            <option title="Combo 2">Monthly</option>
            <option title="Combo 3">Annually</option>
          </select>
        </div>
      </div>

      <div class="ln_solid"></div>
      <div class="form-group">
        <div class="col-md-6 col-md-offset-3">
          <button  type="submit" value="submit" name="submit" class="btn btn-success">Submit</button>
        </div>
      </div>
    </form>

答案 2 :(得分:0)

更改代码中的这些行

<form class="form-horizontal form-label-left" method="post" action="#" >
<button  type="submit" value="submit" name="submit" class="btn btn-success">Submit</button>

使用

<form class="form-horizontal form-label-left" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input  type="submit" value="submit" name="submit" class="btn btn-success" value="Submit" />

答案 3 :(得分:0)

我的代码中有脚本

<script src="../vendors/jquery/dist/jquery.min.js"></script>

我将我的按钮命名为“提交”,因此错误在控制台中

submit is not a function

所以当我将我的按钮名称更改为“btnsubmit”时,它可以正常工作。 当您将按钮命名为submit时,将覆盖表单上的submit()函数。