codeigniter和阿贾克斯联系表 [英] codeigniter and ajax contact form

查看:179
本文介绍了codeigniter和阿贾克斯联系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在codeigniter应用程序使用在我接触的形式阿贾克斯。我有它到了AJAX调用时,但无后的数据被发送到服务器。我想不通为什么。请大家帮帮忙。

I'm trying to use ajax within my contact form in a codeigniter app. I have it to where the ajax call is made, but no post data is being sent to the server. I can't figure out why. Please help.

我在那里有一些收益,但是他们什么都不做。此外,$这个 - >输入 - >后(名称)为空。

I do have some returns in there, but they do nothing. Also, $this->input->post('name') is null.

表单视图

<?php
                    echo form_open('welcome/submit_contact');

                    echo form_error('name');
                    echo form_label('Name', 'name'"');
                    echo form_input('name', set_value('name'), 'id="name);

                    echo form_error('email');
                    echo form_label('Email', 'email');
                    echo form_input('email', set_value('email'), 'id="email"');

                    echo form_error('phone');
                    echo form_label('Phone', 'phone');
                    echo form_input('phone', set_value('phone'), 'id="phone"');
                    #echo '<h5>Do not start with "1" and no dashes.</h5>';

                    echo form_error('message');
                    echo form_label('Message', 'message');
                    echo form_textarea('message', set_value('message'), 'id="message"');

                    $submitData = array(
                        'name'  => 'submit',
                        'value' => 'Submit',
                        'id'    => 'button'

                    );
                    echo form_submit($submitData);

                    echo form_close();
                ?>
                <script type="text/javascript">

                $(function() {

                    $('form').click(function() {

                        // get the form values
                        var form_data = {
                            name: $('#name').val(),
                            email: $('#email').val(),
                            message: $('#message').val()
                        };

                        // send the form data to the controller
                        $.ajax({
                            url: "<?php echo site_url('welcome/submit_contact'); ?>",
                            type: "post",
                            data: form_data,
                            success: function(msg) {
                                $('form').prepend('<h5 class="good">Message sent!</h5>');
                                $('h5.good').delay(3000).fadeOut(500);
                                alert(msg);
                            }
                        });

                        // prevents from refreshing the page
                        return false;   
                    });
                });
                </script>

控制器功能

function submit_contact()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<h4 class="bad">', '</h4>');

        $name = $this->input->post('name');
        echo "name = ".$name;

        $this->form_validation->set_rules('name', 'Name', 'trim|required|alpha_dash|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('phone', 'Phone' , 'trim|integer|exact_length[10]|xss_clean');
        $this->form_validation->set_rules('message', 'Message', 'trim|required|max_length[1000]|xss_clean');

        // there are validation errors
        if($this->form_validation->run() == FALSE)
        {
            return "error";
        }
        else // there are no validation errors
        {
            /*************************
            need to actually send the email 
            *************************/
            return null;
        }

    }

编辑:我已经更新了code在我的问题。基本上,现在如果有验证错误,我将如何让他们在窗体上显示?我假设我会从我的控制器返回一个值,然后有一个声明,我的阿贾克斯成功,如果MSG ==错误显示ELSEIF味精== NULL,显示成功消息的错误。但如何将我告诉我的观点基于一个ajax成功变量显示这些错误?

I've updated the code in my question. Basically now if there are validation errors, how would I get them to display on the form? I'm assuming I would return a value from my controller and then have a statement in my ajax success that if msg == "error" display the errors elseif msg == null, display success message. But how would i tell my view to display those errors based on an ajax success variable?

推荐答案

我想你应该把编号输入和textarea的不是标签,即

i think you should put id on input and textarea not on label i.e.

$data = array
(
   "name"=>'message',
   "value"=>'message',
   "id"=>'message'
)


form_textarea($data);

如果设置标签上的ID,然后jQuery将拿起任何从用户插入的同时也codeigniter验证将无法正常工作

。这就是为什么你的帖子结果为NULL

if you set the id on the label then jquery will pick up nothing from what the user inserted and also codeigniter validation won't work correctly. This is why your post result to be NULL

相同的其他输入字段

修改

您通过AJAX请求数据,以便返回一个不错的JSON对象(先删除所有的调试输出):

you are asking data via ajax so return a nice json object (remove all your debug prints first):

// there are validation errors
if($this->form_validation->run() == FALSE)
{
    echo(json_encode("validate"=>FALSE));
}
else // there are no validation errors
{
    /*************************
    need to actually send the email, then send you mail 
    *************************/
    echo(json_encode("validate"=>TRUE));
}

然后在你的ajax成功函数测试显示正面或负面的消息

then test it on your ajax success function to display positive or negative message

 <script type="text/javascript">

        $(function() {

            $('form').click(function() {

                // get the form values
                var form_data = {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    message: $('#message').val()
                };

                // send the form data to the controller
                $.ajax({
                    url: "<?php echo site_url('welcome/submit_contact'); ?>",
                    type: "post",
                    data: form_data,
                    dataType: "json"
                    success: function(msg)
                    {
                        if(msg.validate)
                        {
                           $('form').prepend('<h5 class="good">Message sent!</h5>');
                           $('h5.good').delay(3000).fadeOut(500);
                        }
                        else
                           //display error
                    }
                });

                // prevents from refreshing the page
                return false;   
            });
        });
        </script>

这篇关于codeigniter和阿贾克斯联系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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