Here is my ajax
$(document).ready(function()
{
$( ".iCheck-helper" ).on( "click", function(){
var value_to_send = $('.i-check:checked').map(function() {
//alert(this.value);
return this.value;
}).get().join(', ');
});
});
Here,its my URL '/hotel/hotelresults/'.folder_name/function_name and the my Controller Name is HotelController
How should I get the ""return this.value"" using ajax to the controller.
Can Someone help me.
Try this:
$.ajax({
type: "POST",
url: "hotel/hotelresults",
data: {
key : value
},
success: function (data) {
alert(data)
}
});
Route:
Route::post('hotel/hotelresults', 'YourController#YourMethod');
In YourController:
public function YourMethod(Request $request)
{
//
return $request->key; //or return Input::get('key');
}
Please read more
docs
Thank you so much #rome 웃
I Tried like this..
$(document).ready(function()
{
$( ".iCheck-helper" ).on( "click", function(){
console.log($('.i-check:checked').map(function() {
// alert(this.value);
var value = this.value;
$.ajax({
// alert();
type: "POST",
url: "hotelresults",
data: {
key : value
},
success: function (data) {
// alert(data);
}
});
}).get().join(', '));
});
});
And in Route:
Route::get('hotel/hotelresults', 'HotelController#postHotelresults');
And in my Controller:
public function postHotelresults(Request $request)
{
//
return $request->key; //or return Input::get('key');
}
Because of giving the URL as "url: "hotel/hotelresults"," It seems to be an error in my console
Related
I have this function:
public function delmultiplemessagesforce(Request $request){
$messages = Message::whereIn('id', $request->input('ids'))->get();
foreach($messages as $message){
$message->parent()->forceDelete();
$message->childs()->forceDelete();
$message->forceDelete();
}
}
it returns code 200 but will not actually delete data in database.
Ajax
<script>
$(function() {
$('.icheckboxdel').on('click', function(e) {
e.preventDefault();
var ids = $(".panel-body input:checkbox:checked").map(function(){
return $(this).val();
}).get();
$.ajax({
url: '{{ route("delmultiplemessagesforce")}}',
type: "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {'ids': ids},
success:function(data) {
alert('successfully deleted.');
window.location.reload();
}
});
});
});
</script>
Note: I have same function for delete() item and it works but for
forceDelete() it doesn't.
Any idea?
I'm trying to make a pagination with ajax and laravel 5, but I can not do the ajax works even with simple tests:
$(document).on('ready',function(){
$('.pager a').on('click', function (e) {
var page = $(this).attr('href').split('page=')[1];
e.preventDefault();
$.ajax({
type: "GET",
url: 'testeserver.php',
//url:"raphael.dev/testeserver.php",
dataType: 'json', // Notice! JSONP <-- P (lowercase)
success:function(json){
alert("Success"+json);
},
error:function(){
alert("Error");
}
});
});
});
In this example I try one have a return on json ,
<?php
$arr = array("element1","element2",array("element31","element32"));
$arr['name'] = "response";
echo $_GET['callback']."(".json_encode($arr).");";
?>
but only with the error alert , in fact I'm trying with these following cases :
BlogController:
public function index(Request $request){
$artigos = Artigo::where('publicado_em', '<=', Carbon::now())
->orderBy('publicado_em', 'desc')
->paginate(config('blog.artigos_por_pagina'));
if ($request->ajax()) {
return Response::json(view('Blog.artigos', compact('artigos'))->render());
}
return view('Blog.index', compact('artigos'));
}
routes.php
post('/', 'BlogController#index');
get('/', 'BlogController#index');
get('/{slug}', 'BlogController#show');
jquery
$(document).on('ready',function(){
$('.pager a').on('click', function (e) {
var page = $(this).attr('href').split('page=')[1];
e.preventDefault();
$.ajax({
type: "POST",
url: 'page=' + page,
dataType: 'json',
success:function(json){
alert("Success"+json);
},
error:function(){
alert("Error");
}
});
});
});
Just found the solution, changed my jquery code to :
$(document).on('ready',function(){
$('.pager a').on('click', function (e) {
var page = $(this).attr('href').split('page=')[1];
e.preventDefault();
var url = '?page=' + page;
$.post( url, function(data) {
alert( "success"+data );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
And found a error in my BlogController :
in
if ($request->ajax()) {
return Response::json(view('Blog.artigos', compact('artigos'))->render());
}
the Facade Response was not declared, so just added
Use Request;
at the top,
but i still dont understand why the $ajax() dosent work, just the $get and $post
Laravel protects your application with CSRF tokens. When utilising Ajax you need to be sure to send across the CSRF token, otherwise Laravel will not process the request to protect your app.
$.ajax({
url: 'test',
type: "post",
data: {'_token': $('input[name=_token]').val()},
success: function (data) {
alert('the joys');
},
complete: function (data) {
}
});
Notice the $('input[name=_token]').val() - that will send your CSRF token. You will of course need to create a hidden input with the CSRF token in your view.
More information can be found here!
I want to call an ajax and display its response :
<script type="text/javascript">
function test() {
var pk = $('#salle_code').val();
var donne = {pk:pk};
var ret = $.ajax({
data: donne,
type: "POST",
url: "<?php echo HTTP_AJAX ?>salle/testAjax.php",
async: false
}).responseText;
return $.trim(ret);
}
$(document).ready(function(){
$('#salle_code').on("blur", function() {
if ($('#salle_code').val() != "") {
alert(""+test());
}
});
});
</script>
Code of the ajax :
<?php
$critere = array();
$critere['salle_code'] = $_POST['pk'];
$ret = Salle::lireParCritere($critere);
echo "111111111111111";
?>
At runtime the alert show a blank result ! So how to work with Phalcon and ajax and models ?
use following code and check browser console for response
$.ajax({
data: donne,
type: "POST",
url: "<?php echo HTTP_AJAX ?>salle/testAjax.php",
async: false
success: function (data) {
console.log(data)
},
error: function (textStatus, errorThrown) {
console.log(textStatus + " : " + errorThrown)
}
});
First you need define a route for AJAX request, e.g. /salle/test:
$router->add('/salle/test', [
'controller' => 'salle',
'action' => 'test',
))->beforeMatch(function ($uri, $route) {
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest') {
return false;
}
return true;
});
then create your action:
public function testAction()
{
// some work ..
$this->response->setJsonContent(json_encode(['foo' => 'bar']));
return $this->response;
}
then test:
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function test() {
var response = $.ajax({
type: "POST",
data: {},
url: '/salle/test',
success:function(results) {
console.log(results);
}
});
return response;
}
$(document).ready(function(){
console.log(test());
});
</script>
#Klay there was a very simple solution : I created an ajax action inside the controller of the actual view.
I have this code in my index.php:
$('#whois').click(function () {
//console.log("button clicked");
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
I want to pass a post value to test.php inside this script, like so:
$('#whois').click(function () {
//console.log("button clicked");
$.ajax({
type: "POST",
url: "test.php",
data: info,
success: function(){
//dont know what to write here
}
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
And then call this post value in test.php $_POST['info']
Maybe someone will understand what im saying.
this would be better
$.post("test.php", { info: "Something", moreinfo: "Something Else" })
.done(function(data) {
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
This should work , try it out:
$('#whois').click(function () {
$.ajax({
type: "POST",
url: "test.php",
data: {info: "some info"},
success: function () {
}
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
I am trying to have my form validate before the ajax runs but can't seem to get the coding right for this to happen.
if i seperate them i can post using ajax with no validation or validation but it posts the default way
here is my current code i am trying
$(document).ready(function(){
// ajax code start
$('#form').on('submit', function (e){
e.preventDefault();
if (spry.widget.form.validate('#form') == true)
{
$.ajax({
type: "POST",
url: "localProxy.php",
data: $('#form').serialize(),
success: function (response) {
document.location = '/thank-you';
// do something!
},
error: function () {
alert('There was a problem!'); // handle error
}
});
};
});
});
figured it out
$(document).ready(function(){
// ajax code start
$('#form').on('submit', function (e){
e.preventDefault();
Spry.Widget.Form.onSubmit = function(e, form)
{
if (Spry.Widget.Form.validate(form) == false) {
return false;
}
{
$.ajax({
type: "POST",
url: "localProxy2.php",
data: $('#consult').serialize(),
success: function (response) {
document.location = '/thank-you';
// do something!
},
error: function () {
alert('There was a problem!'); // handle error
}
});
};
};
});
});