im trying to post to php using jquery but the success function is returning an empty string.
jquery:
function post() {
value = posts[index].id;
console.log(value)
$.ajax({
url: 'post.php',
type: 'POST',
data: {value: 'value'},
success: function(result) {
console.log(result)
}
})
}
php:
if(isset($_POST)) {
var_dump($_POST);
}
Related
I want to get the value from $data->username but it returns NULL because input is not json. What am I doing wrong here?
Here is my current code.
JS:
$(function() {
$("#login-form").submit(function(e) {
e.preventDefault();
$.ajax({
url: 'http://penguin.linux.test/api/login.php',
type: 'post',
dataType: 'application/json',
data: $("#login-form").serialize(),
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
});
});
PHP:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents("php://input", true));
}
I am trying to post data to a php file through ajax call. But I could not get those data in my php file. It just returns the empty array when I check using var_dump($_POST). Anyone help me on this.
Here is my ajax script
<script>
$.ajax({
url: "update_user.php",
type: 'post',
data: {
"type": "active"
},
error: function (e) {
},
success: function(data) {
}
});
</script>
In update_user.php,
<?php
var_dump($_POST);
?>
It returns array(0) { }
I think type should be POST (uppercase not lower).
$.ajax({
url: "update_user.php",
type: "POST",
data: {
type: "active"
},
error: function (e) {
},
success: function(data) {
}
});
Here's my ajax call:
$("#yes, #no").click(function(){
var upOrDown = $(this).attr('id');
$.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
action: "updateVote",
data: {upOrDown: upOrDown},
dataType: "json",
success: function(data) {
console.log(data.output);
}
});
return false;
});
Here's the function in functions.php
function updateVote() {
echo json_encode(array(
'output' => 'hello!'
));
die();
}
add_action('wp_ajax_updateVote', 'updateVote');
add_action('wp_ajax_nopriv_updateVote', 'updateVote');
Why does this keep returning "0". It should return "hello!"
Thanks.
I am trying to pass an array through post to a php file. Here is my function in jQuery:
function callPHP() {
$.post("php/save.php", {
node: node
},
function (responde) {
console.log(responde);
});
}
And my save.php file has following content:
<?php
echo $_POST['node'];
?>
But I get an error that there's an undefined index 'node'. What is it that I am doing wrong?
try following
//define php info and make ajax call
$.ajax({
url: "php/save.php",
type: "POST",
data: { node: node },
cache: false,
success: function (response) {
}
});
Consider the below example. where info is an array
info = [];
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
data: {info:info},
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
.answer is the class of the element where you want to output your answer.
here is my code.
$('input[name="add-message-post"]').click(function() {
$.ajax({
type: 'POST',
url: 'addpost.php',
data: { message_name: $('input[name="new-message-name"]').val(), message_content: $('input[name="new-message-content"]').val() }
})
.done(function(data) {
$(data).hide().prependTo('#messages').fadeIn('slow');
});
});
if I do print_r($_POST) on addpost.php it only shows $_POST['message_name']
it should pass message_content over as well