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

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

Related

Why does my AJAX script not send GET Parameters to PHP Script?

I have page1.php that uses AJAX to load another PHP page which creates a dynamic HTML table, based on the "name" parameter.
To get results from the table i need to send a URL parameter called "name" via a get request.
If the script didn't use ajax it should look like this:
table.php?name=test
I am trying to replicate this with
AJAX in page1.php:
<script>
function table() {
$.ajax({
url: "table.php",
type: 'get',
data: {
name:test ,
},
success: function(data)
$('.table').html(response);
}
});
}
table();
setInterval(table, 5000);
</script>
The table.php contains:
<?php
$name = $_GET['name'];
echo $name ;
?>
If i access table.php?name=test in the browser i can see the table, however AJAX is not passing the parameter, i have also tried POST.
The AJAX runs every 5 seconds to create the table, is this causing an issue with the request?
Note:
I have also tried to send just the url in ajax like:
url: "table.php?name=table",
This also does not load my table.
Edit:
The Ajax works fine without parameters, as it shows a default table with just table.php and no Url data.
How can i fix this?
This may help :)
function table() {
var test = 'test'; // or some other value, eg $('#test').val();
$.ajax(function () {
url: 'table.php',
method: 'GET',
data: { name: test },
success: function (response) { // note the argument is "response", not "data"
$('.table').html(response);
}
})
}

Laravel using an ajax call

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.

Put request in ajax

I have a route like this --
Route::put('avote', 'voteController#avote')->middleware('auth');
I want to access this route from a ajax send request.
When i use this code --
$data = {/* some data here */};
$.post("/avote", $data, function(result) {
$('#avote h2').html(result);
$('#avote a span').css('color', 'orange');
$('#avote a span').unwrap();
});
I get an error method not allowed. I know that it is the problem of method I used (used post not put)
I question is, is there any way i can get the information from /avote using ajax or any other scripts?
Please dont suggest me to change the route request from put to post or Any other way to protect the /avote route
I used Route::put() beacuse i have a database update function in the route controller
Move to $.ajax() function instead of $.post and provide method (type) property:
$.ajax({
url: "/avote",
data: $data,
method: "PUT",
// or type: "PUT", if your jquery version is prior to 1.9
success: function(result) {
$('#avote h2').html(result);
$('#avote a span').css('color', 'orange');
$('#avote a span').unwrap();
}
});

Ajax submit post data to a get url

Is it possible to mix post and get in ajax? Specifically have a form POST data to a url with get variables?
In html and PHP I would normally do this:
<form action="script.php?foo=bar" method="POST">
...insert inputs and buttons here...
</form>
And the PHP script would handle it based on logic/classes.
I have tried the following and several variations to no avail:
$(document).ready(function() {
$('#formSubmitButton').click(function() {
var data = $('#valueToBePassed').val();
$.ajax({
type: "POST",
//contentType: 'application/json',
url: "script.php?foo=bar",
data: data,
processData: false,
success: function(returnData) {
$('#content').html( returnData );
}
});
});
});
Is this even possible? If so what am I doing wrong. I do not feel as if I am trying to reinvent the wheel as it is already possible and used regularly (whether or not if it is recommended) by plenty of scripts (granted they are php based).
Please check out the below jsfiddle URL and
https://jsfiddle.net/cnhd4cjn/
url: "script.php?data="+data, //to get it in the URL as query param
data:"data="+data, // to get it in the payload data
Also check the network tab in dev tools to inspect the URL pattern on click of the submit button
You can, here's what I do.
$(document).ready(function() {
$('#formSubmitButton').click(function() {
// My GET variable that I will be passing to my URL
getVariable = $('#valueToBePassed').val();
// Making an example object to use in my POST and putting it into a JSON string
var obj = {
'testKey': 'someTestData'
}
postData = JSON.stringify(obj);
// Jquery's AJAX shorthand POST function, I'm just concatenating the GET variable to the URL
$.post('myurl.com?action=' + getVariable, postData, function(data) {
JSONparsedData = $.parseJSON(data);
nonparsedData = data;
});
});
});
I can see 1 syntax error in you code .
use
data: {data:data},
instead of
data: data,
and then try to access like
$_POST['data']; and $_GET['foo'];
But i have never tried the GET params inside a POST request :) , this is only a suggestion.

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
}
});
}

Categories