Inserting data in to database using jquery/ajax,i am not getting ant error but values not inserting in to the database

前提是你 提交于 2019-12-24 06:48:51

问题


Inserting data in to database using jquery/ajax,i am not getting ant error but values not inserting in to the database using codeigniter,any mistake i done means please suggest me

<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

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

controller

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

Model

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

回答1:


modified script

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

controller public function buyer_communication() {

    $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);
}



回答2:


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

is not preventing the default postback because you didn't define e. Almost certainly your page is posting back rather than using ajax, because the script never has chance to run. Try:

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

Secondly, you aren't passing all the data to your server from the form. You only send "message", but it's clear the server requires other fields. You can send all your form fields automatically by putting this as the "data" field in your ajax call:

data: $(this).serialize()

You also said "I am not getting any error", but this is unlikely. If you checked your browser console (in the Developer Tools, by pressing F12 in most browsers) you would probably have seen the error "e is not defined" or similar, telling you that you had forgotten to define the variable.



来源:https://stackoverflow.com/questions/44305553/inserting-data-in-to-database-using-jquery-ajax-i-am-not-getting-ant-error-but-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!