0

Hey I'm new to CI and have scoured the internet for a tutorial that will work but for some reason it won't work. Can someone help me with the code please:

What's the right edit to the code to submit an entry to the database via ajax without reloading the page?

Controller:

public function index(){
$this->load->helper('url');
$this->load->view('template');

}

function create()
{
    $this->load->library('form_validation');
    $this->load->helper('form');
    $this->load->model('dbmodel');

    $response = array();
    $this->load->model('dbmodel');
    $result = $this->dbmodel->addnew_row();     
}

Model:

public function addnew_row() {

    $data = array(
        'title' => $this->input->post('title'),
        'description' => $this->input->post('description'),
    );
    $result = $this->db->insert('books', $data);
    return $result;
}

View Form:

<h2>Create</h2> 
<?php echo form_open('welcome/create', array('id'=>'form'));?>


<p>Title: <?php echo form_input('title') ?></p>
<p>Description: <?php echo form_input('description') ?></p> 

<?php echo form_submit('btn_submit','Save'); ?>


<?php echo form_close();?>

View AJAX:

 // $.POST Example Request
$('#form').submit(function(eve){
    eve.preventDefault();

    var title = $('#title').val();
    var description = $('#description').val();

    url = '<?php echo site_url('welcome/create');?>'
    $.ajax({
        url: url,
        type: 'POST',
        data:{title:title, description:description
        }

        $(#dynamic_container).html(response.output);
    });
});
2
  • 1
    Well, at the very least your url variable isn't defined properly nor is it terminated. var url = '<?php echo site_url('welcome/create');?>'; Commented May 25, 2014 at 17:15
  • 1
    could you post the error message please? You can find it in the developer console (F12->Network->Trigger ajax request, and then select the ajax request in the list) Commented May 25, 2014 at 17:16

1 Answer 1

1

Ok,At first you need to briefly go through the syntax of jQuery.ajax() before using it.

Now going though the AJAX code you mentioned in the question , this block of code is not suppose to be there

$(#dynamic_container).html(response.output);

AJAX provides Callback Function Queues to manipulate response before/after an AJAX call has been successfully completed , and in your case using success will resolve the issue :

  $.ajax({
        url: url,
        type: 'POST',
        data:{title:title, description:description
        },
        success : function(response){
        $(#dynamic_container).html(response.output);
        }
    });

And you might be interested in using jQuery.post().

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

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.