Codeigniter ajax pass variable class - ajax

My AJAX in view
<script type="text/javascript">
$(document).ready(function(){
$('.calendar .day').click(function(){
var day_data= $(this).find('.content').html();
if($(this).find('.content').html()!=null){
$.ajax({
url: "<?php echo base_url('calendar/pass_name')?>/",
method:"POST",
data:day_data,
success:function(data){
alert(data);
}
})
My controller
function pass_name(){
$name=$this->input->post('.calendar .day');
echo $name;
return $this->load->model('calendar_model')->get_description($name);
}
My model
function get_description($name){
$query=$this->db->select('description')->from('calendar')->where('name'==$name);
return $query;
}
What I would like to do is pass this variable through ajax, run a db query and return the result in a popup box.
Thanks in advance for any tips.

Try This code:
<script type="text/javascript">
$(document).ready(function(){
$('.calendar .day').click(function(){
var day_data= $(this).find('.content').html();
if($(this).find('.content').html()!=null){
$.ajax({
url: "<?php echo base_url('calendar/pass_name')?>/",
type:"POST",
data:{day_data:day_data},
success:function(data){
if(data==1)
{
$('#response').html(data);
}
}
})
</script>
Changer IN Your Controller
function pass_name()
{
$name=$this->input->post('day_data');
if($this->load->model('calendar_model')->get_description($name)==TRUE)
{
return 1;
}
}

Related

Redirect failed in codeigniter

I am trying to get the fc_id of the immunization then past it in profiling but the problem is the redirect() doesn't work. I successfully transfer my fc_id but the problem is i can't go to profiling view when i click the button.
Here some photo to understand my question clearly
So here's my code.
View:
$(document).on("click", "#view_profile", function(e){
e.preventDefault();
var view_id = $(this).attr("value");
$.ajax({
url: "<?php echo base_url(); ?>immunization/view_profile",
type: "post",
dataType: "json",
data: {
view_id: view_id
}
});
});
View Profile
Immunization Controller:
public function view_profile(){
$this->load->helper('url');
$this->load->library('session');
if ($this->input->is_ajax_request()) {
$view_id = $this->input->post('view_id');
$this->session->set_tempdata('view_id', $view_id);
redirect('profiling/index');
}
}
Profiling Controller:
public function index(){
$this->load->helper('url');
$this->load->library('session');
$this->load->view('profiling');
}
You are trying to redirect in an ajax request and thats not gonna work. Just post back some message if everything is ok and in ajax succes check that message and redirect from there with location.href = yoururl like:
In your php:
$this->session->set_tempdata('view_id', $view_id);
echo json_encode('msg' => 'ok');
js:
$.ajax({
url: "<?php echo base_url(); ?>immunization/view_profile",
type: "post",
dataType: "json",
data: {
view_id: view_id
}
success: function(resp){
if (resp.msg == 'ok') location.href = 'profiling/index';
}
});

Ajax post calling wrong url

Good day.
I want to know why is my project calling the wrong url? The code is:
SCRIPT
$.ajax({
type: "POST",
url: "/Application/Franchise",
data: JSON.stringify(sendInfo),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
$('#myModal3').modal('hide'); //hide the modal
},
error: function () {
alert("Error while inserting data");
}
});
CONTROLLER
public class ApplicationController : Controller
{
public ActionResult Franchise()
{
return View();
}
}
I even tried changing the url by 'Url.Action("Franchise", "Application")' but still the system kept on bringing me to http://localhost:49267/Home/Franchise.
I can't understand what is wrong here. Is there a bug in jquery ajax url post?
Thanks in advance.
UPDATE
<button class="btn btn-success" type="button" id="btnCapture">Submit</button>
#section Scripts{
<script src="~/Scripts/webcam.min.js"></script>
<script src="~/Scripts/webcam.js"></script>
<script language="JavaScript">
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#my_camera' );
</script>
<script language="JavaScript">
function take_snapshot() {
Webcam.snap( function(data_uri) {
document.getElementById('results').innerHTML =
'<img id="base64image" src="' + data_uri + '"/>';
} );
}
</script>
<script>
$(document).on("click", ".open-camera", function () {
var myBookId = $(this).data('id');
$(".modal-body #franid").val(myBookId);
})
</script>
<script>
$(function(){
$('#btnCapture').on('click', function(){
var file = document.getElementById("base64image").src;
var franid = $("#franid").val();
var sendInfo = {
Imagee: file,
FranIDD: franid
};
$.ajax({
type: "POST",
url: "/Application/Franchise",
data: JSON.stringify(sendInfo),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
$('#myModal3').modal('hide'); //hide the modal
},
error: function () {
alert("Error while inserting data");
}
});
});
});
</script>
}
This is all my code.
Ok. So I found my problem. It was in my controller.
In my controller I tried adding return RedirectToAction("Franchise", "Home"); after the save command. As soon as I commented this, everything worked.
Thanks for the help again guys.
Please use the browser to access directly, not from within the IDE directly access, from the localhost:49267 should be from the IDE access, and you will write URL as http://www.google.com try.

ajax code to check new records at database

I am using laravel, and I want to check if there are new records inserted into the database, I want an Ajax code the returns with the result, I don't know ajax so please help me
this is my controller
public function newrecord($target_id){
$record = Message::where('target_id', $target_id)->get();
return $record->count();
}**strong text**
and this is my ajax code
$(document).ready(function(){
var ajaxCall=function()
{
$.ajax({
url:"{{ url('/record/'.$auth->id) }}" ,
type: "GET",
datatype:"html",
data:{},
success:function(data) {
$('.msgnum').html(data)
console.log('new record);
},
error: function(data) {
console.log('error');
}
});
}
setInterval(ajaxCall,5000);
});
all I get is just a loop or " new record " in the console log
Do I need to return anything to tell that there is a new file, any help?
Try this, of course modify the code to how you want it to work but wrap the whole thing in setInterval, you don't even need the document.ready()
<script type="text/javascript">
setInterval(function() {
$.ajax({
url:"{{ url('/record/'.$auth->id) }}",
type: "GET",
datatype:"html",
data:{},
processData:false,
success: function(data){
$('.msgnum').html(data)
console.log('new record');
error: function(data) {
console.log('error');
}
},
error: function(){}
});
}, 5000);
</script>

how to call controller with jquery ajax magento

I have created custom module in magento, I want to access controller using jquery ajax but I am not finding correct method for doing this.
Ajax code in template is
jQuery(".deleteAttrKeyId").on("click",function(){
var baseUrl="<?php echo Mage::getBaseUrl();?>";
var idArr=this.id.split("-");
attrKeyId=idArr[1];
alert(this.id);
jQuery.ajax({
type: "POST",
dataType: "JSON",
data :{'id':attrKeyId},
url :baseUrl+"groupprice/adminhtml_grouppricebackend/index",
complete:function(){alert("completed");
},
success:function(result){
alert(result);
}
});
});
Block code in layout is
<groupprice_adminhtml_grouppricebackend_index>
<reference name="content">
<block type="groupprice/adminhtml_grouppricebackend" name="grouppricebackend" template="groupprice/grouppricebackend.phtml"/>
</reference>
</groupprice_adminhtml_grouppricebackend_index>
Controller code is
<?php
class Group_GroupPrice_Adminhtml_GrouppricebackendController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
echo __FILE__;
}
}
use this
<script type="text/javascript">
function callController(){
new Ajax.Request("<?php echo $this->getUrl('groupprice/adminhtml_grouppricebackend/index') ?>", {
method: 'Post',
parameters: {"parameter_name":"value"},
onComplete: function(transport) {
alert(transport.responseText);
}
});
}
add this code in phtml
<button type="button" onclick="callController()" title="submit" class="button">submit</button>
<script type="text/javascript">
function callController(){
new Ajax.Request("<?php echo $this->getUrl('module/controller/action') ?>", {
method: 'Post',
parameters: {parameter_name:value},
onComplete: function(transport) {
alert(transport.responseText);
}
});
}
</script>

How to associate an event to Html.ActionLink in MVC3

I have made an Ajax function but i am getting a big prolem in that.
I was displaying the contents on click of the link..
The links are fetched from the database and also the url of the links are fetched from the datbase.
I have wriiten ajax to call the contents dynamically on click of the link
<script type="text/javascript">
$(document).ready(function () {
$('a').click(function (e) {
e.preventDefault();
var filename = $(this).text();
var Hobbyurl = '#Url.Action("FetchUrlByHobbyName")';
$.ajax({
type: "POST",
url: Hobbyurl,
data: { data: filename },
success: function (returndata) {
$('iframe').attr('src', returndata);
}
});
});
});
</script>
Now FetchUrlByHobbyName is the function called from the Controller thart returns the url
//Ajax routine to fetch the hobbyinfo by hobbyname
[HttpPost]
public ActionResult FetchUrlByHobbyName(string data)
{
HobbyMasters hobbymaster = new HobbyHomeService().FetchHobbyMasterByHobbyName(data);
string url = hobbymaster.InformationUrl;
if (HttpContext.Request.IsAjaxRequest())
return Json(url);
return View();
}
And in my View i have written the link like this:
#foreach (var item in Model)
{
<li >#Html.ActionLink(item.HobbyName, "Hobbies")
</li>
}
i tried this :
#Html.ActionLink(item.HobbyName, "Hobbies", null, new { id = "alink" })
and then calling Ajax on click of 'alink' but with this my ajax function doesnot get called.
Now the problem is the ajax function is getting called on click of every link on the page..
I want to assign a unique Id to it but i am not understanding how to do that
please Help me...
For that specific link, assign an id. E.g
<a id="someID" href="url">Link</a>
and than bind the click only with that link.
$('#someID').click(function (e)) ....
If I understood you correctly this helps you
The text of the link
<script type="text/javascript">
function myAjaxFunction(){
e.preventDefault();
var filename = $(this).text();
var Hobbyurl = '#Url.Action("FetchUrlByHobbyName")';
$.ajax({
type: "POST",
url: Hobbyurl,
data: { data: filename },
success: function (returndata) {
$('iframe').attr('src', returndata);
}
});
</script>
Try to give a css class selector to you action link like this...
#Html.ActionLink("some link", "Create", "Some_Controller", new { }, new { #class = "test" })
then User jquery for it..
<script type="text/javascript">
$(document).ready(function () {
$('.test').click(function (e) {
e.preventDefault();
var filename = $(this).text();
var Hobbyurl = '#Url.Action("FetchUrlByHobbyName")';
$.ajax({
type: "POST",
url: Hobbyurl,
data: { data: filename },
success: function (returndata) {
$('iframe').attr('src', returndata);
}
});
});
});
</script>

Resources