无法使用jQuery Submit方法提交表单

时间:2018-09-12 13:14:33

标签: php jquery

我想做的是:

  1. 在用户登录时发布表单。
  2. 但是如果他未登录,则会向用户显示弹出式登录名。
  3. ,然后在该弹出窗口中将重定向URL添加到隐藏字段中。
  4. 当弹出窗口打开并且我登录时,将我重定向到该表单。

  5. 但是当我尝试提交表单时,它并未提交。

    //  submit button in form
    $('#submitcompanyEnquiry').on('click',function(e){
        e.preventDefault();
        //get data attr to check if user is login
        if($('#companyEnquiry').data('login')){
            //companyEnquiry =>form id 
            //here i try to submit form  
             console.log('testing');    --->it is working
            jQuery('#companyEnquiry').submit();   ---> //the problem is here this piece of code is executing
        }else{
            if($('#companyEnquiry').attr('action')!=''){
                //here i added the current url to hidden field latter to used for redirection
                $('#loginForm #redirectUrl').val($('#companyEnquiry').data('seotitle'));
            }
            //here the login popup is trigger.
            jQuery("#login").trigger('click');
        }
    });
    

我确认的事情:

  1. 请确保唯一的ID与 提供的名称。

  2. 在if块中包含一些值 运行,但是我提到的代码行。

PHP部分运行正常,我已经删除了e.preventDefault(); 效果很好,但无法实现必需的功能。

HTML代码

                <form action="<?=Route::url('default',array('controller'=>'contact','action'=>'user_contact'))?>" data-login="<?php echo $data; ?>" data-seotitle="<?=Route::url('company', array('controller'=>'listing','seotitle'=>$company_seotitle))?>" id="companyEnquiry" method="post">
                    <input type="hidden" name="company_to" value="<?php echo $id; ?>">
                     <?php if (!$auth->logged_in()) { ?>
                        <div class="input-group searchbox">
                            <input type="text" class="form-control search" placeholder="Name" name="name" required aria-describedby="basic-addon1">
                        </div>
                    <?php  }else { ?>
                        <div class="input-group searchbox">
                            <input type="text" class="form-control search" placeholder="Name" required value="<?php echo $auth->get_user()->company_name; ?>" name="name" aria-describedby="basic-addon1">
                        </div>
                    <?php } ?>
                    <?php if (!$auth->logged_in()) { ?>
                        <div class="input-group searchbox">
                            <input type="email" class="form-control search" placeholder="email" required name="company_from" aria-describedby="basic-addon1">
                        </div>
                    <?php  }else { ?>
                       <div class="input-group searchbox">
                            <input type="email" class="form-control search" placeholder="email" required value="<?php echo $auth->get_user()->companyemail; ?>"  name="company_from" aria-describedby="basic-addon1">
                        </div>
                    <?php } ?>
                    <?php if ($auth->logged_in()) { ?>
                    <div class="input-group searchbox">
                        <input type="text" class="form-control search" placeholder="phone number" required name="phone" value="<?php echo $auth->get_user()->company_phone_1; ?>" aria-describedby="basic-addon1">
                    </div>
                    <?php } else { ?>
                        <div class="input-group searchbox">
                            <input type="text" class="form-control search" placeholder="phone number" required name="phone" aria-describedby="basic-addon1">
                        </div>
                    <?php } ?>
                    <div class="input-group searchbox">
                        <input type="text" class="form-control search" placeholder="subject" required name="subject" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group searchbox">
                        <input type="text" class="form-control search" placeholder="message" required name="message" aria-describedby="basic-addon1">
                    </div>
                    <input data-login="<?php echo $data; ?>" id="submitcompanyEnquiry"  type="submit" name="submit" value="SEND" class="form-control blue-btn send-btn">
                </form>

1 个答案:

答案 0 :(得分:0)

仅当您将登录变量添加到ID为companyEnquiry的表单中时,问题才会存在

检查是否已将其添加为正确的参数,因为jquerys数据功能将仅读取其前面带有“ data-”标签的值。

因此您的php代码应如下所示:

echo '<form id="companyEnquiry" ' . ($login ? 'data-login="1"' : '' . '>