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.
Related
here is my simple code
$.ajax({
url:'action.php',
method: 'POST',
data:{getcart:1},
success:function(response){
$('#getcart').html(response);//want to display $return_value (from action page)
$('#getcart2').html(response);//want to display $return_value2 (from action page)
}
});
Here i am sending request to action.php
and if in action.php i have echo two variables separately for example.
$return_value = " //Some html here "; echo $return_value;
$return_value2= "//some other html"; echo $return_value2;
So the question is in ajax i have function with argument response . how i will be able to receive these both variables from php and display it in different divs.
i hope you guys help me. thanks.
Send the responses as JSON.
In PHP
$return = array( $return_value, $return_value2 );
echo json_encode($return);
In Javascript
var response = JSON.parse(response);
$("#getcart").html(response[0]);
$("#getcart2").html(response[1]);
your could return a json from action
echo json_encode(array('cart' => $return_value, 'cart2' => $return_value2));
then in your js,
$.ajax({
url:'action.php',
method: 'POST',
data:{getcart:1},
dataType: 'json',
success:function(response){
$('#getcart').html(response.cart1);//want to display $return_value (from action page)
$('#getcart2').html(response.cart2);//want to display $return_value2 (from action page)
}
});
You need to use json_encode in order to get multiple records or data.
action.php
<?php
header('Content-Type: application/json');
$array = array(
'var1'=> 'var value 1',
'var2'=> 'var value 2'
// more variables to go
);
echo json_encode($array);
?>
and then read your variables in JS
dataType: 'json',
success:function(response){
console.log(response.var1);
console.log(response.var2);
$('#getcart').html(response.var1);
$('#getcart2').html(response.var2);
}
You can use json_encode to create an object like:
$array = [
"return_value" => " //Some html here ",
"return_value2" => "//some other html",
];
echo json_encode($array)
Then add dataType: 'json' to your ajax options. The response then will be an object like:
{
"return_value" => " //Some html here ",
"return_value2" => "//some other html",
}
Where you can access the specific values with e.g. response.return_value2 and thus separate them.
First, I realize there are a lot of other similar posts. I have read through many, and I have still not been able to this it to work. That being said.....I have a 2 dimensional javascript object that is created dynamically. I am trying to pass it to PHP so I can save insert it into a MySQL table. It looks like the easiest way to do this is with an Ajax post.
Here is my javascript:
var jsonString = JSON.stringify(TableData);
$.ajax({
type: 'POST',
url: 'submit.php',
data: jsonString,
success: function(){
alert("OK");
}
});
I always get the success alert so I don't think the problem is there.
Here is my PHP file.
<?php
$servername = "localhost";
$username = "SME";
$password = "mypass";
$db = "p3";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db);
if (!$conn) {
die("Could not connect to database");
} else echo "hey hey";
$data = json_decode("jsonString");
print $data;
?>
I am not really sure what I am doing wrong. Both of the files are in the same folder so I don't think it's a URL problem. Any help would be appreciated.
I see the problem. In order to access the what you're passing on, you need to get it from the $_POST variable which is an array. You can iterate over that array to get what you need and insert it in to your database after doing '$data = json_decode($_POST);`
Notice that your type in AJAX is post this is the same as the form method. It might also help in your ajax if you added `dataType: "json" to your parameters.
$.ajax({
type: 'POST',
url: 'submit.php',
data: jsonString,
dataType: 'json'
success: function(){
alert("OK");
}
So it looks like in your PHP you're not actually grabbing the posted data.
This is how I would recommend getting the data:
<?php
// Check if the posted value is not empty
if (!empty($_POST('jsonString')) {
$jsonData = json_decode($_POST('jsonString'));
}
?>
Then you can do what you need to do with the $jsonData.
The main problem was in my PHP code. Here's the answer.
$data = json_decode($_POST['denied'], true);
I was then able to insert into my MySQL table like this:
foreach($data as $user) {
$sql = "INSERT INTO deniedusers (firstname, lastname, username, email, password) values ('".$user['fname']."', '".$user['lname']."', '".$user['uname']."', '".$user['email']."', '".$user['password']."')";
I appreciate everyone's answers. Thanks!
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'];?>
My code in ajax.
$.ajax({
type: 'post',
url: 'url.php',
dataType: 'JSON',
success: function(data)
{
id = // I want to get the ID data
}
});
In my (data) there's already a different data in it one of that data is the ID. What I want to do is get the ID data and save it to a variable.
Here's my PHP :
$comments = array();
$get = "Some query";
$result = $connection->query($get);
while($row = mysqli_fetch_array($result))
{
$comments[] = $row;
}
echo json_encode($comments);
The parameter "data" will be the response printed by url.php script. It depends on how you are printing the information from PHP script.
If you print it as a json like {'id': 'some_id'}, then the "id" var can be fetched using data.id on your script.
But if you are just printing text, then "data" parameter will be the printed characters from url.php.
To help you more, you can post what you have inside url.php script.
Well, you're expecting the response to be a json object, since you set dataType to 'JSON'. So for example if your php script returns something like that:
<?php $result['data'] = $yourdata; $result['id'] =$id; echo json_encode($result); ?>
Then you can use the result on the client side like this:
success: function(result)
{
id = result.id;
data = result.data;
}
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