你是怎么做的jQuery与codeIgniter序列化? [英] How do you do jquery serialize with CodeIgniter?

查看:137
本文介绍了你是怎么做的jQuery与codeIgniter序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery和codeIgniter做到这一点。好了,所以基本上我有一个要求用户输入用户名,密码和电子邮件中的登记表格。一旦提交表单时,将jQuery的序列化的形式,并给它一个过程页面,回显值在控制器,然后的JavaScript将在原来的查看警报后的值。到目前为止,这是行不通的。什么都没有惊动。这是我的code:

(控制器) register.php:

 < PHP如果(!定义(BASEPATH'))
        退出(允许无直接脚本访问');

    类注册扩展是CI_Controller
    {
        公共职能步骤1()
        {
        $这个 - >负载>查看('registration_step1');
        }
        公共职能step1_process(){
        后续代码var_dump($ _ POST);
        }
        公共职能步骤2()
        {
        $这个 - >负载>查看('registration_step2');
        }
        公共职能步骤3()
        {
        $这个 - >负载>查看('registration_step3');
        }
    }
 

(视图部分) registration_step1.php:

 <形式风格=的margin-top:100px的;类=registerbox>
        < H2>用户信息:LT; / H>
        <输入类型=文本名称=用户名占位符=用户名的风格=HEIGHT:35px;宽度:300像素;字体大小:20像素;/> < BR />
        <输入类型=文本名称=电子邮件占位符=电子邮件的风格=HEIGHT:35px;宽度:300像素;字体大小:20像素;/>< BR />
        <输入类型=密码NAME =密码占位符=密码的风格=HEIGHT:35px;宽度:300像素;字体大小:20像素;/>< BR />
        <输入类型=提交级=BTN BTN-成功的风格=HEIGHT:35px;宽度:310px;字体大小:20像素;保证金底:5px的;值=下一步&放大器; RAQUO;/>< BR />
        < /形式GT;
 

(JavaScript)的registration.js:

  $(文件)。就绪(函数(){
        $(registerbox)。递交(函数(){
              变种用户信息= $(registerbox。)序列()。
              返回false;
               $。员额(注册/ step1_process,{
               用户信息:用户信息
              },
            功能(数据){
                警报(数据);
            });
        });
     });
 

解决方案

在你的HTML标记,介于< HEAD> ,你应该把这样的:

 <脚本类型=文/ JavaScript的>
    VAR BASE_URL ='<?PHP的回声BASE_URL(); ?>';
< / SCRIPT>
 

然后在你的 registration.js ,改变你的网址,做返回之前:

  $(文件)。就绪(函数(){
    $(registerbox)。递交(函数(){
        变种用户信息= $(registerbox。)序列()。
        $。员额(BASE_URL +注册/ step1_process,{
            用户信息:用户信息
        },功能(数据){
            的console.log(数据);
        });
        返回false;
    });
});
 

I'm using jQuery and CodeIgniter to do this. Ok so basically I have a register form which asks the user for a username, password, and email. Once the form is submitted, jQuery would serialize the form and give it to a process page, echo the value out in the controller, and then the javascript would alert the post values in the original view. So far it isn't working. Nothing is being alerted. Here's my code:

(The controller) register.php:

    <?php if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    class Register extends CI_Controller
    {
        public function step1()
        {
        $this->load->view('registration_step1');
        }
        public function step1_process(){
        var_dump($_POST);
        }
        public function step2()
        {
        $this->load->view('registration_step2');
        }
        public function step3()
        {
        $this->load->view('registration_step3');
        }
    }

(Portion of the view) registration_step1.php:

        <form style="margin-top: 100px;" class="registerbox">
        <h2>User Info:</h2>
        <input type="text" name="username" placeholder="Username" style="height:35px; width: 300px;font-size:20px;"/> <br />
        <input type="text" name="email" placeholder="Email" style="height:35px; width: 300px;font-size:20px;"/><br />
        <input type="password" name="password" placeholder="Password" style="height:35px; width: 300px;font-size:20px;"/><br />
        <input type="submit" class="btn btn-success" style="height:35px; width: 310px;font-size:20px;margin-bottom:5px;" value="Next &raquo;"/><br />
        </form>

(javascript) registration.js:

     $(document).ready(function() {
        $(".registerbox").submit(function() { 
              var userinfo = $(".registerbox").serialize();
              return false;
               $.post("register/step1_process", {
               userinfo : userinfo
              },
            function(data) {
                alert(data);
            });
        });
     });

解决方案

In your html markup, somewhere in <head>, you should put something like:

<script type="text/javascript">
    var base_url = '<?php echo base_url(); ?>';
</script>

Then in your registration.js, change your url and do post before return:

$(document).ready(function() {
    $(".registerbox").submit(function() { 
        var userinfo = $(".registerbox").serialize();
        $.post(base_url + "register/step1_process", {
            userinfo : userinfo
        }, function(data) {
            console.log(data);
        });
        return false;
    });
});

这篇关于你是怎么做的jQuery与codeIgniter序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆