【问题标题】:codeigniter and ajax contact formcodeigniter 和 ajax 联系表
【发布时间】:2011-06-17 13:12:04
【问题描述】:

我正在尝试在 codeigniter 应用程序的联系表单中使用 ajax。我有它到进行ajax调用的地方,但没有发送数据到服务器。我不知道为什么。请帮忙。

我确实有一些回报,但他们什么也没做。另外,$this->input->post('name') 为空。

表单视图

<?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;
        }

    }

编辑:我已经更新了我的问题中的代码。基本上现在如果存在验证错误,我将如何让它们显示在表单上?我假设我会从我的控制器返回一个值,然后在我的 ajax 成功中有一个语句,如果 msg == "error" 显示错误 elseif msg == null,则显示成功消息。但是我如何告诉我的视图根据 ajax 成功变量显示这些错误?

【问题讨论】:

  • 这一行中多余的 " 做什么? echo form_label('Name', 'name'"');
  • 你的名字是 null 因为这个 echo form_input('name', set_value('name'), 'id="name); 你的 id="name 没有用 "

标签: php jquery ajax codeigniter


【解决方案1】:

我认为您应该将 id 放在 input 上,而 textarea 不要放在标签上,即

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


form_textarea($data);

如果您在标签上设置了 id,那么 jquery 将不会从用户插入的内容中获取任何内容,并且 codeigniter 验证也将无法正常工作。这就是为什么您的帖子结果为 NULL

其他输入框也一样

编辑

您通过 ajax 询问数据,因此返回一个不错的 json 对象(首先删除所有调试打印):

// 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 成功函数上对其进行测试以显示正面或负面消息

 <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>

【讨论】:

  • 似乎成功了。我必须在你的回答中修正一些错误,但如果使用 ajax 是我收集的,基本上我不能使用 codeigniter form_error()。我想我明白了。
  • 是的,如果您使用 ajax 显示错误,那么 codeigniter 报告将会丢失,但您仍然有不错的验证 :)
猜你喜欢
  • 2011-06-03
  • 2018-12-19
  • 1970-01-01
  • 2017-11-23
  • 2016-05-28
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多