Cake PHP div refresh server crashed - ajax

I'm using cake php
For 8 times I have used div refresh pattern from the server but after few minutes my server crashed and I have to ask the server provider to reopen the server.
What would be the best way to proceed if I want to use the database through div refresh?
function check()
{
$.ajax({
type : 'POST',
url : '<?php echo $this->webroot;?>'+'Bookings/rand_f',
dataType : 'html',
data: {},
cache : false,
success:function(result){
$('#ht').html(result);
$("#session_value").val(result);
}});
}
setInterval(check, 1000);
function check_count()
{
var a=$("#session_value").val();
$.ajax({
type : 'POST',
url : '<?php echo $this->webroot;?>'+'Bookings/check_count/'+a,
dataType : 'html',
data: {},
cache : false,
success:function(result){
if(result=='change'){
$('#res').val(result);
listings_live1();
}else{
$('#res').val('no_change');
}
}});
}
setInterval(check_count, 1000);
function listings_live1()
{
var a=$("#session_value").val();
$.ajax({
type : 'POST',
url : '<?php echo $this->webroot;?>'+'Bookings/listingsbookingcheck/'+a,
dataType : 'html',
data: {},
cache : false,
success:function(result){
var x= $('#live_listing').html(result);
if(x){
return 1;
}
}});
}
function check_count_all()
{
var x=$("#last_count_all").val();
$.ajax({
type : 'POST',
url : '<?php echo $this->webroot;?>'+'Bookings/check_count_all',
dataType : 'html',
data: {},
cache : false,
success:function(result){
$("#count_all").val(result);
if(result!=x){
listings_liveall();
$("#last_count_all").val(result);
$("#st").val('change');
}
else{
$("#st").val('nochange');
}
}});
}
setInterval(check_count_all, 1000);
function listings_liveall()
{
$.ajax({
type : 'POST',
url : '<?php echo $this->webroot;?>'+'Bookings/listingall',
dataType : 'html',
data: {},
cache : false,
success:function(result){
$('#listings_liveall').html(result);
}});
}
function showaction(str,id){
$("#boking_status_call_cancel").show();
$("#alertshow").html('Are you sure you want to '+str+' this Booking?');
if(str!='accept'){
$("#reject_reason").show();
}else{
$("#reject_reason").hide();
}
$("#role_call").val(str+','+id);
}
function update_mobile_booking(){
var strcall=$("#role_call").val();
$.ajax({
type : 'POST',
url : '<?php echo $this->webroot;?>'+'Bookings/update_booking_mobile/'+strcall,
dataType : 'html',
data: {},
cache : false,
success:function(result){
var x=listings_live1();
if(x==1){
listings_liveall();
}
}});
}
/*settimeout page load mobile booking*/
var myFunc = function() {
listings_live1();
listings_liveall();
}
window.onload = function() {
setTimeout(myFunc, 1000);
}
/*
function check_counter_upper(){
c1=$("#session_value").val();
c2=$("#count_chk_cl").val();
if(c1!=c2 && c2!='undefined'){
listings_live1();
$("#st1").val('force_change');
}else{
$("#st1").val('no_force_change');
}
}
setInterval(check_counter_upper, 4000);*/

Related

AJAX sending data to laravel controller and it is not working

I am trying to send user slug to my laravel controller and its not working
here is my html code :
<a class="card" onclick="chat_send($data = '{{$user->slug}}');">
here is my script :
function chat_send(){
var data = $data;
var action_url = '{{ route('chat.show','data')}}';
$.ajax({
type : "POST",
url : action_url,
data : {data:data},
dataType : "json",
success:function(data){
if(data.errors){
alert('Message Field is Empty.');
}
if(data.success){
alert('Success');
}
}
});
};
controller code :
public function show(Request $request)
{
$slug = $request->get('data');
return view('chat',compact('slug'));
// return view('test');
}
this is the error I got :
Console log
Just do this error will be resolved.
<a class="card" onclick="chat_send('{{$user->slug}}');">
And in script:
function chat_send(data){
var action_url = '{{ route('chat.show','data')}}';
$.ajax({
type : "POST",
url : action_url,
data : {data:data},
dataType : "json",
success:function(data){
if(data.errors){
alert('Message Field is Empty.');
}
if(data.success){
alert('Success');
}
}
});
};
try like below:
<a class="card" id="card" data = '{{$user->slug}}'>
Use JQuery for sending ajax:
$(document).ready(function(){
$('#card').click(function(){
var data = $(this).attr(data)
var action_url = '{{ route('chat.show','data')}}';
$.ajax({
type : "POST",
url : action_url,
data : {data:data},
dataType : "json",
success:function(data){
if(data.errors){
alert('Message Field is Empty.');
}
if(data.success){
alert('Success');
}
}
});
};
})
})

ajax is not sending data to controller

ajax is not sending data to controller
var url = "<?php echo base_url(); ?>";
function allowip() {
var url = "<?php echo base_url(); ?>"
var id = $('#path').attr('value');
$.ajax({
type: "POST",
url: url+"client/home/get_ip",
//dataType: 'text',
data: {'id': id},
//cache: false,
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
alert is showing with data but not sending data to controller
controller is empty
public function get_ip($id)
{
$day = $this->input->post('id');
echo $day;
}
As your get_ip function have argument $id so it must need to pass on it, url keyword might be conflicting so use different variable name and get the value from #path with val() function.
It might help:
function allowip() {
var id = $('#path').val();
var request_url = "<?php echo base_url(); ?>"+"client/home/get_ip/"+id;
$.ajax({
type: "POST",
url: request_url,
data: {'id': id},
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
public function get_ip($id)
{
echo $id;
}
or you can do is to post $id in data as you're already doing it but only have to change some lines:
function allowip() {
var id = $('#path').val();
var request_url = "<?php echo base_url(); ?>"+"client/home/get_ip/";
$.ajax({
type: "POST",
url: request_url,
data: {'id': id},
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
public function get_ip()
{
$day = $this->input->post('id');
echo $day;
}
You are sending a parameter to a function so you can change your code like this
function allowip() {
var id = $('#path').attr('value');
var url = "<?php echo base_url(); ?>"+"client/home/get_ip/"+id
$.ajax({
type: "GET",
url: url,
dataType: "html",
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
and your action will be
public function get_ip($id)
{
echo $id;
}
I think it is probably the way you are getting the value. It's hard to tell without seeing the view code, but I will assume that $('#path') is an <input...> element of some sort. If that is correct then try getting the value like this
var id = $('#path').val();
Forget about using an argument with the get_ip() function. You are posting to the controller not creating a URL with argument values. Try this.
public function get_ip()
{
$day = $this->input->post('id');
echo !empty($day) ? $day : "No Input"; //or, an empty string instead?
}

Sending url using ajax on codeigniter

I have this Ajax script where I pass a link to the data variable link, but I
get a 412 error.
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {
link: $(this).data('link')
},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
I have tried
link: encodeURI($(this).data('link'))
And
link: encodeURIComponent($(this).data('link'))
as is suggested on other threads but I still get the 412 error message.
Hope this will help you :
you have added newline character to json data that is why you got error
Do like this :
var link = $(this).data('link');
data: {"link" : link},
/*----OR do this -----*/
data: {"link" : $(this).data('link')},
instead of this :
data: {
link: $(this).data('link')
},
Whole code should be like this :
var link = $(this).data('link');
/*console.log(link)*/
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"link" : link },
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
For More :https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412
Please change your code in data options. Use this way
data: {"id": ID},
i.e. store the value in a variable and send that variable in data option. If we assume
ID=$(this).data('link');`
Then the code will be as follows:
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"id":ID},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
Please check it.

Semantic UI custom form validation with Ajax

I am trying to implement custom form validation rules for my username field, which should check the database for duplicated or not.
After Searching on the internet, I had write out some code, and it successful getting the response "true / false" from my server. But the fields in the form doesn't update as it should. Below was my javascript code.
$.fn.form.settings.rules.checkUsername = function(value) {
var res = true;
$.ajax({
async : false,
url: '<?= Yii::app()->createAbsoluteUrl('home/doCheckUsername') ?>',
type : "POST",
data : {
username : value
},
dataType: "json",
success: function(data) {
if (data.result == true) {
console.log(data.result);
return false;
} else {
console.log(data.result);
return true;
}
}
});
};
$('.ui.form').form({
username: {
identifier : 'username',
rules: [{
type : 'checkUsername',
prompt : 'Username Already Exists'
}]
}
// some other rules
}, {
inline : true,
on : 'blur',
onSuccess : function(){
//post to controller
}
});
The console.log showing me the correct result as I want, but I keep getting "Username Is Exist" as show at the picture
Semantic UI form Validation result
May I know where are the place I doing it wrong ?
Somehow, Added Async at the ajax solve the problem.
$.ajax({
async : false,
url: '<?= Yii::app()->createAbsoluteUrl('home/doCheckUsername') ?>',
type : "POST",
async: false,
data : {
username : value
},
dataType: "json",
success: function(data) {
if (data.result == true) {
console.log(data.result);
res = false;
}
}
});
Put result in the res variable and return the res variable after the ajax call !
Work for me :
$.fn.form.settings.rules.checkemail = function(value) {
var res = true;
$.ajax({
async : false,
url: '../php/email_exist.php',
type : "POST",
async: false,
data : {
email : value
},
success: function(data) {
console.log(data);
if (data == "0") {
console.log("OK");
res = true;
} else {
console.log("KO");
res = false;
}
}
});
return res;
};
$.fn.form.settings.rules.checkUsername = function(value) {
var res = true;
$.ajax({
async : false,
url: '<?= Yii::app()->createAbsoluteUrl('home/doCheckUsername') ?>',
type : "POST",
data : {
username : value
},
dataType: "json",
success: function(data) {
if (data.result == true) {
//console.log(data.result);
//return false;
**value = 0;**
} else {
//console.log(data.result);
//return true;
**value = 1;**
}
}
});
**return (value == 1 )? true : false;**
};
$('.ui.form').form({
username: {
identifier : 'username',
rules: [{
type : '**checkUsername[value]**',
prompt : 'Username Already Exists'
}]
}
// some other rules
}, {
inline : true,
on : 'blur',
onSuccess : function(){
//post to controller
}
});

Zend Framework: Ajax post data

That is my function:
function deleteAutoAd(id, title) {
alert(title);
$.ajax({
dataType: 'json',
url: '/ajax/deleteautoad',
type: 'POST',
data: {
id : id,
title : title
},
success: function(data) {
alert(data);
}
});
}
In this function when I try alert(title) and popup 'This is title'(because title = 'This is title'). But in ajax/deleteautoad I try this:
public function deleteautoadAction() {
if ($this->getRequest()->isPost()) {
echo $param1 = $this->_request->getParam('id');
echo $param2 = $this->_request->getParam('title');
}
}
Echo param1 show 5, but echo param2 don't show nothing.
i think you should try :
function deleteAutoAd(id, title) {
alert(title);
$.ajax({
dataType: 'json',
url: '/ajax/deleteautoad',
type: 'POST',
data: {
'id' : id, // Note the quotes
'title' : title // Note the quotes
},
success: function(data) {
alert(data);
}
});
}
regards
mimiz
I agree with the above answer
Also, For my application, I also have to post the full url. So you might want to try
var root = location.protocol + '//' + location.host;
and then on your url
url: root+'/ajax/deleteautoad',
You should return from controller a valid JSON with Success status.
public function deleteautoadAction() {
if ($this->getRequest()->isPost()) {
$x->Status = 'Success';
$x->Message = $this->_getParam('id') . $this->_getParam('title');
$this->_helper->json($x);
}
}
And also you should change your alert statement
function deleteAutoAd(id, title) {
$.ajax({
dataType: 'json',
url: '/ajax/deleteautoad',
type: 'POST',
data: {
id : id,
title : title
},
success: function(data) {
alert(data.Message);
}
});
}
Moved from the OP question to a CW answer:
I fixed the problem by removing: dataType: 'json'.

Resources