I am fairly new in the Ajax's world, tho I've tried it maybe twice and it always worked like a charm. Now I am trying to send variable with ajax() method but it seems like I have 0 errors in my console but I think the problem is that I am sending no variable at all.. If, in my php file, I echo a string it's working. So my problem is that I can't echo out the variable. I am on Laravel 5, this is why you will see Request::get('my_input_name').
Here is my js code :
$('.select_blocs_check').click(function() {
var blocID = $(this).val();
$.ajax({
type: "POST",
url: "http://localhost/send/getBlocHtml/",
data: {id: blocID},
success: function(html) {
$('#preview-wrap').html(html);
}
});
});
This is my php file
public function getBlocHtml()
{
$bloc_id = Request::get('id');
echo $bloc_id;
}
So, if I change my php file like this
public function getBlocHtml()
{
$bloc_id = Request::all();
print_r($bloc_id);
}
Now, it will print out : array(). Like if I have nothing in my data.. What's wrong with my data parameter in $.ajax ?
I see Laravel 5 in your question. Try Request::input('id'); Which will pull both Post and get.
Change how you pass your data. Try this :
$.ajax({
type: "POST",
url: "http://localhost/send/getBlocHtml/",
data: {'id': blocID},
success: function(html) {
$('#preview-wrap').html(html);
}
});
try change Request::get('value') to Input::get('value')
API says
This method is used for all request verbs (GET, POST, PUT, and DELETE)
In Laravel 5 API is Input static method.
And article which may help to you.
Related
I am trying to understand a $.ajax call:
var url = "/api/followuser.php/" + userID ;
$.ajax(url, { 'success': function(data) {
/do something
}
});
Thia ajax call is required to pass the variable 'userID' to the file '/api/followuser.php' to make a database query(php/Mysql).
I don't have access to '/api/followuser.php' .
Can anyone help me figure out how to get the variable 'userID' from the URL in a php file to be used in a database query.( I know how to pass variable as 'data: userID,' in $.ajax and use it in a php file but i want to understand this particular example)
Maybe you mean followuser.php?user_id= instead? The slash is probably causing issues since that's interpreted as a directory by the server:
var url = "/api/followuser.php?user_id=" + userID;
you need to use GET method with ajax, to do this you can use next example
$.ajax({
url: "/api/followuser.php",
type: "GET",
data: {variable: "valueofvariable"},
success: function(data) {
console.log(data);
}
});
so in your php file you can read the variable like this
<?php
if(isset($_GET["variable"])){
echo $_GET["variable"];
// if this works you should see in console 'valueofvariable'
}
?>
I am making a forum webpage where I'll put delete this buttons under each comment. Now when you press this button, I send the ID of the comment to PHP file using ajax which looks like this:
function Deletethis(index) {
var result = confirm("Want to delete?");
if (result==true) {
$.ajax({
url: "deletepost.php",
type: "POST",
data: index,
success: function(){
location.reload();
}
});
} else return false;
}
Now the problem is I can't receive it from the PHP end. I've used the debugger and saw that the index value is right. My PHP looks like this:
<?php
$con = mysqli_connect("localhost", "root", "123", "test") or die("DIE");
if (isset($_POST['index'])) {
$SoonToBeDeletedComment = $_POST['index'];
};
$index = intval($SoonToBeDeletedComment);
mysqli_query($con, "DELETE FROM commentsbox WHERE commentsbox.`ID` = $index");
echo "Post Deleted...";
mysqli_close($con);
?>
My code doesnt give any errors but it doesn't delete the post either. When I do the same process manually on navicat, it is working so I thought maybe the index is a string and it should be an integer. So I used intval but it didnt solve the problem either. Any ideas to help me improve my code is appreciated. Thanks in advance
In jQuery's .ajax call, the data property needs to be an object, not a string (string only it it's a full GET query string, actually, but it's not what you need here).
So, try this:
$.ajax({
url: "deletepost.php",
type: "POST",
data: {id: index},
success: function(response){
alert(response);
// location.reload();
}
});
And then in PHP, get it as:
$_POST['id']
UPDATE
Sanity checklist:
is your Deletethis function actually receiving the index?
is your ajax calling the right script?
is ajax data property an object, like I explained above?
what is the output of the PHP script?
what are the contents of $_POST?
is the id inside the $_POST array ($_POST['id'])?
does the row with that id exist in the db?
These questions should help you pinpoint the problem more accurately.
You send param to your PHP code without define his name : data: { index: index } // param : value
function Deletethis(index)
{
if ( confirm("Want to delete?") )
{
$.ajax({
url: "deletepost.php",
type: "POST",
data: {
index: index
},
success: function(){
window.location.reload();
}
});
}
}
Check also if your PHP code is working by calling page directly with param.
In the process of learning Laravel 4, so please pardon me if my question sounds silly.
I am trying to use $.ajax to post form data to the controller.
Upon checking the controller, Input::all() is not populated with data at all.
Have checked that the form is already serialized in the jquery codes.
So now I am here seeking help to see if anyone know how to resolve this issue.
Below is what I have coded so far.
Jquery Code
<script>
$(document).ready(function(){
var BASE = "<?php echo Request::root(); ?>/signup/add";
$('#password').on('blur', function() {
var info = $('.info');
$.ajax({
url:BASE,
method:'post',
contentType:'application/json; charset=utf-8',
cache:false,
dataType:'json',
data: $("#membersignup").serialize(),
success:function(data){
console.log(data);
},
error:function(xhr,status,error){
//errors here
}
});
});
});
</script>
route.php
Route::post('signup/add', array('uses'=>'MemberController#store'));
MemberController.php
public function store()
{
return Response::json(Input::all());
}
Instead of creating the url using this:
var BASE = "<?php echo Request::root(); ?>/signup/add";
Use a named route like this:
Route::post('signup/add', array('uses' => 'MemberController#store', 'as' => 'signup.add'));
So, it'll be easier for you to generate the url for your ajax request, using something like this:
var BASE = "<?php echo route('signup.add'); ?>
This may not solve your problem if you are still generating the right url but this will generate the right url for sure. I doubt that, you are not sending the request to the right url but not sure.
Since there is still no accepted answer, I have just had a similar problem and it was fixed by removing the dataType option and allowing it to select automatically.
Set your contentType like this...
$.ajax({
url:BASE,
method:'post',
contentType: 'application/x-www-form-urlencoded',
cache:false,
dataType:'json',
data: $("#membersignup").serialize(),
success:function(data){
console.log(data);
},
error:function(xhr,status,error){
//errors here
}
});
My guess is that the $.ajax is not working for jquery-1.11.0.min.js.
Changed to $.post and you will have Input::all() working.
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
}
});
}
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.