Laravel using an ajax call - php

In my blade I use an ajax call to call a route. In my controller I am returning a different view. However, the view does not change. I am wondering if I need to change the view differently because I am using an ajax call that needs a return of success or not.
Here is my ajax code in my blade:
$('#btnAnalyze').click(function(){
var text = $('#cv').val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
url: "/widget/measurableresults",
data: {
text: text,
},
success: function (msg) {
console.log("Success");
},
error: function (msg) {
console.log(msg);
}
});
});
Here is my route
Route::post('/widget/measurableresults', 'WidgetController#measurableresults');
Here the method in my controller:
public function measurableresults()
{
$text = Input::get('text');
Log::debug( $text );
return view('results.measurableresults');
}
The Log::debug prints out the value of $text and the ajax call returns success. However, the view does not change to results.measurableresults. What am I missing?

Try and remove the line Log::debug( $text );, it's probably blocking the return statement to execute
update
You seem not to understand how things work in ajax and php communication, the success code you're receiving doesn't mean ajax returned the expected value, if you log the ajax response, you'll get the html text in the view you're returning, it's not that ajax will magically change the view for you.
Please make more research in regards to this.

Related

My ajax delete is not passing data to the controller. Codeigniter

CODEIGNITER
I'm trying to delete an instance in the db using an ajax delete request. I'm trying to pass data with the id of the instance I want to delete.
In my javascript when i use 'type:POST' in my ajax call the code works correctly and the data is deleted but when i use type:DELETEit doesn't work.
JAVASCRIPT AJAX CALL
$(".complete").click(function(){
var title = this.id;
//console.log(title);
$.ajax({
url: '/ajax-requestDel',
type: 'DELETE', <!-- when i use type:'POST" the code works
data: {title: title},
error: function() {
alert('Something is wrong');
},
success: function(data) {
console.log(data);
}
});
});
CONTROLLER CODE
public function ajaxRequestDelete(){
$data = $this->input->post('title');
return $this->db->delete('todo', array('title'=>$data));
}
When debugging i have found that the variable $data is empty but when using a post call it has the desired value.
How do i repeat the same behavior using delete. Thanks.
The data will arrive at the php input stream php://input for a DELETE request.
You should use $this->input->input_stream('title'); as per the manual
See also: get a PHP delete variable

Method not allowed. Must be one of the POST slim 3

This is my route
$app->post('/place_c', 'place_c_controller:place_c',
function ($request, $response, $db_connect) {
return $response;
})->setName('place_c');
JavaScript Code
$("#frm_place_c").ajaxForm({
url: "http://localhost/pub/place_c",
dataType: "text",
beforeSubmit: _Request,
success: _Response,
});
function _Request(formData, jqForm, options) {
$(".loader").show();
return true;
}
function _Response(responseText) {
$(".loader").hide();
}
And I am posting data to the controller /place_c through ajax but i am getting 500 internal server error "Method not allowed. Must be one of: POST" However using $app->get works perfectly fine. What i am doing wrong?
The server response data was different than what was expected.
I answered similar question here
Basically, your route specified a POST request. Therefore, in your AJAX request, you might want to add a method attribute to the object, like so:
$("#frm_place_c").ajaxForm({
url: "http://localhost/pub/place_c",
dataType: "text",
beforeSubmit: _Request,
success: _Response,
method: "POST"
});

Jquery Javascript Variable

Hello i have searched the whole website for a soltution found something but didnt get it to work.
-I have the main function on the head area of the page. (This will return Data from a PHP)
-This Data i need as a variable but dont know how to handle.
function note(content,usern){
note = Function("");
$.ajax({
type: "POST",
url: "note.php",
data: {'token': content, 'user': usern }, success: function(data){ var myneededvar = data; }, }); }
Ok thats works and also data is given out
Now im calling the function in the script like this
note(token,notename);
and i need the result myneededvar
but cant get it to work.
Firstly, your variable myneededvar is local to the success handler function and will not be available outside.
Secondly, your AJAX call is asynchronous and you cannot expect to immediately get the AJAX return data in a variable right after the AJAX call statement.
i.e., you cannot do:
note(...); // call your method
alert(myneededvar); // this won't work as the AJAX call wouldn't have completed
Thirdly, not sure why you have that note = Function(""); statement there. You should remove that.
Something like this should work:
var myneededvar;
function note(content, usern){
$.ajax({
type: "POST",
url: "note.php",
data: {'token': content, 'user': usern },
success: function(data){
myneededvar = data;
// use it here or call a method that uses myneededvar
}
});
}

Call PHP program with Ajax

I have a PHP program for counting user banner clicks. My banner link is something like this:
<a href="<?=$banner_url;?>" onclick="banner_click_count('<?=$banner_id;?>')"><img src=...>
When user clicks on image, it runs banner_click_count() function with $banner_id as parameter.
function banner_click_count($ban_id)
{
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {banner_id: $ban_id}
});
}
At banner_click.php, I get the banner_id with $banner_id = $_GET['banner_id']);, search the database based on it. Find the record, then add 1 to banner_count column field. After that, redirect to banner_url.
When I run the program, I get Parse error: parse error, expecting T_VARIABLE' or '$'' on line $.ajax({
Addendum: the error is cleared with all your help, but when I click on the link it redirects to banner_url directly and does not run the AJAX function.
Addendum:I put the alert("hello"); at the top of ajax function and i got it. So it goes into function
1.You need to put your javascript function under <script> tag
2.you need to pass json string as post data
3.though you are passing your data as post so you will get this data in php as $_POST not $_GET
So change your function as below
<script>
function banner_click_count(ban_id)
{
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {banner_id: ban_id}
});
}
</script>
// in your php use as below
echo $_POST['banner_id']
Make sure banner_id is in quotes and that you are including JQuery in your page.
And don't forget a success/error return.
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {'banner_id': $ban_id},
success: function(s) {
console.log('success' + s);
},
error: function(e) {
console.log('error' + e);
}
});
Don't we need a return false before the function ends?
I found the solution. Thanks to all.
function banner_click_count(ban_id)
{
$.post(
"banner_click.php",
{
banner_id: ban_id
});
}

sending data through JSON(AJAX function) but not able to access the controller of respective page

This is my JavaScript function which I have included in HEAD tag:
function test()
{
//enter code here
$.ajax({
url: 'test?msgto='+document.getElementById('Select1').value,
dataType: 'json',
type : 'GET',
success: function(result)
{
// console.log(result);
alert('test1');
// alert(string(result[0]['Select1']) );
// alert(result[0]['TextArea1']);
//document.getElementById('Text1').Value = ;// string(result[0]['location']);
}
});
}
and I want to send data to my PHP controller using this function on Cilck event of a button. I have written the following code for GETACTION()
// TODO Auto-generated method stub
//echo('Get');
$msgfrom = 1; //$this->_getparam('usrid');
$msgto = $this->_getparam('msgto');
$sql = "call select_messages(".$msgfrom.",".$msgto.");";
$data = Zend_Db_Table::getDefaultAdapter()->query($sql)->fetchAll();
$this->_helper->json($data);
But while on clicking I am not getting any output or result....
Kindly Guide me as soon as possible..... :)
Several misuses there.
Firstly, are you sure GETACTION() retrieves the /test path?
Secondly, in your JS code, I'm not sure you are calling the right path.
You'd better use this :
$.ajax({
type: 'GET',
url: '/test', // Notice the '/' at the beginning
data: {
msgto: $('#Select1').val(), // Since you're using jQuery, do it all the way :)
}
success: function(result) {
alert(result);
}
});
Notice the slash at the beginning of the url parameter. It means: "starting after the domain name". Also, you don't need the use the verbose document.getElementById() when you're using jQuery :).
Lastly, see the data parameter. jQuery will automatically build the correct URL before sending the AJAX request. I think it keeps your code cleaner.

Categories