Reputation: 416
I have Created the MY_Controller
in the core
folder. In which I declared public $footerScript;
. Here is the code of MY_Controller
.
<?php
class MY_Controller extends CI_Controller
{
public $footerScript;
public $data = array();
public function __construct()
{
date_default_timezone_set( 'Asia/Karachi' );
parent::__construct();
$this->load->library(array('ion_auth','form_validation'));
$this->data['C_FullName'] = 'CodeigNiter Shop';
$this->data['C_ShortName'] = 'CI Shop';
}
}
?>
This Home Controller
extends the MY_Controller
. This Home Controller
shows the add_items
file in the views
folder.
<?php
class Home extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
public function items()
{
$this->show("admin/add_items");
}
}
?>
This is the form
in the add_items
in which I give id
to the submit
button. When I click this button a jquery
event
will be called :
<form id="form1" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="Header Text" class="control-label" > Title </i></label>
<input type="text" placeholder="Title" id="title" name="title" class="form-control" tabindex="1">
</div>
<!-- /.form-group -->
</div>
<div class="row">
<div class="col-md-2">
<input type="submit" name="submit" id="Add" value="Add Items" class="btn btn-success">
</div>
<!-- iCheck -->
</div>
<!-- /.col (right) -->
</form>
and this code is written at the end of the add_items
file. The links in the script
tag are working fine but when I click the button to show alert
it does not work.
<?php
//This Section footerScripts Should Execute In Footer/End of the Page.
$this->footerScript = sprintf('
<script src="'.base_url().'assets/bower_components/jquery/dist/jquery.min.js"></script>
<script src="'.base_url().'assets/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$("#Add").on("click", function (e) {
e.preventDefault();
var title= $("#title").val();
alert(title); return false;
}
</script>
');
?>
Upvotes: 2
Views: 216
Reputation: 7997
Since you are using jquery, don't forget to add a document ready like:
$(function() {
$("#Add").on("click", function (e) {
e.preventDefault();
var title= $("#title").val();
alert(title); return false;
}
});
now it waits to execute your function until the DOM is loaded completely. see docs
Upvotes: 3