get value - ajax' - php

I am sending this variable to other page, to make a validation.
However print_r($_POST); only show Array ().
Normally we use serialize, is sent the name attribute, but in this case only is sent the variable with the text. So something like $form = $_POST['data']; only works to
in firebug:
data="something"
but in my case i just send
something
code
<?php $page = "someText"; ?>
JS
default:
$("#msg").fadeTo(200, 0.1, function() {
$(this).html('success').fadeTo(900, 1);
$.ajax({
url: "page_validation.php",
type: "post",
dataType: "json",
data:'<?php echo $page; ?>',
success: function(data) {
$('#one').load('page.php');
}
});
});
break;

try it like
$page = "someText";
$.ajax({
url: "page_validation.php",
type: "post",
dataType: "json",
data:"postedvariable=<?php echo $page; ?>",
success: function(data) {
$('#one').load('page.php');
}
});
and acces the varible in page_validation.php as $_POST['postedvariable']
also another option will be to use
$.post("page_validation.php",
{postedvariable:'<?php echo $page; ?>'},
function(data) {
$('#one').load('page.php');
});
whic looks even simpler than the $.ajax

Related

php jquery dynamic dropdown

I am having problem sending/reading ajax variable.
Ajax code is as below:
$.ajax({
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
I have tried to read it in another php file like this:
$InvoiceType = $_REQUEST['it'];
//$InvoiceType = $_POST['it'];
//$InvoiceType = $_GET['it'];
But none of above works. The variable $InvoiceType always stays empty.
What is the problem?
The best way is to use POST method like this :
$.ajax({
method: "POST",
url: ""/wp-content/themes/canvas-child/get-clients-dropdown.php",
data: { it: 1, param2: "value2" }
})
You can get your value in $_POST['it']
Please try it with full url of file with get_template_directory_uri().
$.ajax({
type: 'GET',
url: '<?php echo get_template_directory_uri(); ?>/get-clients-dropdown.php',
data: {it: 1},
success: function(data) {
$("#ClientID").html(data);
}
});
$.ajax({
type: "GET",
data: {it: "1"},
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
try this
You have to use it like this:
$.ajax({
type: "POST",
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
Then in PHP File use this:
$InvoiceType = $_POST['it'];

Get the content of json file on page load

I am using ajax, php and json to store click counts in json file...Let's say the file is this:
count.json
{"countbtn1":28,"countbtn2":25,"countbtn3":39}
downloadsCount.php
<?php
$buttonName=$_POST["buttonName"];
$file = "count.json";
$json = json_decode(file_get_contents($file), true);
echo $json['count'.$buttonName] ;
?>
downloads.php
<?php
$buttonName=$_POST["buttonName"];
$file = "downloads.json";
$json = json_decode(file_get_contents($file), true);
$json['count'.$buttonName] = $json['count'.$buttonName] + 1;
file_put_contents($file, json_encode($json));
echo 'success';
?>
And I need to place each btn value inside these on page load:
<div class='smallbtn1'>0</div>
<div class='smallbtn2'>0</div>
<div class='smallbtn3'>0</div>
Counting and updating the json file with this:
$('.download').click(function() {
//get name of button
var name= $(this).prop('name');
$.ajax({
url: "downloads.php",
data:{buttonName:name},
method: "POST",
success: function(response) {
if (response = 'success') {
//get count download
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response);
}
});
}
}
});
return true;
});
..and I tried updating the count on page load with this:
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response); }
});
});
...but it doesn't update the values on load, just after the first click.
PS: The whole thing IS wrapped in $(document).ready(function(){..});
After seeing the code on your server, I'd say the issue is how you are calling the AJAX. You are doing this:
$(document).ready(function(){
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response); }
});
});
But you don't have defined what name is (it is defined on the click event, but not when the page is loaded for the first time). You should do something like this:
$(".download").each(function() {
var name = $(this).attr("name");
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name },
method: "POST",
success: function(response){
$('.small'+name).html(response);
}
});
});
So the AJAX is called for each one of the buttons, and you use the attribute name by doing $(this).attr("name").
Try sending the AJAX request after the page completely loads using $(document).ready():
$(document).ready(function() {
$.ajax({
url: "downloadsCount.php",
data: { buttonName:name },
async: false,
method: "POST",
success: function(response) {
$('.small' + name).html(response);
}
});
});

ajax call php code in a file and get result

Some code I want to call from ajax is in a separate file.php:
<?php
session_start();
$email1 = $_POST['email1'];
//some code here processing $email1
$response = 'some text';
?>
This is how I call it from ajax:
$.ajax({ url: 'file.php',
data: {email1: $("#user_email").val()},
type: 'post'
});
I'd like to be able to do something like this after the call to file.php:
alert($response);
How do I do that?
In your PHP you have to echo the $response, and in your JS you have to specify the callback function like so:
$.ajax({
url: 'file.php',
data: {
email1: $("#user_email").val()
},
type: 'post',
success: function(data) {
alert(data);
}
});
Inside the ajax call, include a success.. ex:
success: function(data) {
alert(data);
},
This will pop an alert up with your response.
Try:
$.ajax({ url: 'file.php',
data: {email1: $("#user_email").val()},
type: 'post',
success: function(data) {
alert(data);
}
});
Check out the documentation
You also need to echo out the response in your PHP file:
echo $response;
Something like this?
$.ajax({
type: "POST",
url: "file.php",
data: {email1: $("#user_email").val()},
success: function(data) {
alert(data);
}
});

Ajax PHP Jquery - echo-ing back data

I'm having trouble bringing back data from my PHP file. I guess I don't really understand the data parameter of this jquery function so I just went off some tutorials.
Jquery
$.ajax(
{
url: 'test.php',
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
Now from my understanding the test: declares the variable used in php and 1 is the value in that variable. But I'm not entirely sure...
Here's my PHP
$item1 = $_POST['test'];
echo $item1;
Now it's just supposed to alert that value so I know it is at least returning something but in the alert it's just blank so I'm losing the value somewhere, but where?
use $_REQUEST it will handle both the GET and POST
$item1 = $_REQUEST['test'];
by default ajax request is GET type, either specify expilicitly the type like
$.ajax(
{
url: 'test.php',
type:'POST'
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
or use $_GET like
item1 = $_GET['test'];
echo $item1;
The correct way:
<?php
$change = array('key1' => 'blabla', 'key2' => '12432rr424234');
echo json_encode($change);
?>
Then the jquery script:
<script>
$.get("location.php", function(data){
var mydata= $.parseJSON(data);
var art1 = mydata.key1; // <----------- access the element
});
</script>
This is work for me
ajax code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<title>Test Page</title>
<script>
$.ajax(
{
url: 'test.php',
type: 'POST',
dataType: 'text',
data: {latitude: '7.15588546', longitude: '81.5659984458'},
success: function (response)
{
alert(response);
}
});
</script>
</head>
<body>
</body>
</html>
php code (test.php)
<?php
$lat = $_REQUEST['latitude'];
$lon = $_REQUEST['longitude'];
echo 'latitude- '.$lat . ', longitude- ' . $lon;
?>
add POST method:
$.ajax(
{
url: 'test.php',
dataType: 'text',
type: 'post',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
you forgot to put the method POST/GET by which you are sending the data to your php file.
$.ajax(
{
type:'POST',
url: 'test.php',
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
just add "type" POST or GET
//example
$.ajax(
{
url: 'test.php',
type: 'POST',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
If you are trying to get information from the php file, I don't think the data field is required. Here is what I have.
$.ajax(
{
url: 'response.php',
type: 'get',
dataType:'text',
success: function(data){
window.alert(data);
}
}
and as far as the php goes..
<?php
echo "1";
echo "2";
echo "3";
echo "4";
echo "5";
echo "6";
echo "7";
echo "8";
?>
Alert box on execution
Been struggling on this for a while, my issue wasn't on the js but in php script : error reporting was set to E_ALL - which apparently blocks it from returning data to the AJAX call. My guess is it prints notices before the final echo and blocks it all. Weird thing is it didn't print me any notice in the logs i set up to get to the bottom of this.
Changed it to E_ERROR and now works like a charm.

pass PHP variable value to jquery function

This code is not working properly. What i want is just send the variable $something to the page.php
What is the correct way to do this ? : data: <? php $something; ?>,
script
$something = "text";
$.ajax({
url: "page.php",
type: "post",
dataType: "html",
data: <? php $something; ?>,
success: function (data) {
$('#total').load('xxx.php');
}
});
myFile.php:
<?php $something = 'text'; ?>
<script>
$.ajax({
url: "page.php",
type: "post",
dataType: "html",
data: '<?php echo $something; ?>',
success: function (data) {
$('#total').load('xxx.php');
}
});
</script>
First of all, I think you have mistakenly mixed PHP and JavaScript. In your code the line:
$something = "text";
may be understood in two ways. If this is the whole code you have, then you are actually initializing JavaScript variable called $something. Later in the code you are trying to use the value of PHP variable called $something.
What you need to do is to change the code into (assuming you want to pass variable from PHP):
<?php $something = "text"; ?>
$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: '<? php $something; ?>',
success: function (data) {
$('#total').load('xxx.php');
}
});
or into (assuming you want JS variable):
var $something = 'text';
$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: $something,
success: function (data) {
$('#total').load('xxx.php');
}
});

Categories