Alert box is not showing inside codeigniter controller - codeigniter

In this controller alert box is not working inside if loop.if condition is working,only problem with the alert box,but its not showing the dialog box.Please help. i tried so many times.
Controller Erp_c
function tabl()
{
$result['query2']=$this->erp_m->getregion();
$this->load->view('head1');
$this->load->view('header3');
$this->load->view('userregionview',$result);
}
function userregioninsert($user)
{
if($this->input->post())
$m1=$this->input->post('reg');
// print_r($m1);die;
$result['query']=$this->erp_m->insertregion($m1,$user);
$result['query2']=$this->erp_m->getregion();
// print_r($result['query']);die;
if(($result['query'])>0)
{
?>
<script type=text/javascript>alert("Region already added");</script>
<?php
}
else
{
?>
<script type=text/javascript>
alert("Name Available");
</script>
<?php
$this->erp_m->insertregion2($m1,$user);
}
redirect('Erp_c/tabl');
}
model
erp_m
function getregion()
{
$query2=$this->db->get('region3');
return $query2->result();
}
function insertregion($m1,$user)
{
$this->db->where('region',$m1);
$res=$this->db->get('region3');
$num=$res->num_rows();
return $num;
}
function insertregion2($m1,$user)
{
$data=array('region'=>$m1,'user'=>$user);
$this->db->insert('region3',$data);
}

You are missing with "".
This <script type=text/javascript> Should come as like this <script type="text/javascript">
<script type="text/javascript">
alert("Region already added");
</script>
<script type="text/javascript">
alert("Name Available");
</script>
And Check empty with empty() function
Edit 01
function insertregion($m1)
{
$query = $this->db->query("SELECT * FROM table_name WHERE region='$m1' ");//change table name
$result = $query->result_array();
$count = count($result);
return $count;
}
And instead of using this if ( $this->input->post() ) use if(isset($_POST['reg']))
So your Final Answer would be
function userregioninsert($user)
{
if(isset($_POST['reg']))
{
$m1 = $this->input->post('reg');
$count = $this->erp_m->insertregion($m1);
$result['query2'] = $this->erp_m->getregion();
if ($count==1)
{
?>
<script type="text/javascript">
alert("Region already added");
</script>
<?php
}
else
{
?>
<script type="text/javascript">
alert("Name Available");
</script>
<?php
$this->erp_m->insertregion2($m1, $user);
}
}
else
{
redirect('Erp_c/tabl');
}
}

e.g.
echo "<script>alert('This card was not approved, Thanks.');</script>";
also you can con-cat PHP value in echo

echo '<script>alert("Region already added");</script>';
try this

Related

i want to search in codeigniter and view result is which i searched

Here is my Model:
function get_search() {
$match = $this->input->post('search');
$this->db->like('p_category',$match);
$this->db->or_like('p_place',$match);
$this->db->or_like('p_price',$match);
$query = $this->db->get('tbl_property');
if($query->num_rows() > 0)
return $query->result();
else
return FALSE;
}
Here is my View:
<?php
foreach($query as $pack_details):
?>
<table class="table table-hover table-bordered">
<?php echo $pack_details->p_title?>
</table>
<?php
endforeach;?>
want to search and will show result only which i searched.
This is an old one of mine, but you may be able to adapt it to yours. Your form sends the post to the controller. The variable $art is the search item. The variable $num is the number of results. The jquery sends the form to the article class search_results
Controller
public function seek()
{
// this is the artist search display
$art = html_escape(trim($this->input->post('art')));
$this->db->like('artist', $art);
$this->db->select('artist, song, album, mix_name');
$query = $this->db->get('podcasts');
$num = $query->num_rows();
echo "<h3>We found $num $art song's</h3>";
if($query->num_rows() > 0) {
foreach ($query->result() as $row) {
echo "<li class='search_list'>Song: $row->song <br> Album: $row->album <br> Podcast: $row->mix_name</li> ";
}
}else {
echo "You searched for $art, Please check your spelling, or ensure the artists name is complete";
}
}
Jquery if you want the search to show up on the same page
$(function () {
"use strict";
$("#search").submit(function () {
var data = $('#search').serialize();
//alert(data); return false;
$.ajax({
url: "/display/seek",
data: data,
type: "POST",
success: function (msg) {
$('article.search_results').html(msg).show();
}
});
return false;
});
});
View is simply. See the last line in the jquery
<article class="search_results"></article>

Wordpress ajax request returns 0 in my infinite scroll script

I'd like to implement a very customized infinite scroll. But that comes later, cause i'm already stuck at the ajax request. What's wrong with my code (i get alwas a '0' response even though i have die() in the php function and added nopriv ...)??
my header.php:
<script type="text/javascript">
var count = 2;
var total = <?php echo $wp_query->max_num_pages; ?>;
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() == jQuery(document).height() - jQuery(window).height()){
if (count > total){
return false;
}else{
loadArticle(count);
//viewsite();
}
count++;
}
});
function loadArticle(pageNumber) {
jQuery.ajax({
url: ajaxurl,
type:'POST',
data: {
action: 'ozinfinite_scroll',
page_no: pageNumber
},
success: function(html){
jQuery("#inf-cont-1").append(html);
}
});
return false;
}
</script>
functions.php:
function ozinfinite_scroll(){
// $loopFile = $_POST['loop_file'];
$paged = $_POST['page_no'];
$posts_per_page = get_option('posts_per_page');
# Load the posts
query_posts(array('paged' => $paged ));
get_template_part( 'contentoz', 'blog' );
die();
}
add_action('wp_ajax_ozinfinite_scroll', 'ozinfinite_scroll'); // for logged in user
add_action('wp_ajax_nopriv_ozinfinite_scroll', 'ozinfinite_scroll'); // if user not logged in
function add_ajaxurl_cdata_to_front(){ ?>
<script type="text/javascript"> //<![CDATA[
ajaxurl = '<?php echo admin_url( 'admin-ajax.php'); ?>';
//]]> </script>
<?php }
add_action( 'wp_head', 'add_ajaxurl_cdata_to_front', 1);
thanks for your help!!
According to the docs http://codex.wordpress.org/AJAX_in_Plugins
you should have something like
echo $someValue
before wp_die() or die()

selectbox doesn't change by ajax

I have ajax code that display the name of the cities(in selectbox) according the area that has been chosen:
<?PHP if ($_POST) { ?>
$(document).ready(function(){
$('#areaID').filter(function(){
var areaID=$('#areaID').val();
var cityID=<?PHP echo $cityID ?>;
$('#cityID').load('js/ajax/getCities.php?areaID=' + areaID+'&cityID=' + cityID);
return false;
});
});
<?PHP }else { ?>
$(function () {
function updateCitySelectBox() {
var areaID = $('#areaID').val();
$('#cityID').load('js/ajax/getCities.php?areaID=' + areaID);
return false;
}
updateCitySelectBox();
$('#areaID').change(updateCitySelectBox);
});
<?PHP } ?>
The problem is - after user submit the form and get error (i.e - forget to fill some field), user can change the area but the cities select box doesn't change according the new area.
What is wrong with the code?
<p><label>area</label>
<select name='areaID' id='areaID'>
<?PHP
$query = mysql_query("SELECT * FROM `areas` ORDER BY id ASC ");
while($index = mysql_fetch_array($query))
{
$db_area_id = $index['id'];
$db_area_name = $index['name'];
if ($db_area_id == $areaID)
echo "<option value='$db_area_id' selected>$db_area_name</option>";
else
echo "<option value='$db_area_id'>$db_area_name</option>";
}
?>
</select><span>*</span>
</p>
<p><label>City</label>
<select id='cityID' name='cityID'> </select>
</p>

questions about MVC architecture in web application

I have certain questions since I'm doing a web app, and I feel kinda confused about keeping the mvc structure in a web application without using any frameworks.
Here is the structure of my app.
index:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<!--<link rel="stylesheet" type="text/css" href="index.css" />-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script src="js/functions.js"></script>
<script>
$(document).ready(function() {
loginSend();
});
</script>
<title></title>
</head>
<body>
<div id="login">
Username: <input type="text" id="username" name="username"><br>
Password: <input type="password" id="password" name="password"><br>
<input id="send" type="button" value="Enviar">
</div>
</body>
</html>
The loginsend function sends the data to service.php
<?php
include('server/main.php');
$action = $_POST['action'];
switch($action){
case "loginCheck":
$username=$_POST['username'];
$password=$_POST['password'];
$users = new users();
echo $users->loginCheck($username,$password);
break;
}
?>
And the service uses some classes I defined in main.php
<?php
class db{
public function conn() {
try{
$dbhost = "localhost";
$dbname = "eout";
$dbuser = "root";
$dbpass = "";
if($conn = new PDO("mysql:host=".$dbhost.";dbname=".$dbname, $dbuser, $dbpass)){
return($conn);
}
}
catch (Exception $e){
echo "Se ha presentado un error al conectar con la base de datos".$e;
}
}
}
class users{
function loginCheck($username, $password) {
try{
$db = new db();
$conn = $db->conn();
$pass_encriptada = md5 ($password);
$SQL_LOGIN_CHECK = "SELECT * FROM users where username='".$username."' and password='".$pass_encriptada."' and deleted='0'";
$conn->prepare($SQL_LOGIN_CHECK);
foreach($check = $conn->query($SQL_LOGIN_CHECK) as $row) {
$username_check = $row['username'];
if ($username_check == $username){
session_start();
$_SESSION['logged'] = 1;
$_SESSION['user_id'] = $row['id'];
$_SESSION['username'] = $row['username'];
$_SESSION['namelastname'] = $row['name'] ." ".$row['lastname'];
$usertype_id = $row['usertype_id'];
if ($usertype_id == 1 ){ //problema
$_SESSION['teacher'] = 1;
$response = "teacher";
$json = json_encode($response);
echo $json;
}
if ($usertype_id != 1 ){ //problema
$response = "user";
$json = json_encode($response);
echo $json;
}
}
}
}
catch(Exception $e){
echo "Se ha presentado un error en loginCheck".$e;
}
}
}
?>
In this case, I understand that index.php its my view, however I have certain doubts about the model/controller part. I understand in my example that the main file is the one that interacts with data, so it must be the model... in my case... its service.php my controller? Since is the one that interacts with the model? I feel kinda confused about this. In case I am wrong. How can I make a controller?
Thanks in advance.

Ajax Pagination Error in codeigniter

i have the following code which i modiefied from a tutorial
but the problem is that, i cant display the result properly in my view page..
if i display the array in the controller then its working properly, but in the view, its throwing the error..
<?php
class test extends CI_Controller {
public function index($start = 0) {
$this->load->model('pages_model');
$data_page=$this->pages_model->get_pages();
$this->load->helper('url');
$this->load->library('pagination');
$config['base_url'] = base_url().'test/index';
$config['total_rows'] = count($data_page);
$config['per_page'] = 5;
$data['user'] = array();
for($i=$start; $i<$start+$config['per_page']; $i++)
{
if (isset($data_page[$i])) {
$data['user'] = $data_page[$i];
}
}
//print_r($data['user']['page_id']); // this line displays 3, since the tupple with id 3 has maximum priority 5
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
if ($this->input->post('ajax')) {
$this->load->view('test', $data);
} else {
$this->load->view('test', $data);
}
}
}
?>
now comes the model
<?php
class pages_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_pages()
{
return $this->db->query("SELECT * FROM td_pages ORDER BY page_priority ASC")->result_array();
}
}
?>
and finally the view
<!DOCTYPE html>
<html>
<header>
<title>CodeIgniter Ajax Jquery Pagination</title>
<script src="<?php echo base_url(); ?>assets/front_assets/js/jquery-1.7.1.min.js"></script>
<script>
$(function() {
applyPagination();
function applyPagination() {
$("#ajax_paging a").click(function() {
var url = $(this).attr("href");
$.ajax({
type: "POST",
data: "ajax=1",
url: url,
beforeSend: function() {
$("#content").html("");
},
success: function(msg) {
$("#content").html(msg);
applyPagination();
}
});
return false;
});
}
});
</script>
</header>
<body>
<div id="content">
<div id="data">
<?php foreach($page_frag as $ut)
{?>
<div><?php echo $ut['page_slug'];?></div>
<?php } ?>
</data>
<div id="ajax_paging">
<?php echo $pagination; ?>
</div>
</div>
</div>
</body>
</html>
the problem is that the view is displaying the results in wrong way, but if i display in controller, then it shows that the array in the controller is working perfectly...
please help me solving the prob
You are loading the entire view in your ajax request which also contains the body and head text, Create a seperate view with the following content and load it in your ajax request.
<div id="data">
<?php foreach($page_frag as $ut)
{?>
<div><?php echo $ut['page_slug'];?></div>
<?php } ?>
</div>
What you're doing is WRONG
Pagination is meant to off-load processing power/ram from the server not only from the client side.
You're pulling off all your result then paginate them while you should be asking MySQL to paginate it for you & have a second query to give you the number of results to use it.
If you have index.php in your links then you must be using site_url() instead of base_url() & please note that both accepts an argument so you do not need to concatenate it the way you did it would be like this:
site_url('test/index');
codeigniter uses GET method, while in your javascript code you're using POST.
An easier method would be to catch the pagination li > a tags for the pagination & process it inside a container.

Resources