在创建表单的同一弹出窗口中显示错误消息

时间:2017-12-13 06:59:25

标签: php jquery ajax wordpress jquery-plugins

我已经制作了一个显示在弹出窗口中的表单现在我想以这样的方式验证该表单,即错误消息应该显示在创建表单的同一弹出窗口中,并且我还想停止提交事件错误数组包含一些错误这是我到目前为止使用的代码。

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><?php the_title(); ?></h4>
            </div>
            <div class="modal-body">
                <p style="font-size: 18px; font-weight: normal; font-family: inherit;">
                    After clicking claim this item will be reserved for you and you will 
                    receive an invoice from <?php echo get_the_author(); ?> with 
                    instructions for completing payment.
                </p>


                <?php 
                    if($errArray != '') {
                        foreach ($errArray as $error) {
                ?>
                    <div class="btn btn-danger">
                        <ul>
                        <?php
                            if($error != '') {
                        ?>

                            <li><?php echo $error; ?></li>

                        <?php } ?>
                        </ul>
                    </div>
                <?php
                        }
                    }
                ?>

                <form class="form-horizontal" action="#" method="POST">

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class=" col-md-5 control-label h4">Name *</lable>
                        <div class="col-md-6">
                            <input type="text" name="name" id="name" placeholder="Enter your Name" class="form-control" required />
                        </div>
                    </div>

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">Email *</lable>
                        <div class="col-md-6">
                            <input type="email" name="email" id="email" placeholder="Enter your Email" class="form-control" required/>
                        </div>
                    </div>


                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">Shipping Address *</lable>
                        <div class="col-md-6">
                            <input type="text" name="shipping_address" id="ship_add" placeholder="Enter your Shipping Address" class="form-control" required/>
                        </div>
                    </div>

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">City *</lable>
                        <div class="col-md-6">
                            <input type="text" name="city" id="city" placeholder="Enter your City" class="form-control" required/>
                        </div>
                    </div>

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">State *</lable>
                        <div class="col-md-6">
                            <input type="text" name="state" id="state" placeholder="Enter your State" class="form-control" required/>
                        </div>
                    </div>

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">Zip *</lable>
                        <div class="col-md-6">
                            <input type="text" name="zip" id="zip" placeholder="Enter your Zip Code" class="form-control" required/>
                        </div>
                    </div>

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">Phone *</lable>
                        <div class="col-md-6">
                            <input type="text" name="phone" id="phone" placeholder="Enter your Phone Number" class="form-control" required/>
                        </div>
                    </div>

                    <div class="form-group" style="margin: 10px; padding: 0px;">
                        <lable class="control-label col-md-5 h4">Notes</lable>
                        <div class="col-md-6">
                            <input type="text" id="notes" name="notes" class="form-control"/>
                        </div>
                    </div>

                    <div class="col-md-12">
                        <div>
                            <button type="button" class="btn btn-default pull-right" data-dismiss="modal">Close</button>
                        </div>
                        <div>
                            <input type="submit" value="Claim" class="btn btn-primary pull-right" style="margin-right: 2%;"/>
                        </div>
                    </div>
                </form>
            </div>
            <div style="clear: both;"></div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

<?php
if (!empty($_POST)) {
    $product_id = get_the_ID();
    $name = esc_attr($_POST['name']);
    $email = esc_attr($_POST['email']);
    $s_address = esc_attr($_POST['shipping_address']);
    $city = esc_attr($_POST['city']);
    $state = esc_attr($_POST['state']);
    $zip = esc_attr($_POST['zip']);
    $phone = esc_attr($_POST['phone']);
    $notes = esc_attr($_POST['notes']);

    $errArray = [];
    if(empty($name)) {
        echo "Name is required";
        array_push($errArray, 'Name is requried');
    }
    if(empty($email)){
        array_push($errArray, 'Email is required');
    }
    if(empty($s_address)){
        array_push($errArray, 'Shipping Address is required');   
    }
    if(empty($city)){
        array_push($errArray, 'City is required');
    }
    if(empty($state)) {
        array_push($errArray, 'State is required');
    }
    if(empty($zip)) {
        array_push($errArray, 'Post Code is required');
    }
    if(empty($phone)){
        array_push($errArray, 'Phone is required');
    }

    if(empty($errArray)){
        global $woocommerce;
          $address = array(
              'first_name' => $name,
              'email'      => $email,
              'phone'      => $phone,
              'address_1'  => $s_address,
              'city'       => $city,
              'state'      => $state,
              'postcode'   => $zip,
          );
          // Now we create the order
          $order = wc_create_order();
          // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
          $order->add_product( get_product($product_id), 1 ); // This is an existing SIMPLE product
          $order->set_address( $address, 'billing' );
          //
          $order->calculate_totals();
          $order->update_status("wc-new", 'Imported order', TRUE);



        global $wpdb;

        $table = wp_store_claims;
        $data = array(
            'product_id' => $product_id,
            'name' => $name,
            'email' => $email,
            's_address' => $s_address,
            'city' => $city,
            'state' => $state,
            'zip' => $zip,
            'notes' => $notes,
            'phone' => $phone,
        );

        $success = $wpdb->insert($table, $data);
    } else {
        return false;
    }
  }

我被困在这里任何能帮助我的人都会很棒。提前谢谢

0 个答案:

没有答案