【问题标题】:Inserting data in to database using jquery/ajax,i am not getting ant error but values not inserting in to the database使用 jquery/ajax 将数据插入数据库,我没有收到 ant 错误,但值没有插入数据库
【发布时间】:2026-01-20 16:05:01
【问题描述】:

使用 jquery/ajax 将数据插入数据库,我没有收到 ant 错误,但没有使用 codeigniter 将值插入数据库,我犯的任何错误意味着请建议我

<script type="text/javascript">
        $(document).ready(function () {
            $("#personal-info").click(function () {
                e.preventDefault();
                var message = $("#message").val();
                //  var password= $("#password").val();


                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>index.php/Profile_cntrl/buyer_communication",
                    data: {message: message},
                    success: function (data)
                    {
                        alert('Successfully inserted');
                    },
                    error: function ()
                    {
                        alert('fail');
                    }
                });
            });
        });
    </script>

表格

<form class="form-horizontal" method="POST" id="personal-info"  role="form" action="#"> 
                            <div class="panel-footer">
                                <div class="input-group">

                                    <input type ="hidden" name="suppid" id="suppid" value="<?php echo $row->supplier_id; ?>" class="form-control" />
                                    <input type ="hidden" name="proid" id="proid" value="<?php echo $row->product_id; ?>" class="form-control" />
                                    <input type ="hidden" name="custid" id="custid" value="<?php echo $row->Customer_id; ?>" class="form-control" />



                                    <input id="messagee" name="message" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." />
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary btn-sm" id="submit-p" name="submit-p">Send</button>
                                        <!--<input type="submit" name="submit-p" id="submit-p" value="send" class="btn btn-primary btn-sm" >-->
                                    </span>
                                </div>
                            </div>
                        </form>

控制器

public function buyer_communication() {

    $supp_id = $this->input->post('suppid');
    $product_id = $this->input->post('proid');
    $cust_id = $this->input->post('custid');



    $result1 = $this->Profile_model->fetch_Data($product_id);


    $Userid = $this->session->userdata('id');
    $result3 = $this->session->userdata('tt');
    $data4 = array(
        'message' => $this->input->post('message'),
        'supplier_id' => $supp_id,
        'product_id' => $product_id,
        'Customer_id' => $cust_id,
        'From' => $result3,
    );

    $this->Profile_model->buyer_insert($data4);

    redirect('welcome/buyermessageview?id=' . $product_id);
}

型号

function buyer_insert($data4) {
        $this->db->insert('communication', $data4);
        return ($this->db->affected_rows() != 1) ? false : true;
    }

【问题讨论】:

  • index.php/Profile_cntrl/buyer_communication 把它放在你的行动中,看看数据是否进入数据库......你至少会知道你的错误并可以纠正它们
  • 在旁注中注释掉 ajax 部分,以便它执行常规发布

标签: jquery html ajax codeigniter


【解决方案1】:
$("#personal-info").click(function () {
    e.preventDefault();

不会阻止默认回发,因为您没有定义 e。几乎可以肯定您的页面是回发而不是使用 ajax,因为脚本永远没有机会运行。试试:

$("#personal-info").click(function (e) {
    e.preventDefault();

其次,您不会将所有数据从表单传递到服务器。您只发送“消息”,但很明显服务器需要其他字段。您可以通过将其作为“数据”字段在您的 ajax 调用中自动发送所有表单字段:

data: $(this).serialize()

您还说“我没有收到任何错误”,但这不太可能。如果您检查浏览器控制台(在开发人员工具中,在大多数浏览器中按 F12),您可能会看到“e 未定义”或类似错误,告诉您忘记定义变量。

【讨论】:

  • 抱歉您的代码不工作..错误消息显示为失败
  • 这意味着它 is 现在按照上面的解决方案工作 - 现在 ajax 调用正在运行,但由于某种原因它失败了。您需要输出完整的错误消息。再次检查您的控制台/网络工具。您还可以根据api.jquery.com/jquery.ajax 的文档,通过使用适当的参数定义“错误”回调来让 jQuery 给出错误。我们需要知道正在生成的状态代码和错误消息。
  • P.S.您将此数据data: {message: message} 发送到服务器,但看起来该方法需要表单中的一些其他字段,例如suppid, proid, custid 你没有发送。同样在 PHP 结束时,您执行重定向,但 ajax 无法处理重定向,因为它位于一个页面的上下文中,而不是回发。您需要直接返回一条消息或一些数据。
  • 举一个我看不懂的简单例子
  • 我做了两个cmets。哪一点你不明白?如何发现错误?或者如何发送ajax请求中的其他字段?
【解决方案2】:

修改后的脚本

<script>
        $(document).ready(function(){
            $("#personal-info").submit(function(e){
               e.preventDefault();


               var suppid = $("#suppid").val();
               var proid = $("#proid").val();
               var custid = $("#custid").val();
                var message = $("#message").val();

                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>index.php/Profile_cntrl/buyer_communication",
                    data: {suppid:suppid,proid:proid,custid:custid,message:message},
                    success:function(data)
                    {
                        alert('SUCCESS!!');
                    },
                    error:function()
                    {
                        alert('fail');
                    }
                });
            });
        });
    </script>

控制器 公共功能买家通信(){

    $result1 = $this->Profile_model->fetch_Data($product_id);


    $Userid = $this->session->userdata('id');
    $result3 = $this->session->userdata('tt');
    $data4 = array(
        'message' => $this->input->post('message'),
        'supplier_id' => $this->input->post('suppid'),
        'product_id' => $this->input->post('proid'),
        'Customer_id' => $this->input->post('custid'),
        'From' => $result3,
    );

    $this->Profile_model->buyer_insert($data4);

    redirect('welcome/buyermessageview?id=' . $product_id);
}

【讨论】: