CodeIgniter 中的 AJAX 联系表单

发布于 2024-10-10 04:00:56 字数 1669 浏览 0 评论 0原文

有几个问题:

我正在使用 CI 和 JQuery AJAX。

在下面的代码中,我组装了 dataString,默认情况下,它会作为查询字符串附加到 URL。

我已将 AJAX“类型”更改为 POST,所以我的问题是 - 如何在 CI 应用程序中访问 dataString

看来我仍然必须使用

$name=$this->input->post('name')

对我来说,这使得设置 dataString 变得多余?

——

我尝试过搜索,但找不到任何具体的东西。

是否仍然可以使用 CI 验证库和 AJAX?

if($this->form_validation->run() == FALSE)
{
    // what can i return so that my CI app shows errors?
}

通常您会重新加载联系表单或重定向用户。在理想的情况下,我希望向用户显示错误消息。

Jquery:

    $(document).ready(function($){
        $("#submit_btn").click(function(){
            var name = $("input#name").val();
            var company = $("input#company").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var dataString = 'name=' +  name + '&message=' + message + '&return_email=' + email + '&return_phone=' +
            phone + '&company=' + company;
            var response = $.ajax({
                type: "POST",
                url: "newsite/contact_ajax/",
                data: dataString
            }).responseText;

            //$('#contact').hide();
            //$('#contact').html('<h5>Form submitted!  Thank you!</h5><h4>We will be in touch with you soon.</h4>');
            //$('#contact').fadeIn('slow');
            return false;
        });  
    });

希望我已经说得足够清楚了 - 如果有人有 CI 联系表单的不错示例,那就太好了。互联网上的东西五花八门,但没有什么是符合所有条件的。

谢谢

Few questions:

I'm using CI and JQuery AJAX.

In my code below, I assemble dataString, which by default, is appended to the URL as a query string.

I've changed the AJAX "type" to POST, so my question is - how do I access dataString in my CI app?

It would seem I still have to use

$name=$this->input->post('name')

Which to me, makes setting dataString redundant?

--

I've tried searching but can't really find anything concrete.

Would it be possible to still make use of CIs validation library and AJAX?

if($this->form_validation->run() == FALSE)
{
    // what can i return so that my CI app shows errors?
}

Normally you would reload the contact form or redirect the user. In an ideal world I would like the error messages to be shown to the user.

Jquery:

    $(document).ready(function($){
        $("#submit_btn").click(function(){
            var name = $("input#name").val();
            var company = $("input#company").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var dataString = 'name=' +  name + '&message=' + message + '&return_email=' + email + '&return_phone=' +
            phone + '&company=' + company;
            var response = $.ajax({
                type: "POST",
                url: "newsite/contact_ajax/",
                data: dataString
            }).responseText;

            //$('#contact').hide();
            //$('#contact').html('<h5>Form submitted!  Thank you!</h5><h4>We will be in touch with you soon.</h4>');
            //$('#contact').fadeIn('slow');
            return false;
        });  
    });

hope i've been clear enough - if anyone has a decent example of a CI contact form that would be great. there's mixed stuff on the internet but nothing that hits all the boxes.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-10-17 04:00:56

正如我在评论中所写,您不需要设置 dataString 因为 jQuery 可以为您做到这一点:

$(document).ready(function($){
    $("#submit_btn").click(function(){
        var response = $.ajax({
            type: "POST",
            url: "newsite/contact_ajax/",
            data: $(your_form_id).serialize()
        }).responseText;

        //$('#contact').hide();
        //$('#contact').html('<h5>Form submitted!  Thank you!</h5><h4>We will be in touch with you soon.</h4>');
        //$('#contact').fadeIn('slow');
        return false;
    });  
});

As I wrote in the comments, you don't need to set a dataString since jQuery can do it for you:

$(document).ready(function($){
    $("#submit_btn").click(function(){
        var response = $.ajax({
            type: "POST",
            url: "newsite/contact_ajax/",
            data: $(your_form_id).serialize()
        }).responseText;

        //$('#contact').hide();
        //$('#contact').html('<h5>Form submitted!  Thank you!</h5><h4>We will be in touch with you soon.</h4>');
        //$('#contact').fadeIn('slow');
        return false;
    });  
});
谜泪 2024-10-17 04:00:56

如果您使用 jQuery $.post() 函数,您可以显式命名 post 变量,然后按照您在问题中建议的方式在控制器中访问它们。

 $.post("<?php echo site_url("newsite/contact_ajax/";?>",
        {my_name:name,my_company:company,my_email:email,my_phone:phone,my_message:message},
        function(){
              //callback function
         },    
         html
      );

在你的控制器中:

$this->input->post('my_name');
$this->input->post('my_company');
//etc

If you use the jQuery $.post() function, you can explicitly name the post variables, then access them in your controller the way you suggested in your question.

 $.post("<?php echo site_url("newsite/contact_ajax/";?>",
        {my_name:name,my_company:company,my_email:email,my_phone:phone,my_message:message},
        function(){
              //callback function
         },    
         html
      );

In Your controller:

$this->input->post('my_name');
$this->input->post('my_company');
//etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文