This method will easy to understand ajax data pass
View/home
<html>
<head>
<title>Submit form</title>
<script src="http://localhost/js/jquery.min.js" type="text/javascript" ></script>
<script type="text/javascript">
$(document).ready(function() {
$('.jsForm').on('submit', function(form) {
form.preventDefault();
$.ajax({
url: 'index.php/home/insert',
type: 'POST',
data:
{
username: $('#username').val(),
password: $('#password').val()
},
success: function(data) {
$(".jsError").html(data).hide("slow");
console.log(data);
}
});
});
});
</script>
</head>
<body>
<div class="jsError"></div>
<?php echo form_open('home/insert', array("class" => 'jsForm')); ?>
<input type="text" name="username" id="username"/>
<input type="text" name="password" id="password"/>
<input type="submit" value="save" />
<?php echo form_close(); ?>
</body>
</html>
Controller/Home
<?php
class Home extends CI_Controller {
public function view($page = 'home') {
$this->load->library('form_validation');
$this->load->view('home/' . $page);
}
public function insert(){
$user=$this->input->post('username');
$pass=$this->input->post('password');
echo $user." ".$pass;
}
}
Comments
Post a Comment