0

Using codeIgniter, I successfully implemented user registeration with enctype: multipart/form-data but after that I started trying user registration using AJAX I got stuck. I can see ajax responses in console but there is one response which always has a null value (i.e password). And also its not registering user anymore. No data is inserting after submitting ajax request. Here's what I am doing. This is my registration view in which I wrote this script

<script type="text/javascript">
    function AJAXcreatemember() {

            //getting form fields values
            var fname = $('#firstname').val();
            var lname = $('#lastname').val();
            var email = $('#email').val();
            var pwd = $('#pwd').val();
            var cpwd = $('#cpwd').val();
            var gender = $('#gender').val();
            var country = $('#country').val();
            var city = $('#city').val();
            var occupation = $('#occupation_area').val();
            var about = $('#about').val();
            var interest = $('#interest').val();
            var contact_no = $('#contact_no').val();
            var website_url = $('#website_url').val();

            //setting ajax information to be sent to $.ajax()
            var path =  "<?php echo base_url()?>user/register/";
            var data = 'first_name='+fname+'&last_name='+lname+'&email='+email+'&password='+pwd+'&gender='+gender+'&country='+country+'&city='+city+'&occupation_area='+occupation+'&about='+about+'&interest='+interest+'&contact_no='+contact_no+'&website_url'+website_url;

            alert('URL: ' + path);

            $.ajax({

                url: path,
                data: data,
                type: 'GET',
                success: function(data) {
                    alert(data);
                }   
            });

            return false;

</script>

User Controller:

public function register() {

    $time = date("Y-m-d H:i:s");

    $this->form_validation->set_rules('firstname', 'First Name', 'required');
    $this->form_validation->set_rules('lastname', 'Last Name', 'required');
    $this->form_validation->set_rules('gender', 'Gender', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('pwd', 'Password', 'matches[cpwd]');
    $this->form_validation->set_rules('city', 'City', 'required');
    $this->form_validation->set_rules('country', 'Country', 'required');
    $this->form_validation->set_rules('occupation_area', 'Functional Area', 'required');

    if ($this->form_validation->run() == FALSE) {

        $this->index();
    }
    else {

        $user_email = $this->user_model->get_user_by_email($this->input->post('email'));

        if($user_email->num_rows() == 0) {


            $avatar = $this->upload('profile_image'); 
            $this->user_model->create_user($time , $avatar);

            /*if($this->upload->data()) {

                $data['success'] = 'yes';
                $this->load->view('index', $data);
            }*/ 
        }
        /*else {

            $data['page_title'] = 'CM Rizwan - Khabrain News Group Columnist';
            $data['email_exist_error'] = 'Email Already exist.';
            $this->load->view('index', $data);
        }*/

    }
}

public function upload($field) {

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '200';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['encrypt_name']  = TRUE;

    $this->load->library('upload', $config);

    if(!$this->upload->do_upload($field)) { 

        $upload_error = array('upload_error' => $this->upload->display_errors());
        $this->load->view('index', $upload_error);
    }
    /*else {

        $upload_success = array('upload_data' => $this->upload->data());
        $this->load->view('index', $upload_success);
    }*/

    //$this->upload->data() returns all the required information for an upload file.
    $avatar = $this->upload->data();
    return $avatar['file_name'];
}

NOTE EDITED: Registration view is a part of index.php file which I have sliced, registeration view is a popup that shows up when clicking onto Registration link.

zemar
  • 49
  • 7
  • seriously, this is like the 4th time today I've posted this link: http://codebyjeff.com/blog/2013/04/how-do-i-use-ajax-with-framework-x Debugging ajax submissions is not that difficult - no one wants to read through all your code and fix it for you. At least START debugging and tell us what you've found out so far – jmadsen Nov 09 '13 at 08:34
  • use $this->input->get() because your method is GET – rajesh kakawat Nov 09 '13 at 08:49
  • @Rajesh, Yar mene get use kia hay response bhi ja raha hay lekin values database me insert ni horahi :( – zemar Nov 09 '13 at 09:03
  • I solved the problem myself: When you encounter a Jquery POST internal server error 500 in codeigniter, you should consult this link, see the answer by v1r00z: http://stackoverflow.com/questions/1349118/jquery-ajax-post-results-in-500-internal-server-error – zemar Nov 09 '13 at 10:05

0 Answers0