热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHPCodeigniter表单使用JQuery提交-PHPCodeigniterformsubmitusingJQuery

IamnewtoPHPCodeigniterframework.IamdesigningapageinwhichIamusingalink.Onclicking

I am new to PHP Codeigniter framework. I am designing a page in which I am using a link. On clicking the link it is calling a jquery function, which submits form through jquery. I have used codeigniter form validation methods for server side validation and for the timebeing I have disabled the client side validation.

我是PHP Codeigniter框架的新手。我正在设计一个我正在使用链接的页面。单击链接时,它调用一个jquery函数,该函数通过jquery提交表单。我已经使用codeigniter表单验证方法进行服务器端验证,并且我已经禁用了客户端验证。

The problem is that in this process when the form is submitted through jquery, the codeigniter form validation method is not working.

问题是,在此过程中,通过jquery提交表单时,codeigniter表单验证方法不起作用。

But if I am using a submit button to submit the form then the codeigniter form validation method works perfectly.

但是如果我使用提交按钮提交表单,则codeigniter表单验证方法可以正常工作。

Please advise me what to do if I need to submit the form through jquery and use the codeigniter form validation method.

如果我需要通过jquery提交表单并使用codeigniter表单验证方法,请告诉我该怎么办。

Please find the code below:

请找到以下代码:

Login Form:


index.php/login/login_form" >
    

Login

Email:
Password:



jquery function to submit the form on clicking the "link":

jquery函数在单击“链接”时提交表单:


    $("#login-submit").click(function()
    {
    $('#login-form').submit();
    return false;
    });

Controller function:


    public function login_form()
    {

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $data['title'] = 'Log In';

        $data['errorMessage'] = '';
        $this->form_validation->set_rules('user-name', 'Email', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == FALSE)
        {
        $this->load->view('templates/header', $data);
        $this->load->view('login', $data);
        $this->load->view('templates/footer');
        }

        else
        {
        $this->load->view('templates/header', $data);
        $this->load->view('templates/menu');
        $this->load->view('index', $data);
        $this->load->view('templates/footer');
        }

    }

Here if I click on the "Submit button of the form, then the codeigniter validation works for user-name and password fields. But if I click the link with id="login-submit", then it calls the jquery function and the form get submitted. But the codeigniter validation does not work for user-name and password fields this time.

在这里,如果我点击表单的“提交”按钮,那么codeigniter验证适用于用户名和密码字段。但是如果我点击带有id =“login-submit”的链接,那么它会调用jquery函数和表单但是,这次codeigniter验证对用户名和密码字段不起作用。

I need to submit the form through the link and the codeigniter validation function should work for this.

我需要通过链接提交表单,并且codeigniter验证功能应该适用于此。

Thanks in Advance.....

提前致谢.....

2 个解决方案

#1


2  

Use .preventDefault() instead of just returning false on the anchor click event.

使用.preventDefault()而不是仅在锚点击事件上返回false。

This has happened to me before and it seems to me that there is a conflict somewhere using .submit() inside a click event and returning false to stop the anchor's default behavior.

这在我之前发生过,在我看来,在click事件中使用.submit()并返回false以停止锚点的默认行为。

#2


2  

The code above is working fine. Actually I made a silly mistake in my code. I used following code for jQuery and it is working now.

上面的代码工作正常。实际上我在代码中犯了一个愚蠢的错误。我使用了以下jQuery代码,它现在正在运行。


    $("#login-submit").click(function(e) {
        e.preventDefault();
        $('#login-form').submit();
    });

Thanks


推荐阅读
  • 本文介绍了如何使用jQuery和AJAX来实现动态更新两个div的方法。通过调用PHP文件并返回JSON字符串,可以将不同的文本分别插入到两个div中,从而实现页面的动态更新。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • wpf+mvvm代码组织结构及实现方式
    本文介绍了wpf+mvvm代码组织结构的由来和实现方式。作者回顾了自己大学时期接触wpf开发和mvvm模式的经历,认为mvvm模式使得开发更加专注于业务且高效。与此同时,作者指出mvvm模式相较于mvc模式的优势。文章还提到了当没有mvvm时处理数据和UI交互的例子,以及前后端分离和组件化的概念。作者希望能够只关注原始数据结构,将数据交给UI自行改变,从而解放劳动力,避免加班。 ... [详细]
  • 本文介绍了使用FormData对象上传文件同时附带其他参数的方法。通过创建一个表单,将文件和参数添加到FormData对象中,然后使用ajax发送POST请求进行文件上传。在发送请求时,需要设置processData为false,告诉jquery不要处理发送的数据;同时设置contentType为false,告诉jquery不要设置content-Type请求头。 ... [详细]
  • 本文介绍了DataTables插件的官方网站以及其基本特点和使用方法,包括分页处理、数据过滤、数据排序、数据类型检测、列宽度自动适应、CSS定制样式、隐藏列等功能。同时还介绍了其易用性、可扩展性和灵活性,以及国际化和动态创建表格的功能。此外,还提供了参数初始化和延迟加载的示例代码。 ... [详细]
  • 本文介绍了iOS开发中检测和解决内存泄漏的方法,包括静态分析、使用instruments检查内存泄漏以及代码测试等。同时还介绍了最能挣钱的行业,包括互联网行业、娱乐行业、教育行业、智能行业和老年服务行业,并提供了选行业的技巧。 ... [详细]
  • 本文介绍了一个Magento模块,其主要功能是实现前台用户利用表单给管理员发送邮件。通过阅读该模块的代码,可以了解到一些有关Magento的细节,例如如何获取系统标签id、如何使用Magento默认的提示信息以及如何使用smtp服务等。文章还提到了安装SMTP Pro插件的方法,并给出了前台页面的代码示例。 ... [详细]
  • ps:写的第一个,不足之处,欢迎拍砖---只是想用自己的方法一步步去实现一些框架看似高大上的小功能(比如说模型中的toArraytoJsonsetAtt ... [详细]
  • angular.element使用方法及总结
    2019独角兽企业重金招聘Python工程师标准在线查询:http:each.sinaapp.comangularapielement.html使用方法 ... [详细]
author-avatar
广东蒗缦m莎
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有