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.
Related
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...
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'];
?>
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
I'm sending an ajax call to my PHP script as follows:
function load(){
var request = {};
request['action'] = 'load';
request['file'] = 'lorem_ipsum.txt';
$.ajax({
type: 'POST',
url: cgi_file,
data: JSON.stringify(request),
processData: false,
dataType: 'html',
contentType: 'application/html',
success:function(response){
console.log("received " + response);
}
});
}
and my PHP script is as follows:
$content_dir = '/static/content/';
$action = $_POST['action'];
switch ($action){
case 'load':
$file = $_POST['filename'];
echo file_get_contents($content_dir . $file);
exit();
}
The PHP is responding with the following failure:
Notice: Undefined index: action in /var/www/river/api.php on line 5
What's the issue here?
Try ditch processData: false and contentType: 'application/html' and it should work
$.ajax({
type: 'POST',
url: cgi_file,
data: request,
dataType: 'html',
success:function(response){
console.log("received " + response);
}
});
Just leave data as it is:
data: request,
You don't need to stringify it.
Also, your file parameter allows an attacker to read arbitrary files from your filesystem. Sanitize it.
A few things wrong here, firstly the contentType property is for the data you are sending to the server, secondly dataType should be set to text as that is what you are recieveing from the server. If you want to receive the data in the $_POST array your javascript should look like this,
$.ajax({
type: 'POST',
url: cgi_file,
data: {
action: "load",
file: "lorem_ipsum.txt";
},
dataType: 'text',
success:function(response){
console.log("received " + response);
}
});
Jquery will send your data as a standard post to your server side code.
I am sending to the server this call:
var Message = {};
Message['islands'] = '1-15-13';
Message['newMessageText'] = 'this is test message';
$.ajax({
url: "sendnote.php",
type: "POST",
data: Message,
dataType: "json",
contentType: "charset=utf-8",
success: function (data) {
alert(data["result"]);
},
error: function (data) {
alert(data["result"]);
}
});
and on server (sendnote.php) I have
print_r($_POST);
just to check do I receive anything, but after cchecking response in Firebug I see that this array is empty.
What am I doing wrong in sending data?
Thanks in advance!
PS I've checked previous post on this subject, but still have problems with it.
The problem is the contentType
Try this:
jQuery
$(document).ready(function(){
var Message = {};
Message['islands'] = "1-15-13";
Message['newMessageText'] = 'this is test message';
$.ajax({
url: "sendnote.php",
type: "POST",
data: Message,
dataType: "json",
success:function(data) {
alert("Islands: "+data.islands+", Message: "+data.newMessageText);
},
error:function(data) {
alert('error');
} });
});
php
<?php
echo json_encode($_POST);
?>
print_r($_POST) does not give a JSON response. do you even know what the actual reply of the request looks like when not using AJAX?
try echo json_encode($_POST); - this should print out a valid JSON.
or might i add that you might have forgotten PHP's <?php ?> opening and close tags