简体   繁体   English

jQuery Ajax表单未将数据发布到控制器(Codeigniter)

[英]jQuery Ajax form not posting data to controller (Codeigniter)

I have a page where users can write reviews about certain websites. 我有一个页面,用户可以在其中写有关某些网站的评论。 I need to post this data to my Codeigniter controller, which uses a model function to insert the data to a database. 我需要将此数据发布到我的Codeigniter控制器,该控制器使用模型函数将数据插入数据库。 Everything worked fine until I added the jquery and now it doesn't post, and doesn't seem to go to the controller at all. 一切正常,直到我添加了jquery,现在它不再发布了,而且似乎根本没有去过控制器。 Any help fixing this will be appreciated. 解决此问题的任何帮助将不胜感激。 Thanks. 谢谢。

html html

<table id="revForm">
    <tr>
        <td>Site reviewed:</td>
        <td><input readonly type="text" id="site" name="profile" value="<?php echo $sitename; ?>"></td>
    </tr>
    <tr>
        <td>Name:</td>
        <td> <input type="text" id="name" name="name" value="Enter name"> </td>            
    </tr>
    <tr>
        <td>Message:</td>
        <td><textarea row="6" column="40" id="message" name="message" value="message"></textarea></td>
    </tr>
    <tr>
        <td>Rate this site</td> 
        <td>
            <div>                        
                <input name="rating" type="radio" value="1" class="star"/>
                <input name="rating" type="radio" value="2" class="star"/>
                <input name="rating" type="radio" value="3" class="star"/>
                <input name="rating" type="radio" value="4" class="star"/>
                <input name="rating" type="radio" value="5" class="star"/>                    
            </div>

        </td>
    </tr>
</table><br>
<button id="submitReview2">Submit</button><br/>

Controller 'reviews' 管制员“审查”

public function addReview() { 

    $this->load->helper('form');         
    if ($this->input->post("message")!="")  {
            $this->reviewsMod->addReview($this->input->post('profile'),
                                    $this->input->post('name'),
                                    $this->input->post('message'),
                                    date('d/m/Y'),
                                    $this->input->post('rating'));
            $data['sitename'] = $this->input->post('profile');
            $this->load->view('shared/header');
            $this->load->view('profile', $data );
            $this->load->view('shared/footer');                            
        }

        else {
            $this->load->view('shared/header', $session);
            $this->load->view('profile', $data);
            $this->load->view('shared/footer'); 
            }      
    }

jQuery jQuery的

$('#submitReview2').click(function() {
    var profile = $('#site').val();
    var name = $('#name').val();
    var message = $('#msg').val();
    var rating = $('#rating').val();

    var msgData = {
       'profile':profile,
       'name':name,
       'message':message,
       'rating':rating
    }
   var base_url = '<?php echo base_url(); ?>';
   $.ajax ({
       type: 'POST',
       url: base_url+'reviews/addReview',
       data:  msgData,
       success:
           function(){                   
                $('#revForm').html("<div id='success'></div>")
                $('#success').html("Your review has been submitted")
                .fadeIn(1500);
           }

  });
  return false;

Model function 模型功能

public function addReview($profile, $name, $message, $stamp, $rating) {
   $this->setValues($profile, $name, $message, $stamp, $rating);
   $this->db->insert('messages', $this);

} }

Did you load the url helper for the base_url() function? 您是否为base_url()函数加载了url帮助器?

$this->load->helper('url');

Also use chrome with and use the developertools to check "console" and "network". 还将chrome与配合使用,并使用developertools检查“控制台”和“网络”。

I think thw JQuery post is does not work like this. 我认为JQuery帖子不像这样工作。 you need to use the Jquery .submit() or .post() methods. 您需要使用Jquery .submit().post()方法。

please find the example and explanation of the Jquery post() and submit() 请找的例子,解释Jquery post()submit()

1. Jquery submit 2. Jquery post 1. jQuery提交 2. jQuery发布

First of all specify the error handle for your ajax request so you can see what's happening after error occures: 首先,为您的ajax请求指定错误句柄,以便您可以查看发生错误后发生的情况:

$.ajax({
        //
        // Other parameters
        //
        success: function (result, textStatus, jqXHR) {
            // Some code
        },
        error: function (jqXHR, textStatus, errorThrown) {
           // Some code to debbug e.g.:               
           console.log(jqXHR);
           console.log(textStatus);
           console.log(errorThrown);
        }
    });

Also start using some kind of javascript debbuging tool (depends on the browser you're using) like someone said chrome developer tools (press f12 when being on website and go to network tab) in Chrome or Firebug in Firefox or even a more sophisticated desktop application like Fiddler 还要开始使用某种javascript调试工具(取决于您使用的浏览器),例如有人说Chrome开发工具(在网站上按f12进入网络标签,然后转到“网络”标签)在Firefox或Firebug甚至是更复杂的台式机中Fiddler之类的应用程序

If you'll still have problem with posting data please give us some more information about error you've encountered 如果您仍然无法发布数据,请向我们提供有关您遇到的错误的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM