I have this in my javascript function
var jsonData = $.ajax({
url: "pie_chart_community.php",
community_id: $c_id,
dataType: "json",
async: false
}).responseText;
I want to get the community_id in another PHP page. How can I get it?
Thanks in advance.
You could store the community_id variable in session data?
<?php session_start(); ?>
<?php $_SESSION['community_id'] = SOME_ID; ?>
Then to Access it:
<?php echo $_SESSION['community_id'];?>
Related
Is it Possible to include php function inside JS/Ajax? $('#rb').val(response.uid); uid is sql table and results are encrypted using custom php function can be decrypt using custom php function declared in php.
data sent from get.php => $result = $stmt->fetch(PDO::FETCH_ASSOC); echo json_encode($result);
Now Normally variables are decrypted as decrypt($row['uid']); how can decrypt val(response.uid)?
$.ajax({
type: 'POST',
url: 'get.php',
data: {id:id},
dataType: 'json',
success: function(response){
$('#rb').val(response.uid);
}
});
}
data sent from get.php => $result = $stmt->fetch(PDO::FETCH_ASSOC);
$result['uid'] = decrypt($result['uid']);
echo json_encode($result);
Thanks Chris G For the push and i solved it on my own.
This is the simplest way to fix this.
I have a script that copies an entire div into a variable. It works when I alert the data, but It wont work when I try to echo it in php.
<script>
var vin = "<?php echo trim($vin1); ?>";
function orderImage(){
var orderIm=$('<div/>').append($('#image-dropzone').clone()).html();
$.ajax({
type: 'POST',
url: 'orderImage.php?id='+vin,
data: {ordering:orderIm},
dataType: 'html'
})};
</script>
And my php:
<?php
echo $_GET['id'];
echo '<br />';
echo gettype($_POST['ordering']);
echo $_POST['ordering'];
?>
Output:
JS2YB417785105302
NULL
You can using full post request
<script>
var vin = "<?php echo trim($vin1); ?>";
function orderImage(){
var orderIm=$('<div/>').append($('#image-dropzone').clone()).html();
$.ajax({
type: 'POST',
url: 'orderImage.php,
data: {ordering:orderIm,id:vin}, // added id:vin on POST parameter
dataType: 'html'
})};
</script>
And my php :
<?php
echo $_POST['id']; // converted into $_POST
echo '<br />';
echo gettype($_POST['ordering']);
echo $_POST['ordering'];
?>
when i use the succes function :
var vin = "<?php echo trim($vin1); ?>";
function orderImage(){
var orderIm=$('<div/>').append($('#image-dropzone').clone()).html();
$.ajax({
type: 'POST',
url: 'orderImage.php',
data: {ordering:orderIm,id:vin},
dataType: 'html',
success: function(data) {
alert(data)}
})};
it shows the correct data. i guess it means that its solved. i just dont see it in the php file while i access it via the console log--> double clicking on XHR finished loading.
even tho the variables types shows NULL in the php script, i can still manipulate the content of them and everything is working fine !
I have a problem to pass value to my php script via Ajax, there is my code :
I send value to ajax via a href link :
After scan directory i get links of my files on href :
<?php echo $file; ?>
My js function to receive value :
function getfilename(content) {
alert(content); // i can see my value here
$.ajax({ //not work
type: 'POST',
url: 'script.php',
data:{song: content},
async: false,
success: function(response) {
alert(response); //nothing
});
}
My script.php
$album = $_POST['song'];
echo $album;
I dont understand why it does not work.
Thank you for your help!
Try changing
<?php echo $file; ?>
To this
<?php echo $file; ?>
Maybe your page is refreshing before the ajax data loads.
When you use the link element it will automatically go to the location in the href after it executes the onclick event. Leaving it empty will reload the page.
I would recommend you to add a "return false;" as the last instruction of the onclick.
<?php echo $file?>
Hope this helps.
Looking to your js code your success callback is missing a "}" in the end of function.
// $file = 'teste';
<?php echo $file?>
function getfilename(content) {
alert(content);
$.ajax({
type: 'POST',
url: 'script.php',
data:{song: content},
async: false,
success: function(response) {
alert('Response: ' + response); //Alerts Result
}
});
}
// Script.php
<?php
echo $_POST['song']
?>
i cant seem to get this to work, i'm trying to send a variable to php so it can write it to file but its just not working..
var jsonString = JSON.stringify(vars);
$.ajax({
type: "POST",
url: "woepanel.php",
data: {data : jsonString},
cache: false,
success: function(){
$('#sent').attr("bgcolor", "#00FF00");
$('#notsent').attr("bgcolor", "#FFFFFF");
}
});
it seems to be sending ok because the success works but php wont pick it up
<?php
$vars=json_decode($_POST['jsondata']);
?>
<?php
$fp = fopen('vars.txt', 'w');
fwrite($fp, $_POST["jsondata"]);
fclose($fp);
?>
Try this code:
<?php
$vars=json_decode($_POST['data']);
$string_data = serialize($vars);
file_put_contents('vars.txt', $string_data);
?>
you have this
data: {data : jsonString}
in your ajax call which means you should use
$_POST['data']
to extract the value
I have three files:
index.php
ajax.php
function.php
I want to pass a global variable from index.php to function.php via ajax.php.
So the alert message in index.php should be "2". But in fact, the result is "1" because function.php does not know the $global_variable.
Here is the code:
index.php
<?php
$global_variable = 1;
?>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$.ajax({
type: "POST",
url: 'ajax.php',
success: function(data) {
alert(data);
}
});
</script>
ajax.php
<?php
include 'function.php';
$result = process_my_variable(1);
echo $result;
?>
function.php
<?php
function process_my_variable($new_variable) {
global $global_variable;
$result = $global_variable + $new_variable;
return $result;
}
?>
I do not want to pass the global variable to ajax call, because my real project has many variable like this, and they should not to display because of security.
How to do this?
$.ajax({
type: "POST",
url: 'ajax.php',
data:{
global_variable:<?php echo $global_variable?>
},
success: function(data) {
alert(data);
}
});
You can send it with data object to ajax.php page
and at ajax.php page you can fetch it by:
<?php
include 'function.php';
$global_var=$_POST['global_variable'];
$result = process_my_variable($global_var);
echo $result;
?>
index.php and ajax.php (with the included function.php) are different programs. They do not share variables.
You either need to store the data somewhere that both programs can get at it (such as in a SESSION) or pass the data from index.php to the browser, and then send it to ajax.php in the query string or POST body of the Ajax request.