Unable to Fetch data using AJAX to another page Undefined index: userLoggedIn - php

I am Getting Following Error Undefined index: userLoggedIn in C:\xampp\htdocs\HamdardSocial\includes\handler\ajax_load_posts.php
AJAX Code to Fetch data from ajax_load_posts.php
<script>
var userLoggedIn = '<?php echo $userLoggedIn; ?>';
$(document).ready(function() {
$('#loading').show();
//Original ajax request for loading first posts
$.ajax({
url: "includes/handler/ajax_load_posts.php",
type: "POST",
data: "page=1&userLoggedIn=" + userLoggedIn,
cache:false,
success: function(data) {
$('#loading').hide();
$('.posts_area').html(data);
}
});
});
</script>`
I have also tried using POST and REQUEST method but no success
Code of ajax_load_posts.php
<?php
include ('../../config/config.php') ;
include ('../classes/User.php') ;
include ('../classes/Post.php') ;
$limit=10;
$posts = new Post($conn, $_POST['userLoggedIn']);
$posts->loadPostsFriends();
?>

To send data by jQuery's $.ajax is necessary to format the data field.
Example:
...
$.ajax({
url: "includes/handler/ajax_load_posts.php",
type: "POST",
data:{
page: "1",
userLoggedIn: userLoggedIn
},
...
More information can be found here.
But follow that #Alex Howansky said, it's important for the system security.
I hope it helps...

Related

I need to read an ajax post with PHP

I am attempting to update a database using ajax and PHP. Right now, i am just testing to see if my receiving PHP file is getting the POST data by echoing it, but all I get back is an 'undefined index' error.
Here's my sending code:
var content = $(".homeContent").html();
var dataString = "homepage|content|"+content;
//var
$.ajax(
{
type: "POST",
url: "Update.php",
data: "dataString="+dataString,
success: function (result) {
console.log(dataString);
}
});
and here is my receiving file (Update.php):
<?php
echo $_REQUEST['dataString'];
?>
Try this:
Ajax (JavaScript):
$.post( "Update.php", { dataString: "some String" } );
PHP:
<?php
echo $_POST['dataString'];
?>

how to use ajax response data in php

I am having problem with response data. I want to use response data in my php file. I want to assign them to a php variable.
here is the ajax code in insert.php file.
function change(){
var tp=$('#choose').val();
var country_val=$('#country_filter').val();
//alert(country_val);
//var country_val=$('#country_filter').val();
$.ajax({
type: "POST",
url: "filter.php",
data: { name: tp, country:"test"},
success:function( data ) {
alert(data);
}
});
}
here is php code in filter.php
if($_REQUEST["name"]){
$lvl = $_REQUEST["name"];
//global $lvl;
echo $lvl;
}
Now I want to use the response data to be assigned to a php variable in insert.php file. How may I do that?
Please help.
Send the data to insert.php file using ajax, instead of alerting it.
change success function to
$.ajax({
type: "POST",
url: "filter.php",
data: { name: tp, country:"test"},
success:function(response) {
var res = response;
$.ajax({
type: "POST",
url: "insert.php",
data: { res: res },
success:function(data){
alert(data);
}
});
});
In insert.php,
use this to get the variable.
$var = $_POST['res'];
If insert.php calls filter.php with ajax and then returns a value. you cannot apply that javascript var to a php var in the insert.php. That is because the Php script runs on the server when the page is loaded, and the javascript runs locally on the users computer. To apply anything to a php variable you will have to reload the page.

Unable to find ajax json data on the server side

I'm trying to send some data from jquery on the client side to a codeigniter controller. I have :
var data = {
"value" : value,
"message" : message
};
console.log(postData);
$.ajax({
type: "POST",
url: "my_controller/my_function",
data: data,
dataType:'json',
success: function(){
}
});
This appears to be working properly as I can see the correct post parameters in chrome dev tools. In my codeigniter controller I have tried:
echo 'post' . $_POST['value'].' '.$_POST['message'];
$postData=$this->input->post('value');
var_dump($postData); exit;
I'm getting:
Message: Undefined index: value
Message: Undefined index: message
boolean(false)
the $_POST array is empty.
How can I fix this? Thank you for your help
Based on your code I got this that works well:
js:
var data = {
value : '_value_',
message : '_message_'
};
$.ajax({
type: "POST",
url: "php.php",
data: data,
dataType:'json',
success: function(postData){
console.log(postData);
}
});
and in the php file php.php:
<?php
echo json_encode($_POST);
?>
And as a result I got this in the browser's console:
Object {value: "value", message: "message"}
You can simply follow the code
var data = {
value : value,
message : message
};
$.ajax({
type: "POST",
url: '<?php echo base_url(); ?>my_controller/my_function',
data: data,
dataType:'json',
success: function(responseFromServer){
console.log(responseFromServer);
}
});
In controller action method you can find those value just use this code
$postData= $this->input->post('value');
$message= $this->input->post('message');
Before return any thing just use these code
header('Content-Type: application/json', true);
echo json_encode($postData);
Now you find an array of Object in client side.

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

Using AJAX to pass variable to PHP and retrieve those using AJAX again

I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.
The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?
My code is as follows:
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:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
PHP CODE:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Use dataType:"json" for json data
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
Read Docs http://api.jquery.com/jQuery.ajax/
Also in PHP
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.
SO, I recommend using one AJAX call and get the value with success.
example: in first ajax call
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
alert(data);
//or if the data is JSON
var jdata = jQuery.parseJSON(data);
}
});
$(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);
}
});
}
});
});
});
Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call
In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.
Try this: var_dump($_REQUEST); and check if you're receiving the values.
you have to pass values with the single quotes
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: '145'}), //variables should be pass like this
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
try it it may work.......

Categories