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'];
?>
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 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.
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.
I am trying to get the hang of php and ajax post. I am posting simple user input via text box to a php page. The php page takes the user input, counts words and sends it back as response. However it can't receive the data, comes back saying. Why is this how do I fix it?
This is my jQuery ajax post code
var dataString = name;
$.ajax({
type: "POST",
url: "test_get.php",
data: dataString,
success: function(response) {
alert(response);
}
});
return false;
});
});
This is my PHP code
// if data are received via POST, with index of 'dataString'
if (isset($_POST['dataString'])) {
$str = $_POST['dataString']; // get data
echo "The string: '<i>".$str."</i>' contains ". strlen($str). ' characters and '. str_word_count($str, 0). ' words.';
}
else echo 'There is no data!';
Your data argument is incorrect.
var dataString = name;
$.ajax({
type: "POST",
url: "test_get.php",
data: {"dataString" : dataString },
success: function(response) {
alert(response);
}
});
Read the jQuery $.ajax docs for more info.
You probably need to change:
data: dataString,
To
data: "dataString="+dataString,
Because a HTTP POST still runs off of key/value pairs.
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