extract json data in php and print it [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
array(2) {
["data"]=> array(1)
{
["MemberData"]=> array(1)
{
["Verified"]=> string(1) "Y"
}
}
["error"]=> array(2)
{
["code"]=> string(0) ""
["text"]=> string(0) ""
}
}
/i want to print verified value in a php page/

This is the way to do it from PHP to javascript:
var ar = JSON.parse( '<?php echo json_encode($array) ?>' );
//ar[1]['MemberData']; //first memberdata value
console.log(ar[1]['MemberData'].value);
and from javascript to php you need to use ajax
jQuery.ajax({
type: "POST",
url: 'xxxxxxx',
dataType: 'json',
data: {array:array, //here you can pass the verified value of the array or the whole array if you want
},
success: function(data) {
if(data.success == true){
}
},
error: function(data) {
if(data.success != true){
}
},
});
And then you can read it like normal POST on PHP-side.
Otherway of doing it would be to reload the URL with ?verified='javascript_array_value' and read it with GET

Related

Why the javascript code doesn't connect to the php file? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
Javascript code
$(document).ready(function () {
//Add to cart function
$('.add_to_cart').click(function (e) {
//alert("add to cart");
e.preventDefault();
var product_id = $(this).val();
$.ajax({
method: "POST",
url: "functions/carthandler.php",
data: {
"product_id": product_id,
"scope": "add"
},
dataType: "dataType",
success: function (response) {
alert("success");
if (response == 401) {
alert("Login to continue");
}
}
});
});
});
PHP code with path 'functions/carthandler.php'
<?php
include('../includes/connect.php');
session_start();
if (isset($_SESSION['username'])) {
if (isset($_POST['scope'])) {
$scope = $_POST['scope'];
echo "<script>alert('success')</script>";
exit;
}
} else {
echo 401;
}
?>
I've checked whether js code connect to the php code using echo, but the php doesn't seem to have respond anything. I've also tried rechecking the indentation and everything and still not working.

The check does not work:- if (isset ($_PHP["form_name"])) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
There are several forms for queries in the file, you need to do a form check, if you do not do this, all forms with queries are executed, as for me it is not correct. Here with the help of if (isset($_POST ['form_name'])) I do a check, but does not exit. What is the problem?
<form id="form" method="POST">
<input type="text" name="telephone[]">
<input type="text" name="telephone[]">
<input type="hidden" name="form_name" value="form_ex">
<button type="submit" name="update" id="btn_post_phones" value="button_value">Update phone</button>
</form>
if (isset($_POST['form_name'])) {
var_dump ('true -->');
echo "<pre>";
var_dump(json_decode($_POST["phones"], true));
} else {
var_dump ('false-->');
echo "<pre>";
var_dump(json_decode($_POST["phones"], true));
}
//string(8) "false-->"
//array(2) {
// [0]=>
// string(6) "345345"
// [1]=>
// string(6) "345345"
//}
$('#form').submit(function(event) {
var arrPhones = new Array();
event.preventDefault();
$('input[name="telephone[]"]').each(
function() {
arrPhones.push($(this).val());
});
var data='phones='+JSON.stringify(arrPhones);
$.ajax({
type: 'POST',
url: 'regist.php',
dataType: 'json',
data: data,
// data: $('input[name="telephone[]"]').serialize,
beforeSend: function(){
console.log('before ' + data);
},
success: function(response){
console.log(response);
}
});
});
1.Remove var data='phones='+JSON.stringify(arrPhones);
2.instead of data: data, write data:{'phones':JSON.stringify(arrPhones)},
3.Instead of if (isset($_POST['form_name'])) {...} use if (isset($_POST['phones'])) {...}

jQuery / Ajax: How to pass JS variable to specific PHP function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to PHP and never used Ajax before so I hope someone can help me with this.
I have a separate functions.js file that stores all the JS I use for the pages of a website.
So far all the exchange with the db is done with PHP directly on the pages which works fine so far but now I have a scenario where I need to initiate this from the JS file.
My thought was I could create a separate PHP file (ajax.php) that hosts functions just for such purposes and then pass data via Ajax from the JS file to this PHP file.
So far I have the following but since there are multiple functions on the ajax.php file I am not sure how to pass this to the specific function I need.
Also, I am not sure if the Ajax call I have is set up correctly.
Can someone help me with this and maybe also explain your answer in a few words ?
Basically, here I want to pass the JS variable "itemID" to the PHP function fetchTransMain where this should be used for the variable $itemID (the variable $trans is generated in PHP).
Update: I would also need to know how to get the result from the PHP function back in JS. I didn't find an approach for this yet, perhaps with an Ajax GET call ??
What I have in my functions.js file:
var itemID = "someID";
$.ajax({
url: "ajax.php",
type: "post",
cache: "false",
data: itemID,
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
...and on my ajax.php file:
function fetchTransMain($trans, $itemID){
foreach($trans as $key => $val){
if($val["ID"] == $itemID){
echo $val["trans"];
}
}
}
Many thanks in advance,
Mike
You have to define data as an object:
$.ajax({
url: "ajax.php",
type: "post",
cache: "false",
data: {itemId: itemID},
success: function(data) {
console.log(data);
alert("success");
},
error: function(){
alert("failure");
}
});
And then:
$itemId = $_POST['itemId'];
function fetchTransMain($trans, $itemID){
foreach($trans as $key => $val){
if($val["ID"] == $itemID){
echo $val["trans"];
}
}
}
I strongly suggest you to read some tutorial about ajax and PHP because there are so many aspects involved in this question that we can't explain them all in one answer. Anyway what you want to do is very common, you have a value in your browser that you want to save on your server.
The first part is almost correct:
$.ajax({
url: "ajax.php",
type: "post",
cache: "false",
data: {valueName: itemID},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
Just pay attantion to the data parameter, you should pass an object there with a name. That name is the name that you'll use on PHP to retrieve the value.
Then on server side:
function fetchTransMain($trans, $itemID){
foreach($trans as $key => $val){
if($val["ID"] == $itemID){
echo $val["trans"];
}
}
}
$valueFromClient = $_POST['valueName'];
fetchTransMain($trans, $valueFromClient);
Here you have to use $_POST since we have choose type: "post" on the client side.
Hope this helps

How to store value passed by ajax to database? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to display data or value return by the ajax function to be store in php variable as mentioned below. Any help?
This is my code
function getUserInfo()
{
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + acToken,
data: null,
success: function(resp) {
user = resp;
console.log(user);
$('#uName').text('Welcome ' + user.name);
$('#imgHolder').attr('src', user.picture);
},
dataType: "jsonp"
});
}
I want to display in php variable as
$name=$_POST['uName'];
$pics=$_POST['imgHolder'];
echo $name;
echo $pics;
I would use PHP to make the request and store the result in an array, since you need to use the result in php
$acToken='your value';
$result = file_get_content('https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$acToken);
echo '<pre>';
print_r($result);
echo '<pre>';
make sure to assing a value to $acToken;
In your javascript, create ajax post request to your php url:
$.ajax({
type: "POST",
url: "some.php",
data: { uName: "John", imgHolder: "img" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
For more information, you can read jQuery AJAX documentation
try below:
$name=$_POST['uName'];
$pics=$_POST['imgHolder'];
echo json_encode(array('name'=>$name,'pics'=>$pics));
as that help for you

JavaScript/Php strange mishap, data became a key?

I am using this code:
javascript: (function () {
var jsCode = document.createElement('script');
jsCode.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js');
document.body.appendChild(jsCode);
var stuid = prompt("Please provide student id");
$.ajax({
url: "data.php",
data: stuid,
success: function(response) {
console.log(response);
}
});
}());
and the var_dump that came back was strange. I var_dumped the GET request and this is what i got..
array(1) {
[1]=>
string(0) ""
}
Why did it become the associative array's key? How can i avoid it?
I am sorry if this sounds like a stupid question, i am new.
This is your problem:
data: stuid,
The data variable needs key - value pairs, so you would need to do something like:
data: { "id": stuid },

Categories