footer scripts do not work in codeigniter - php

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>
');
?>

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

Related

autocomplete gives codeigniter3 404 error

I am creating a search Bar using Jquery autocomplete in Codeigniter 3 using and ajax and MySQL
Here is Controller College.php Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class College extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('College_model');
}
public function index() {
$this->load->view('college_view');
}
function get_autocomplete() {
if (isset($_GET['term'])) {
$result = $this->college_model->search_college($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row) {
$arr_result[] = array(
'name' => $college_name,
'description' => $row->college_description,
);
echo json_encode($arr_result);
}
}
}
}
}
?>
This is College_model.php
<?php
class College_model extends CI_Model
{
function get_all_college() {
$result = $this->db->get('college');
return $result;
}
function search_college($name) {
$this->db->like('college_name', $name, 'both');
$this->db->order_by('college_name', 'ASC');
$this->db->limit(10);
return $this->db->get('college')->result();
}
}
?>
This is view page college_view.php
<div class="tab-content py-3 px-3 px-sm-0 m-auto" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<form action="http://vufind.carli.illinois.edu/vf-aru/Search/Home" method="get" role="search" target="vufind" name="searchForm">
<div class="input-group lrcInputs">
<input value="1" name="start_over" type="hidden">
<label></label>
<input class="form-control" id="college" name="college" type="text" placeholder="Search for books, ebooks, & media">
<div class="input-group-btn">
<button class="btn btn-success lrcSearchButton" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#college').autocomplete({
source: "<?php echo site_url('college/get_autocomplete'); ?>",
select: function (event, ui) {
$('[name="college"]').val(ui.item.name);
}
});
});
</script>
How to remove the error of 404 not found. if need more code or file i will help.
I m getting error in chrome console as GET http://localhost/apluscollege/college/get_autocomplete?term=as 404 (not found)
jquery.min.js
what is the isuue in the code ...I have checked from every possible angle of code.
In the controller, please write
$this->load->model('college_model');
instead of
$this->load->model('College_model');
The model name should be the same case when you load and use it

404 not found in codeigniter 3

This is controller College.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class College extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('College_model');
}
public function index() {
$this->load->view('college_view');
}
function get_autocomplete() {
if (isset($_GET['term'])) {
$result = $this->college_model->search_college($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row) {
$arr_result[] = array(
'name' => $college_name,
'description' => $row->college_description,
);
echo json_encode($arr_result);
}
}
}
}
}
?>
This is College_model.php
<?php
class College_model extends CI_Model
{
function get_all_college() {
$result = $this->db->get('college');
return $result;
}
function search_college($name) {
$this->db->like('college_name', $name, 'both');
$this->db->order_by('college_name', 'ASC');
$this->db->limit(10);
return $this->db->get('college')->result();
}
}
?>
This is view page college_view.php
<div class="tab-content py-3 px-3 px-sm-0 m-auto" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<form action="http://vufind.carli.illinois.edu/vf-aru/Search/Home" method="get" role="search" target="vufind" name="searchForm">
<div class="input-group lrcInputs">
<input value="1" name="start_over" type="hidden">
<label></label>
<input class="form-control" id="college" name="college" type="text" placeholder="Search for books, ebooks, & media">
<div class="input-group-btn">
<button class="btn btn-success lrcSearchButton" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#college').autocomplete({
source: "<?php echo site_url('college/get_autocomplete'); ?>",
select: function (event, ui) {
$('[name="college"]').val(ui.item.name);
}
});
});
</script>
How to remove the error of 404 not found. if need more code or file i will help.
i m getting error in chrome console as GET http://localhost/apluscollege/college/get_autocomplete?term=as 404 (not found) jquery.min.js
i m making a search bar using autocomplete using jQuery ana ajax

Upload Image to Database using Ajax and Codeigniter

I have a CRUD with ajax working , but i want to implement a file upload to it.
Everything works fine except the image upload , the image is the only thing that is not saving on database and folder , all the other data are saving.
This is my CRUD Controller(just the add part) where I've implemented the upload code (dados)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class dados extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('dados_model');
$this->load->database();
}
public function index()
{
$data['dados']=$this->dados_model->get_all_dados();
$this->load->view('dados_view',$data);
}
public function dados_add()
{
$config = array(
'upload_path' => "./assets/uploads",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000",
);
$this->load->library('upload',$config);
$this->upload->do_upload('userfile');
$data2=array('upload_data' => $this->upload->data());
$data = array(
'Name' => $this->input->post('Name'),
'City' => $this->input->post('City'),
'address' => $this->input->post('address'),
'lastname' => $this->input->post('lastname'),
'Image' =>$data2['upload_data']['file_name']
);
$this->dados_model->dados_add($data);
echo json_encode(array("status" => TRUE));
}
public function ajax_edit($id)
{
$data = $this->dados_model->get_by_id($id);
echo json_encode($data);
}
And this is my Model, I use it to store the data on the database(dados_model)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class dados_model extends CI_Model
{
var $table = 'dados';
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_all_dados()
{
$this->db->from('dados');
$query=$this->db->get();
return $query->result();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('ID',$id);
$query = $this->db->get();
return $query->row();
}
public function dados_add($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
This is my Ajax code to save
<script type="text/javascript">
$(document).ready( function () {
$('#table_id').DataTable();
} );
var save_method; //for save method string
var table;
function add_person()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('#modal_form').modal('show'); // show bootstrap modal
//$('.modal-title').text('Add Person'); // Set Title to Bootstrap modal
title
}
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('dados/dados_add')?>";
}
else
{
url = "<?php echo site_url('dados/dados_update')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data:$('#form').serialize(),
dataType: "JSON",
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
and this is my Modal Form to save
<!-- Bootstrap modal -->
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">dados Form</h3>
</div>
<div class="modal-body form">
<form action="#" method="post" enctype="multipart/form-data" id="form"
class="form-horizontal">
<input type="hidden" value="" name="ID"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Name</label>
<div class="col-md-9">
<input name="Name" placeholder="" class="form-control"
type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">City</label>
<div class="col-md-9">
<input name="City" placeholder="City" class="form-control"
type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Address</label>
<div class="col-md-9">
<input name="Address" placeholder=""
class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Last Name</label>
<div class="col-md-9">
<input name="lastname" placeholder="" class="form-control"
type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Image</label>
<div class="col-md-9">
<input type="file" name="userfile" placeholder="" class="form-control">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger" data-
dismiss="modal">Cancel</button>
<input type ="submit" name="submit" value="Salvar" id="btnSave "
onclick="save()" class="btn btn-primary" />
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- End Bootstrap modal -->
</body>
</html>
First, try adding a '/' to the end of 'upload_path' => "./assets/uploads" in your code. This way you have a complete path for the image to go to.
Also, just so your aware, uploading an image like this will save the image on your server in the path provided. This means you need to store the file name in the database.So Make sure that 'Image' =>$data2['upload_data']['file_name'] actually has the correct file name so that when you query that file name from the db you can then find it at ./assets/uploads/filename on your server.
Additionally,
What do you get if you var_dump($data2['upload_data']['file_name'])?
What do you get if you var_dump($this->upload->display_errors()) after calling do_upload?
after searching some ajax codes, this is what worked for me
I changed my ajax function save to this
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('dados/dados_add')?>";
}
else
{
url = "<?php echo site_url('dados/dados_update')?>";
}
$('#form').submit(function(e)
{
$.ajax({
url : url,
type: "POST",
data: new FormData(this),
dataType: "JSON",
processData:false,
contentType:false,
cache:false,
async:false,
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
});
}

JQuery Live Search in codeigniter

I want to make live search using codeigniter and jquery and mysql
but when i type something the result's not showing
here is my model code :
<?php
class artikel_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function cari_artikel($q)
{
$this->db->select('judul,contain');
$this->db->like('judul', $q);
$this->db->or_like('contain', $q);
$query = $this->db->get('artikel');
return $query->result_array();
}
}
?>
and here is my controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('artikel_model');
}
public function index()
{
if(isset($_GET['q']) && $_GET['q']){
$q = $this->input->get('q');
$this->data['data']=$this->artikel_model->cari_artikel($q);
}
else{
$this->data['data']=array_fill_keys(array('judul', 'contain'), NULL);
}
$this->data['body']='dashboard';
$this->load->view('welcome_message',$this->data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
this is my view code
<?php $this->load->helper('html');?>
<div class="row">
<div class="span12 offset3">
<form class="form-inline" name="form1" method="get" action="">
Search : <input type="text" class=span5 name="q" id="q"/> <label for="mySubmit" class="btn btn-primary"><i class="icon-search icon-white"></i></label> <input id="mySubmit" type="submit" value="Go" class="hidden" />
</form>
</div>
</div>
<?php echo br(15); ?>
<div id="result"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
var allow = true;
$(document).ready(function(){
$("#q").keypress(function(e){
if(e.which == '13'){
e.preventDefault();
loadData();
}else if($(this).val().length >= 2){
loadData();
}
});
});
function loadData(){
if(allow){
allow = false;
$("#result").html('loading...');
$.ajax({
url:'http://localhost/helpdesk?q='+escape($("#q").val()),
success: function (data){
$("#result").html(data);
allow = true;
}
});
}
}
</script>
<?php
foreach($data as $row){ ?>
<h3><?php echo $row['judul']; ?></h3>
<?php } ?>
The live search is not working.
when i type something the output just "loading..."
and the result is not showing..
If i press enter, the result will show. but that is just the usual searching method. Not live searching.
Make a new function say "live_search" that will only return the result with the ajax search and not the "index" function. This new function will return the result with the HTML markup.
public function live_search(){
if($this->input->post(null)){
//put your search code here and just "echo" it out
}
}
$.ajax({
url:'http://localhost/domain/controller/live_search',
type : 'POST',
data : { 'q' : escape($("#q").val()) },
success: function (data){
$("#result").html(data);
allow = true;
}
});
Hope it helps you.

Ajax Request on jQuery Mobile using with Codeginiter

Currently Im trying to do ajax request using jQuery Mobile. I`m trying to use example on http://www.giantflyingsaucer.com/blog/?p=2574. Yes, it works perfectly. But problems was coming when i tried to use the code inside codeigniter.
Here is my controller :
<?php
class Ajax extends CI_Controller {
function __construct() {
}
function index() {
?>
<!DOCTYPE html>
<html>
<head>
<title>Submit a form via AJAX</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" />
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js"></script>
</head>
<body>
<script>
function onSuccess(data, status)
{
data = $.trim(data);
$("#notification").text(data);
}
function onError(data, status)
{
// handle an error
}
$(document).ready(function() {
$("#submit").click(function(){
var formData = $("#callAjaxForm").serialize();
$.ajax({
type: "POST",
url: "callajax",
cache: false,
data: formData,
success: onSuccess,
error: onError
});
return false;
});
});
</script>
<!-- call ajax page -->
<div data-role="page" id="callAjaxPage">
<div data-role="header">
<h1>Call Ajax</h1>
</div>
<div data-role="content">
<form id="callAjaxForm">
<div data-role="fieldcontain">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" value="" />
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" value="" />
<h3 id="notification"></h3>
<button data-theme="b" id="submit" type="submit">Submit</button>
</div>
</form>
</div>
<div data-role="footer">
<h1>GiantFlyingSaucer</h1>
</div>
</div>
</body>
</html>
<?php
}
function callajax() {
echo "ok";
}
}
I want to display 'ok' in notification. Im trying to call function callajax instead of call from callajax.php is this possible or do I need to create callajax.php?
Ive tried to create callajax.php but it was failed.
Thank you,
You probably want the url to be:
url: "ajax/callajax",
As you are calling the callajax function within the controller ajax.

Categories