douchun1900 2018-07-26 11:34
浏览 67

如何在PHP中为动态添加/删除输入字段创建错误处理程序

I have created several form fields. In which first one is normal input text field while other are dynamic add / remove input fields. I have made the first field as required by using PHP arguments. It is working fine. But I am not able to make the add / remove input fields as required one using PHP arguments.

I want the error handler for add / remove input fields in same format as I have created.

Following is my code:

<?php

if ( isset( $_POST['project_draft'] ) ) {

$project_title = wp_strip_all_tags( $_POST['project_title'] );   
$fund_details->allocate_items = array_unique( array_map( 'sanitize_text_field', $_POST['allocate_items'] ) );
$fund_details->allocate_amount = array_map( 'sanitize_text_field', $_POST['allocate_amount'] );

    global $project_draft_errors;
    $project_draft_errors = new WP_Error;

if ( empty( $project_title ) ) {

    $project_draft_errors->add('field', 'Project title is required.');

    } else

if ( strlen( $project_title ) > '100' ) {

    $project_draft_errors->add('field', 'Project title should not be more than 100 character.');

    }

if ( is_wp_error( $project_draft_errors ) ) {

    foreach ( $project_draft_errors->get_error_messages() as $project_draft_error ) { ?>

      <div class="alert alert-danger" role="alert"><strong>ERROR</strong>: <?php echo $project_draft_error; ?></div>

<?php } }

}

?>

<form method="POST" enctype="multipart/form-data">

<div class="panel panel-default">

  <div class="panel-body">

  <div class="form-group">

    <label for="project_title">Project Title <b style="color:#FF0000;">*</b></label>

    <input type="text" class="form-control" name="project_title" id="project_title" placeholder="Give your project a nice title...">

  </div>  

  </div>

</div>

<div class="panel panel-default">

  <div class="panel-heading"><center><b>Allocation of Funds</b></center></div>

  <div class="panel-body">

    <div class="row">

      <div class="col-md-5"><label>Allocation Items <b style="color:#FF0000;">*</b></label></div>
      <div class="col-md-5"><label>Amount <b style="color:#FF0000;">*</b></label></div>
      <div class="col-md-2"></div>

    </div>

    <div class="row">

      <div class="col-md-5">

        <div class="form-group">

          <input type="text" class="form-control" name="allocate_items[]" placeholder="">

        </div>

      </div>

      <div class="col-md-5">

        <div class="form-group">

          <input type="text" class="form-control" name="allocate_amount[]" placeholder="">

        </div>

      </div>

      <div class="col-md-2">

        <button type="button" class="btn btn-success" id="add-allocation-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add</button>

      </div>

    </div>

    <div id="fund-allocation-fields">

    </div>

<p class="help-block"><i>Total amount must be equal to the goal amount.</i></p>

  </div>

</div>

<input class="btn btn-warning btn-block" type="submit" name="project_draft" value="Draft" style="border-radius: 0px;">

</form>

<script type="text/javascript">

  var i = 0;

  jQuery(document).ready(function($) {

    //fadeout selected item and remove
    $(document).on('click', '#remove-allocation-fields', function(event) {

      event.preventDefault();
      $(this).parent().fadeOut(300, function() {
        $(this).parent().empty();
        return false;

      });

    });

    var rows = '<div class="fund-fields"><div class="row"><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_items[]" placeholder=""></div></div><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_amount[]" placeholder=""></div></div><div class="col-md-2"><button type="button" class="btn btn-danger" id="remove-allocation-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</button></div></div><div class="clear"></div></div>';

    //add input
    $('#add-allocation-fields').click(function() {

      $(rows).fadeIn("slow").appendTo('#fund-allocation-fields');
      i++;
      return false;

    });

  });

</script>

Please Help me... Thanks in advance...

  • 写回答

1条回答 默认 最新

  • dtyw10299 2018-07-26 12:01
    关注

    Try below code:-

    <?php
    
    if ( isset( $_POST['project_draft'] ) ) {
    
    $project_title = wp_strip_all_tags( $_POST['project_title'] );   
    $fund_details->allocate_items = array_unique( array_map( 'sanitize_text_field', $_POST['allocate_items'] ) );
    $fund_details->allocate_amount = array_map( 'sanitize_text_field', $_POST['allocate_amount'] );
    
        global $project_draft_errors;
        $project_draft_errors = new WP_Error;
    
    
    if ( empty( $project_title ) ) {
    
        $project_draft_errors->add('field', 'Project title is required.');
    
        } else
    
    if ( strlen( $project_title ) > '100' ) {
    
        $project_draft_errors->add('field', 'Project title should not be more than 100 character.');
    
        }
        /* validating blank values only */
        unset($_POST['project_title']);
        foreach($_POST as $postDataKey=>$postDataVal){
            if ( empty( $postDataVal ) ) {
    
                $project_draft_errors->add('field', $postDataKey.' is required.');
    
            }
        }
    
    if ( is_wp_error( $project_draft_errors ) ) {
    
        foreach ( $project_draft_errors->get_error_messages() as $project_draft_error ) { ?>
    
          <div class="alert alert-danger" role="alert"><strong>ERROR</strong>: <?php echo $project_draft_error; ?></div>
    
    <?php } }
    
    }
    
    ?>
    
    <form method="POST" enctype="multipart/form-data">
    
    <div class="panel panel-default">
    
      <div class="panel-body">
    
      <div class="form-group">
    
        <label for="project_title">Project Title <b style="color:#FF0000;">*</b></label>
    
        <input type="text" class="form-control" name="project_title" id="project_title" placeholder="Give your project a nice title...">
    
      </div>  
    
      </div>
    
    </div>
    
    <div class="panel panel-default">
    
      <div class="panel-heading"><center><b>Allocation of Funds</b></center></div>
    
      <div class="panel-body">
    
        <div class="row">
    
          <div class="col-md-5"><label>Allocation Items <b style="color:#FF0000;">*</b></label></div>
          <div class="col-md-5"><label>Amount <b style="color:#FF0000;">*</b></label></div>
          <div class="col-md-2"></div>
    
        </div>
    
        <div class="row">
    
          <div class="col-md-5">
    
            <div class="form-group">
    
              <input type="text" class="form-control" name="allocate_items[0]" placeholder="">
    
            </div>
    
          </div>
    
          <div class="col-md-5">
    
            <div class="form-group">
    
              <input type="text" class="form-control" name="allocate_amount[0]" placeholder="">
    
            </div>
    
          </div>
    
          <div class="col-md-2">
    
            <button type="button" class="btn btn-success" id="add-allocation-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add</button>
    
          </div>
    
        </div>
    
        <div id="fund-allocation-fields">
    
        </div>
    
    <p class="help-block"><i>Total amount must be equal to the goal amount.</i></p>
    
      </div>
    
    </div>
    
    <input class="btn btn-warning btn-block" type="submit" name="project_draft" value="Draft" style="border-radius: 0px;">
    
    </form>
    
    <script type="text/javascript">
    
      var i = 0;
    
      jQuery(document).ready(function($) {
    
        //fadeout selected item and remove
        $(document).on('click', '#remove-allocation-fields', function(event) {
    
          event.preventDefault();
          $(this).parent().fadeOut(300, function() {
            $(this).parent().empty();
            return false;
    
          });
    
        });
    
        var rows = '<div class="fund-fields"><div class="row"><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_items[]" placeholder=""></div></div><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_amount[]" placeholder=""></div></div><div class="col-md-2"><button type="button" class="btn btn-danger" id="remove-allocation-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</button></div></div><div class="clear"></div></div>';
    
        //add input
        $('#add-allocation-fields').click(function() {
    
          $(rows).fadeIn("slow").appendTo('#fund-allocation-fields');
          i++;
          return false;
    
        });
    
      });
    
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集