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');
}
}
});
};
})
})
Related
Here's code for Controller :
public function Update()
{
$id_form = $this->input->post('id_form');
$no_request = $this->input->post('no_request');
$data = {
"no_request" => $no_request,
"date_ps" => date('d M Y')
}
return $this->db->where('id_form', $id_form);
return $this->db->update('table', $data);
}
Here's code for View :
<input type="hidden" name="id_form" id="id_form"></br>
<input type="text" name"no_request" id="no_request"></br>
Edit
Here's code for Ajax :
$('#btn_update').on('click', function() {
var id_form = $('#id_form').val();
var no_request = $('#no_request').val();
var date_ps = $(date_ps).val();
$.ajax({
type: "POST",
url: "<?= site_url('Controller/Update') ?>",
dataType: "JSON",
data: {
id_form: id_form,
no_request: no_request,
date_ps: date_ps
},
success: function(data) {
console.log(date_ps);
}
})
}
But The result for date_ps is undefined, How can I get the date_ps value without having to undefined
Why i am not getting any value in my controller. Please help .
Here is my controller : welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function ajax_process(){
$data = array(
'email' =>$this->input->post('email'),
'password'=>$this->input->post('password')
);
echo json_encode($data);
}
}
**this is my view file : welcome_mesage.php
$(document).ready(function(){
$('#sbtn').click(function(){
var email = $('#email').val();
var password = $('#password').val();
var datas = $('#myform').serialize();
$.ajax({
type: "POST",
url: "http://localhost/codeigniter_testing/index.php/welcome/ajax_process",
data: {email : email, password : password},
success:function(data){
echo data;
}
});
});
});
my base url:
$config['base_url'] = 'http://localhost/codeigniter_testing/';
In your code their are code error
success:function(data){
echo data;
}
you have add alert instead of echo.
EX:
success:function(data){
alert(data.email);
}
Try it....
<script type="text/javascript">
$(document).ready(function(){
$('#sbtn').click(function(){
var email = $('#email').val();
var password = $('#password').val();
var datas = $('#myform').serialize();
$.ajax({
type: "POST",
url: "http://localhost/codeigniter_testing/index.php/welcome/ajax_process",
data: {email : email, password : password},
success:function(data){
var result=eval(data);
alert(result.email);
}
});
});
});
</script>
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);*/
I can't seem to get my voting system to work in ajax. I'm trying to establish the 'upvote' and have my onclick function call my route and insert a vote accordingly, except nothing happens. I can't see why or where I'm going wrong.
JAVASCRIPT
$( document ).ready(function() {
$(".vote").click(function() {
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if (name=='up')
{
alert(dataString);
$(this).fadeIn(200).html('<img src="/img/vote-up-on.png" />');
$.ajax({
type: "POST",
url: "http://domain.com/knowledgebase/upvote",
dataType: "json",
data: {id : id},
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
if (name=='down')
{
alert(dataString);
$(this).fadeIn(200).html('<img src="/img/vote-down-on.png" />');
$.ajax({
type: "POST",
url: "downvote",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
return false;
});
});
</script>
ROUTE.PHP
Route::get('knowledgebase/upvote/{id}', 'PostController#upvote');
POSTCONTROLLER.PHP
public function upvote($id)
{
if (Auth::user()) {
if (Request::ajax()) {
$vote = "1";
$user = Auth::user()->id;
$post = Post::find($id);
$checkvotes = Vote::where('post_id', $post->id)
->where('user_id', $user)
->first();
if (empty($checkvotes))
{
$entry = new Vote;
$entry->user_id = $user;
$entry->post_id = $post->id;
$entry->vote ="1";
$entry->save();
}
}
}
else
{
return "Not an AJAX request.";
}
}
You are using post in your jquery, but you are waiting for a GET route.
Use...
Route::post('knowledgebase/upvote/{id}', 'PostController#upvote');
Additionally, the way you are handling your route may not work correctly with the id. It would be expecting the id in the URL so what you can do is append your id to the url when setting up the ajax.
url: "http://domain.com/knowledgebase/upvote/"+id,
Or not use it at all, take the {id} portion out of your route, and grab it using Input::get('id');
I am working with MVC3. I have a javascript function which makes an ajax call to mvc3 controller action. When i send null in the data, it calls the controller action. but when I try to send location in data it gives javascript error i.e. 'this is not defined'.
function getPictureContent(location)
{
var pictures = getLocationPictures(location);
var content = "<div id=markerpictures></div>";
return content;
}
function getLocationPictures(location) {
var pics;
$.ajax({
type : "POST",
url : "/Home/GetLocationPictures",
data : {'location' : location},
contentType : "application/json; charset=utf-8",
dataType : 'json',
async : false,
success : function (data) {
pics = data;
}
});
return pics;
}
Here is the controller action:
public JsonResult GetLocationPictures(string location)
{
List<string> pictures = new List<string>();
return Json(pictures);
}
Try this.. Location should be string.Be sure that will string.
function getLocationPictures(location) {
var pics;
$.ajax({
type : "POST",
url : "/Home/GetLocationPictures",
data : JSON.stringify({'location' : location}),
contentType : "application/json; charset=utf-8",
dataType : 'json',
async : false,
success : function (data) {
pics = data;
}
});