Today we learn about how to submit form using jquery and ajax.Its very simple to do this task in codeigniter framework.Its very powerfull php framework and its opensource.
first step (create controller) :
class Test extends Controller {
function Test()
{
parent::Controller();
$this->load->helper('url');
$this->load->view('test');
}
function create(){
if($_POST):
echo "done this task";
print_r($_POST);
return true;
endif;
}
}
Second step : (create views(test.php))
<html>
<head>
<script language="javascript" src="<?php echo base_url().'js/Jquery.js';?>"></script>
<script>
$(function(){
$("#t").click(function(){
var state=document.getElementById('state').value;
dataString = $("#comment").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url();?>index.php/test/create",
data: dataString,
success: function(data){
alert('Successful!');
document.getElementById('dd').innerHTML=data;
}
});
return false; //stop the actual form post !important!
});
});
</script>
</head>
<body>
<h3>New Comment</h3>
<form id="comment" method="post">
State : <select name="state" id="state">
<option value="0">State</option>
<option value="guj">Gujarat</option>
</select><br/>
<input type='button' name='submit' id='t' />
</form>
<div id="dd">hello</div>
</body>
</html>
 
After Run this code just click on the button it can post form data and display this data into div.
Free tutorial for learning codeigniter framework : Tutorials
Download Codeigniter : Download
first step (create controller) :
class Test extends Controller {
function Test()
{
parent::Controller();
$this->load->helper('url');
$this->load->view('test');
}
function create(){
if($_POST):
echo "done this task";
print_r($_POST);
return true;
endif;
}
}
Second step : (create views(test.php))
<html>
<head>
<script language="javascript" src="<?php echo base_url().'js/Jquery.js';?>"></script>
<script>
$(function(){
$("#t").click(function(){
var state=document.getElementById('state').value;
dataString = $("#comment").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url();?>index.php/test/create",
data: dataString,
success: function(data){
alert('Successful!');
document.getElementById('dd').innerHTML=data;
}
});
return false; //stop the actual form post !important!
});
});
</script>
</head>
<body>
<h3>New Comment</h3>
<form id="comment" method="post">
State : <select name="state" id="state">
<option value="0">State</option>
<option value="guj">Gujarat</option>
</select><br/>
<input type='button' name='submit' id='t' />
</form>
<div id="dd">hello</div>
</body>
</html>
After Run this code just click on the button it can post form data and display this data into div.
Free tutorial for learning codeigniter framework : Tutorials
Download Codeigniter : Download

This is nice post...!
ReplyDeleteweb designing bangalore