Can't access the values posted by jQuery Ajax - php

I have a an script that sends a post to my php file:
$('.target').change(function() {
$.ajax({
type: "POST",
url: "/form-actions.php",
data: {rechten : '0'},
cache: false,
success: function(){
alert("Submitted");
}
});
});
When i use firebug i can see the post being send: Parameter: rechten 0
But my form-actions.php (which is in the right location) can't see the post when i use
<?php print_r($_POST); ?>
The outcome of this is Array ( )
What am i doing wrong?
Thank you for your time!

(I'm adding this as an answer rather than a comment because of the code)
Are you super-sure your php file is in the right place?
Change your JS to
$('.target').change(function() {
$.ajax({
type: "POST",
url: "/form-actions.php",
data: {rechten : '0'},
cache: false,
success: function(data){
alert(data);
}
});
});
And see what is alerted.
This is assuming that form-actions.php contains just
<?php
print_r($_POST);
?>
and nothing else (or you'll see that too).

Related

submit forum (post) delete my ajax get

I am using ajax for sending to the server that a checkbox is checked and now other inputs fields need to be required:
EDIT: ok i will change the question to be more clarified.
i have this code:
$(document).ready(function() {
$('#button_ajax').click(function() {
var request = jQuery.ajax({
type: 'get',
url: 'http://orenmizrah.git/nir1.php',
data: {'id1': $('#input_text').val()},
async: true,
success: function(){
console.log("sent");
}
});
});
and now i check that i got the info in $_GET['id1'] with php code and if $_GET['id1'] is set, echo something to the browser.
the problem is that $_GET['id1'] is set but there is no output to the browser.
what i should do?
No output is shown in the browser because nothing is done to show it.
$(document).ready(function() {
$('#button_ajax').click(function() {
var request = jQuery.ajax({
type: 'get',
url: 'http://orenmizrah.git/nir1.php',
data: {'id1': $('#input_text').val()},
async: true,
success: function(data){
console.log(data); // outputs the returned data to the console
}
});
});

Get Ajax URL from PHP script

I know this has been asked before, but I can't get it working.
I'm loading a file on a page whose path and name are a variable contained in a PHP script.
My script.php is outputing a variable $filename which contains the path to a file that has to be open in an ajax request.
So, the file can be for example:
'../path/to/file/filea.json' or
'../another/path/fileb.json'
I tried this in my jQuery:
$.ajax({
url:'script.php',
success:$.ajax({
url: ??? // This ($filename) is what I'm trying to get from the 1st Ajax call
sucess: function(data){
//other code here
}
})
);
I know $filename is wrong in the second Ajax call but how do I get the value of that variable?
$.ajax({
url: "script.php",
success: function(data){
$.ajax({
var someParam=data; //response Data from 1st Ajax Call
type: "post",
url: "example.php",
data: 'page='+someParam,
success: function(data){
//Do More Here!
});
});
Try this:
inside script.php
echo json_encode($filename); //somewhere you need to have this echo.
jQuery
$.ajax({
url: 'script.php',
success: function (data) {
alert(data); //or console.log() and doublecheck you have the right info
$.ajax({
url: JSON.parse(data),
success : function (data) {
//other code here
}
})
}
}); // added a extra } to close the ajax
This solved the problem:
$.ajax({
async:false,
url: 'script.php',
success: function(data){
$.ajax({
url: data,
success: //code here
})
})
#Sergio: script.php wasn't echoeing the output of $filename... Thanks for reminding!

jQuery: $.ajax() won't fire or no response from PHP

I've just started learning jQuery and PHP, and I encountered a problem when I try to use Ajax. Either the $.ajax() function won't fire, or PHP won't return anything, I cannot tell. I must have forgotten something really stupid, I guess...
Here's the code. There's no reply, no alert, nothing.
js:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
done: function(response) {
alert("response");
}
});
});
</script>
PHP:
<?php echo "Something"; ?>
Thanks in advance.
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
done: function(response) {
alert("response");
}
});
supposed to be
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
}).done(function(response) {
alert("response");
});
success , error methods are generally declared in the place where you have written done which are now deprecated
You have your done in the wrong place.
Try this instead:
$.ajax({
url: "get_profile.php",
type: "GET",
data: {}
})
.done(function(response) {
alert("response");
});
You can have alternative option for check is there any error in your ajax call. and you can also do some stuff before getting the response of your ajax call like loading image shows to end users until the response result. for this you can use following code:
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
beforeSend:function(){
//do something like loading image
},
success:function(response){
alert(response);
},
error:function(e){
alert("something wrong"+e);
}
})

AJAX call to return values from PHP not working

I have a AJAX script which sends value to PHP script and to retrieve the value from the PHP script. The part where the script sends the value is working fine. Its the problem with the retrieving values. I am not able to figure out what is wrong.
AJAX code:
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
data: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
PHP code:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Please check whether "name" is posted before assigning the value to $userAnswer.
Both ajax scripts are sending to "ajax.php". In first ajax request "name" is posted, but in 2nd ajax request "name" is not posted.
To see warnings and errors, enable error reporting in php.
<?php
//To enable error reporting
ini_set('display_errors',true);
error_reporting(E_ALL);
data: {name: 145}
try this hope this will work.
Your nested AJAX call does not have the request type specified. The default is GET but your ajax.php is trying to find a POST.
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php',
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
type: "POST", //<-- added here
data: {name:data}, //<-- also required for POST
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Set type:'POST' inside the second ajax call and try to use data1[0]. Also remember that you are sending a json string (that comes from the first ajax) with the second request. Basically you are encoding an encoded value,so when you receive the post value you should json_decode the post value

How can I send a jquery var to php?

When I try to send a var with jquery, ajax php doesn't receive it, and I don't know why.
This is my script:
function showHiddenUserInfoPanel(id){
$(document).ready(function(){
$('#admin-resume').fadeToggle('slow', 'linear', function(){
$.ajax({
async: true,
type: "POST",
dataType: "html",
contentType: "application/x-www-form-urlencoded",
data: "userid="+id, // Also I tried with : data: {userid: id},
url: "user-profile.php",
success: function(){
$('#info-user').fadeToggle('slow', 'linear');
alert(id); // I receive the correct id value
}
});
});
});
And this is my user-profile.php:
<div id="activity_stats">
<h3>Viendo el perfil de <?php echo $_POST["userid"]; ?></h3>
</div>
Can anybody can help me?
Thanks :)
Is it just me or you are not using the response at all?
$.ajax({
success: function(){
$('#info-user').fadeToggle('slow', 'linear');
}
});
Your anonymous success function is ignoring all responses. I suspect your #info-user should be an empty element and you are not getting a visual feedback.
An example could be:
$.ajax({
success: function(response){
$('#info-user').html(response).fadeToggle('slow', 'linear');
}
});
Other alternative could be using .load().

Categories