Can some help me to solve this problem
I have a map on my website and i want to refresh map only on clicking on category my this is my product
<?php
/* $message = "in home";
echo "<script type='text/javascript'>alert('$message');</script>";*/
foreach($categories as $category){
echo '<li><img src="'.base_url('uploads/'.$category->image).'" alt="'.$category->cat_name.'" width="30" height="30"/></br><span>'.$category->cat_name.'</span></li>';
}
?>
</ul>
</div>
</div>
<script>
function changeData(category){
var urlCI = '.base_url('main/home/' .$category->adv_category).';
$.ajax({
type: "POST",
url: urlCI,
data:{'categoryReq':category},
success: function(response) {
if(response!=''){
$('map_marker').html(response);
}else{
return false;
}
}
});
}
</script>
my controller code is if you need other code please ask i will give. I give this just for refrence, thank you
public function home($cat=NULL)
{
//$cat=10;
//echo debug_backtrace()[1]['function'];
if($_POST)
{
print_result($_POST);
}
$sql="SELECT * FROM (`ad_category`) WHERE `status`=1 AND `del_status`=0";
$this->data['categories']=$this->db->query($sql)->result();
if($cat)
{
//$message = $cat;
//echo "<script type='text/javascript'>alert('$message');</script>";
//$this->data['middle_view']='home_again';
$this->data['ads']=$this->advt_m->get_advts($cat);
$this->input->post('map_marker',$this->data);
$this->data['subview']='home';
$this->load->view('__layout_main',$this->data);
}
else
{
/*$message = "Here 2";
echo "<script type='text/javascript'>alert('$message');</script>";
*/
$this->data['subview']='home';
//$this->data['middle_view']='home';
$this->data['ads']=NULL;
$this->load->view('__layout_main',$this->data);
}
var urlCI = '.base_url('main/home/' .$category->adv_category).';
probably should be
var urlCI = '<?php echo base_url("main/home/$category->adv_category"); ?>';
Split the Map div and its javascripts into another view.
Create function with all necessary functionalities for map. Add and load the it through Ajax call.
Ajax success:
success:function(response){
$('div').html(response);
//response will be the function which holds the $this->load->view('map');
}
map.php
<div id="map">
YOUR MAP GOES HERE
</div>
<script>
MAP SCRIPT
</script>
main.php (Your frontend)
<div id="map_here">
</div>
<script>
$.ajax({
type:"POST",
//Send some values
data:{
somevalue:'somedata',
category:"mine"
},
url:"<?php echo base_url();?>mycontroller/mapfunc",
success:function(response){
$('#map_here').html(response);
}
})
</script>
myconroller:
function mapfunc(){
//Using this value you can change the script
$data['category'] = $_POST('category');
$data['location'] = $_POST('somevalue');
//Process the works
$this->load->view('map',$data);
//On Ajax call this page can be loaded
}
Each ajax call it will reload the div.
any doubts just comment it...
Related
Unable to get value from database in codeigniter. I tried to fetch data based on select box value(menu_code) without refreshing page using ajax but I got result undefined.
This my controller's code : login.php
public function get_menu_rights()
{
if (isset($_POST['name']))
{
$root_id = $this->input->post('menu_root_id');
$data['res'] = $this->login_model->get_menu_check($root_id);
// print_r($data['res']);
echo json_encode($data);
//$this->load->view('pages/role_rights',$data);
}
}
Below is my model code login_model.php
public function get_menu_check($root_id)
{
$this->db->select('menu_code,menu_name');
$this->db->from('create_menu as C1');
$this->db->where('C1.menu_root_id',$root_id);
$this->db->order_by('menu_code');
return $this->db->get()->result_array();
}
This is my view code role_rights.php
<form action="<?php echo base_url('login/get_menu_rights');?>" method="post">
<?php
print"<select class=\"form-control\" name=\"menu_root_id\" onchange=\"javascript:__doPostBack();\" id=\"menu_root_id\">"; ?> <option value="select">select</option>
<?php foreach($result as $res) { ?>
<option value="<?php echo $res->menu_code; ?>">
<?php echo $res->menu_name.'-'.$res->menu_code; ?>
</option>
<?php } ?>
</select>
</form>
</div>
<script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
<script type="text/javascript">
function __doPostBack()
{
var name = document.getElementById('menu_root_id').value;
var dataString='name='+ name;
$.ajax({
type:"post",
url:"<?php echo base_url('login/get_menu_rights'); ?>",
data:dataString,
cache:false,
dataType: 'json',
success: function(data)
{
var id = data[0];
var vname = data[1];
$('#output').html("<b>menu_code: </b>"+id+"<b> menu_name: </b>"+vname);
}
});
return false;
}
</script>
</div>
<div id="output"></div>
Hope this will help you :
Replace
$root_id = $this->input->post('menu_root_id');
with
$root_id = $this->input->post('name');
Your controller's get_menu_rights method should be like this :
public function get_menu_rights()
{
$root_id = $this->input->post('name');
if(! empty($root_id))
{
$data = $this->login_model->get_menu_check($root_id);
// print_r($data);
echo json_encode($data);
exit;
}
}
Your ajax success function should be like this :
success: function(data)
{
var html = '';
$.each(data,function(k,v){
alert(v);
html += "<b>menu_code: </b>"+v.menu_code+"<b> menu_name: </b>"+v.menu_name
});
$('#output').html(html);
}
There are a few things I noticed
$data is an undefined array & you are settings the result array returned by the model function to it's 'res' key
dataString is not a json neither it's a js array that you are sending
since you used json_encode, you need to use JSON.parse(data) in the ajax success
if you do have the result in $data['res'], then you need to do something like this - data=JSON.parse(data)['res']; now you can get id from data[0]
I think the query return empty please try this Code.....
public function get_menu_check($root_id)
{
$data = $this->db->select('C1.menu_code,C1.menu_name')
->from('create_menu as C1')
->where('C1.menu_root_id',$root_id)
->order_by('C1.menu_code')
->get();
if($data->num_rows() >= 0)
return $data->result_array();
else
return false;
}
I'm trying to pass a php variable from page1.html (which contains a php variable that I got from another form) to page2.php using ajax but it's not working, so I need your help.
code from page1.php:
<script>
var url = 'page2.php';
$("#save").click( function() {
$.ajax({
url : url,
type: 'post',
data : {
admin : <?php echo $admin; ?>,
gname : <?php echo $gname; ?>,
filename : docname,
content : encodeURIComponent(editor.getValue())
}
});
});
</script>
The datas filename and content are being sent successfully but admin and gname are not.
Thanks in advance.
UPDATE:
<?php
if(isset($_GET['admin'])) {
$admin = $_GET['admin'];
}
if(isset($_GET['gname'])) {
$gname = $_GET['gname'];
}
?>
This code is where I get the php variable that I want to send, from another form which is irrelevant to this question.
Here you go buddy, replace your script code with this.
<script>
var url = 'page2.php';
var admin = '<?php echo $admin; ?>';
var gname = '<?php echo $gname; ?>';
$("#save").click( function() {
$.ajax({
url : url,
type: 'post',
data : {
admin : admin,
gname : gname,
filename : docname,
content : encodeURIComponent(editor.getValue())
}
});
});
</script>
Youre using the type post so must use the $_POST in php
PHP
<?php
if(isset($_POST['admin'])) {
$admin = $_POST['admin'];
}
if(isset($_POST['gname'])) {
$gname = $_POST['gname'];
}
?>
You are using $_GET['admin'] which is used to fetch parameters delivered with GET method. You should be using $_POST['admin'] since you have set POST as your http request type in the JS code.
As everyone is saying you're mixing POST and GET. But it caught another thing that may bother you. Are those variables strings? If they are, maybe you're missing the apostrophe/single quote.
<script>
var url = 'page2.php';
$("#save").click( function() {
$.ajax({
url : url,
type: 'post',
data : {
admin : '<?php echo $admin; ?>',
gname : '<?php echo $gname; ?>',
filename : docname,
content : encodeURIComponent(editor.getValue())
}
});
});
</script>>
Your below code
<?php
if(isset($_GET['admin'])) {
$admin = $_GET['admin'];
}
if(isset($_GET['gname'])) {
$gname = $_GET['gname'];
}
?>
is to be replaced by
<?php
if(isset($_POST['admin'])) {
$admin = $_POST['admin'];
}
if(isset($_POST['gname'])) {
$gname = $_POST['gname'];
}
?>
You are sending POST parameters,while using get in PHP script. And another issue is you are not using quotes with $admin and $gname variables in ajax request.
Try following :
<script>
var url = 'page2.php';
$("#save").click( function() {
$.ajax({
url : url,
type: 'post',
data : {
admin : '<?php echo $admin; ?>',
gname : '<?php echo $gname; ?>',
filename : docname,
content : encodeURIComponent(editor.getValue())
}
});
});
</script>
PHP Script:
<?php
if(isset($_POST['admin'])) {
$admin = $_POST['admin'];
}
if(isset($_POST['gname'])) {
$gname = $_POST['gname'];
}
?>
Check if it helps you.
I am submitting form data via Ajax and would like to display a message above the form on successful submit.
Currently the form does send the data successfully. It should render the feedback message on form submit <?php $this->renderFeedbackMessages(); ?> as defined in my config.php
Where am I going wrong? Possibly doing things in the wrong order due to first time working with mvc?
my config.php file I have the following defined;
define("FEEDBACK_BOOK_ADD_SUCCESSFUL", "Book add successful.");
my model;
public function addIsbn($isbn)
{
// insert query here
$count = $query->rowCount();
if ($count == 1) {
$_SESSION["feedback_positive"][] = FEEDBACK_BOOK_ADD_SUCCESSFUL;
return true;
} else {
$_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED;
}
// default return
return false;
}
my controller;
function addIsbn()
{
// $_POST info here
header('location: ' . URL . 'admin/searchIsbn');
}
my searchIsbn.php;
<?php $this->renderFeedbackMessages(); ?>
<div>
//my html form here
</div>
<div id="result"></div>
<script>
$('#form').submit(function() {
event.preventDefault();
var isbn = $('#isbn_search').val();
var url='https://www.googleapis.com/books/v1/volumes?q=isbn:'+isbn;
$.getJSON(url,function(data){
$.each(data.items, function(entryIndex, entry){
$('#result').html('');
var html = '<div class="result">';
html += '<h3>' + entry.volumeInfo.isbn + '</h3>';
html += '<hr><button type="button" id="add" name="add">add to library</button></div>';
$(html).hide().appendTo('#result').fadeIn(1000);
$('#add').click(function(ev) {
$.ajax({
type: 'POST',
url: '<?php echo URL; ?>admin/addIsbn',
data: {
'isbn' : isbn
}
});
});
});
});
});
</script>
No console error messages.
You are redirecting here:
header('location: ' . URL . 'admin/addIsbn');
remove it.
echo the success message here and add it to an HTML element's .html() API.
Your page will not be refreshed.
Your page is making the call to admin/addIsbn which is redirected to admin/searchIsbn. So you already have the output of renderFeedbackMessages() being sent to your function.
Use the success callback to output the results to the page:
$.ajax({
type: 'POST',
url: '<?php echo URL; ?>admin/addIsbn',
data: {
'isbn' : isbn
},
success: function(data) {
$('#result').html(data);
}
});
The only way I could get this to work was to add an auto-refresh to my Ajax success function as follows;
window.location.reload(true);
Working however open to suggestions.
I am trying to implement a ajax plus one button with my site in code igniter. I am very new with Ajax and codeigniter so I'm needing some guidance.
Here is the portion of my controller where I'm starting. Please note, this is inside the method that creates my profile view in which I'm trying to create this.
$voteId= $this->input->post('voteId');
$upOrDown= $this->input->post('upOrDown');
$status ="false";
$updateRecords = 0;
if($upOrDown=='upvote'){
$updateRecords = $this->community_model->updateUpVote($voteId);
}else{
$updateRecords = $this->community_model->updateUpVote($voteId);
}
if($updateRecords>0){
$status = "true";
}
echo $status;
// This variable will be accessed from the view
$data['airwave'] = $airwave;
$data['profile_id'] = $profile_id;
$this->load->view('includes/templates/main_page_template', $data);
}
Here is the method in the model:
function updateUpVote($voteId){
$sql = "UPDATE airwaves_comments set thumbsup = thumbsup+1 WHERE thumbsup =?";
$this->db->query($sql, array($voteId));
return $this->db->affected_rows();
}
here is my view:
<div id='thumb_holder'>
<div id='thumb_report'>
<a href='mailto:info#cysticlife.org'>
• report
</a>
</div>
<div class= 'thumb_counter'>
+0
</div>
<div id='thumb_thumb'>
<a class='myButtonLink voteMe' id='1_upvote'<span id="1_upvote_result">+</span>></a>
</div>
</div>
here is the script:
<script>
$(document).ready(function(){
$(".voteMe").click(function() {
var voteId = this.id;
var upOrDown = voteId.split('_');
$.ajax({
type: "post",
url: "account/profile/voteMe",
cache: false,
data:'voteId='+upOrDown[0] + '&upOrDown=' +upOrDown[1],
success: function(response){
try{
if(response=='true'){
var newValue = parseInt($("#"+voteId+'_result').text()) + 1;
$("#"+voteId+'_result').html(newValue);
}else{
alert('Sorry Unable to update..');
}
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
});
});
</script>
additionally here is what it looks like to give a better idea of what's going on, but I want there to be the number of votes after the plus sign:
To summarize: Essentially I was taking a tutorial on a demo tutorial that is similar, but it was not exactly what I am trying to do. This is basically me taking the code from the tutorial and trying to splice it with my code and meet my specific needs and learning ajax at the same time. I feel like I'm close as it's updating the specified column in my table (incorrectly),however can't get it to visibly function.
I'm not exactly sure what your saying your problem is. But This I do see.
When trying to fetch a view for ajax you have to return the view as data
http://ellislab.com/codeigniter/user-guide/general/views.html (bottom of the page)
Like this
$view = $this->load->view('includes/templates/main_page_template', $data, true);
echo $view;
This will send the rendered view to the browser
Put this code in view file
<input type="hidden" name="siteurl" id="siteurl" value="<?php echo site_url(); ?>">
<input type="hidden" name="baseurl" id="baseurl" value="<?php echo base_url(); ?>">
Then get hidden value in your script
var site_path = $("#siteurl").val();
var base_path = $("#baseurl").val();
$.ajax({
type: "post",
url: site_path+"account/profile/voteMe",....
I'm looking to make a like counter as like in facebook. I will load various posts from database with message id. i want to make like(here it is "pray") counter and it should do behind the page using jquery and ajax. I cant get specific message id(mid). here the code is
jQuery:
$(document).ready(function () {
$("div.pray").click( function()
{
var mid=$(this).val();
$.ajax(
{
type:"POST",
url:"increment.php",
data:'mid='+mid,
success:function ()
{
$("div.pray").text("Prayed");
}
});
});
PHP:
session_start();
if(isset($_SESSION['usrid']))
{
$usr = $_SESSION['usrid'];
include('dbcon.php');
$cot = "select * from posts where userid=$usr LIMIT 10";// order by eventDate DESC, eventHour DESC" LIMIT0,10";
$ex = mysql_query($cot,$con);
while($cont=mysql_fetch_array($ex))
{
$date = date_create($cont['date']);
$mid=$cont['mid'];
echo "<div id='posts'>";
echo $cont['message'];
echo $photo;
echo "<div class='pray'>";
echo "<input type='hidden' class='mid' value='$mid'>";
echo "<a href='#'>Pray</a></div>";
echo "Prayed(".$cont['prayers'].")    ";
echo date_format($date, 'd-m-Y H:i:s');
echo "<hr>";
echo "</div>";
}
}
else
{
echo "Login to see";
}
?>
i want to replace only the message which i clicked as pray and also it should load current number of prayed.
put your javascript AFTER your php script and check. If that fails, change your .click() handler to,
$(document).on('click', 'div.pray', function(){
var mid=$(this).children('.mid').val();
$.ajax(
{
type:"POST",
url:"increment.php",
data:'mid='+mid,
success:function ()
{
$("div.pray").text("Prayed");
}
});
//rest of your code
})
Notice, I've changed the first line from
var mid=$(this).val();
To
var mid=$(this).children('.mid').val();
As, $(this) will select the div.pray and not the input.mid element.
In the html code try something like this:
echo "<a href='increment.php?mid=$mid'>Pray</a></div>";
In javascript:
$(document).on('click', 'div.pray', function(e){
e.preventDefault();
var $target = $(e.target).closest('a');
$.ajax({
type:"POST",
url: $target.attr('href'),
success:function (data){
$target.closest("div.pray").html(data);
}
});
});
In the ajax response the html code should go into the div.pray