1

I'm new in codeigniter I try to add data in table users using ajax when I send data I have now problem and all data send good but no data stored in data base and this is my code controller

public function addusersajax()
{
    if($this->input->post("action") =="addusersintable")
    {
        $this->load->helper('date');
        $data=array(
            "fullname"=> $this->input->post("fullname"),
            "username"=> $this->input->post("username"),
            "password" => md5($this->input->post("password")),
            "email"=>$this->input->post("email"),
            "groubid"=>$this->input->post("groubid"),
            "date"=>mdate('%Y-%m-%d ', now()),
            "time"=>date(" H:i:s")
        );
        $this->load->model("usersmodel");
        if($this->usersmodel->adduserbyajax($data)){
            echo "done";
        }else{
            echo 'failed';
        }
    }
}

this is function for view form

public function view()
{
    $data['pagetitle']="xx";
    $this->load->view("template/admin/header",$data);
    $this->load->view("users/ss",$data);

    $this->load->view("template/admin/footer");
}

this is my view

<div class="container">
    <div id="container">

        <div class="col-lg-8">
            <form id="insercodegn"  method="post"  action="" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="recipient-name" class="control-label">Full Name :</label>
                    <input type="text" class="form-control"  name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">

                </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label">UserName :</label>
                    <input type="text" class="form-control"  name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
                </div>
                <div class="form-group">
                    <label for="message-text" class="control-label">Password:</label>
                    <input type="password" class="form-control"  name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
                    <i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
                </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label">Email :</label>
                    <input type="text" class="form-control"  name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
                </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label">User Type :</label>
                    <select class="form-control" name="groubid"  id="groubid">
                        <option value="1">Administartor</option>
                        <option value="0">User</option>
                        <option value="2">Maker</option>
                        <option value="3">cheker</option>

                    </select>
                </div>
                <div class="form-group">
                    <label for="exampleInputFile">Image :</label>

                </div>
                <input type="submit" name="action" value="adduserssss">
                <button type="submit"   name="action" value="addusersintable" class="btn btn-primary">addcc</button>
            </form>
        </div>
    </div>
</div>

****this is my script inside ss viewpage****

 <script>
        $(document).ready(function () {
            $(function () {
                $("#insercodegn").on('submit', function (e) {

                    e.preventDefault();

                    $.ajax({

                        url:'<?php echo base_url()?>Users/addusersajax',
                          //url:"<?php echo base_url() .'Users/addusersajax'?>",

                        method:'post',
                        data: new FormData(this),
                        contentType: false,
                        cache: false,
                        processData: false,
                        success:function(data)  {
                            alert(data);
                        }
                    })
                })
            })
        })

    </script>

my form is

<div class="container">
    <div id="container">

        <div class="col-lg-8">
            <form id="insercodegn"  method="post"  action="" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="recipient-name" class="control-label">Full Name :</label>
                    <input type="text" class="form-control"  name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">

                </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label">UserName :</label>
                    <input type="text" class="form-control"  name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
                </div>
                <div class="form-group">
                    <label for="message-text" class="control-label">Password:</label>
                    <input type="password" class="form-control"  name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
                    <i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
                </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label">Email :</label>
                    <input type="text" class="form-control"  name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
                </div>
                <div class="form-group">
                    <label for="recipient-name" class="control-label">User Type :</label>
                    <select class="form-control" name="groubid"  id="groubid">
                        <option value="1">Administartor</option>
                        <option value="0">User</option>
                        <option value="2">Maker</option>
                        <option value="3">cheker</option>

                    </select>
                </div>
                <div class="form-group">
                    <label for="exampleInputFile">Image :</label>

                </div>
                <button type="submit"   name="action" value="adduserssss" class="btn btn-primary">add</button>
            </form>
        </div>
    </div>
</div>
1
  • You may want to format your question Commented Feb 4, 2018 at 19:43

2 Answers 2

1

Change this:

<input type="submit" name="action" value="adduserssss">
<button type="submit"   name="action" value="addusersintable" class="btn btn-primary">addcc</button>

To this:

<input type="hidden" name="action" value="addusersintable">
<button type="submit" class="btn btn-primary">addcc</button>

Buttons don't have $_POST data thus: if($this->input->post("action") =="addusersintable") is always evaluating to false and never hitting your model. A hidden input is what you are looking for.

Sign up to request clarification or add additional context in comments.

5 Comments

first i want to thank your effort alex but it dosent work
it does for me: imgur.com/a/v1QZx (not sure what you are doing ;p )
how you did this alert message
Using the same exact code as you with the above modification and a print_r after data as shown in the image
I'm sorry bro I don't do that. Last guess, check to make sure your url is correct.
1

1) Use lower case url in your javascript file, try "users/addusersajax" instead of "Users/addusersajax".

2) Use var_dump($_POST) or print_r($_POST) in your controller. in network tab of Chrome or Firefox check to see result of your request. you must see what is posted. if it is all good you must check your model

3) In your model it is best to use transactions. see transactions documentations in Codeigniter user guide. also you can echo $this->db->last_query() to see what query is executed. copy the executed query and run it from phpMyAdmin or any other MySQL client you use and see if you get any error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.