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
Related
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 1 year ago.
Improve this question
I am trying to access some data in php file sent via jquery ajax method
but I get an error of Undefined Index.
sender.php
$('#slot1').click(function(){
var selectedDate = $('#selectedDate').html();
var timeSlot = $('#timeSlot1').html();
var hm = timeSlot.slice(0, 5);
var seconds = ':00';
var time = hm + seconds;
$.ajax({
url: 'insertBookings.php',
type: 'post',
data: {date: selectedDate, timeslot: time},
async: false,
success: function(data) {
alert(data);
}
})
})
reciver.php
<?php
$date = $_POST['date'];
echo date;
?>
Consider testing with the following.
$('#slot1').click(function() {
var selectedDate = $('#selectedDate').text().trim();
var timeSlot = $('#timeSlot1').text().trim();
var selectedTime = timeSlot.slice(0, 5) + ":00";
$.ajax({
url: 'insertBookings.php',
type: "POST",
data: {
"date": selectedDate,
"timeslot": selectedTime
},
async: false,
beforeSend: function() {
console.log("Sending Date: " + selectedDate + ", Time: " + time);
},
success: function(data) {
console.log("Received: ", data);
},
error: function(xhr, status, error) {
console.log("Error:", status, error);
}
});
});
No major changes, yet I switched from .html() to .text(). The former will gather more than just the text if there is anything. Since you did not provide a Minimal, Reproducible Example, this is a precaution to ensure you're not capturing incorrect items before sending them.
I also added some extra Ajax code to help see each part as it happens.
In your PHP, you have a syntax error too. You are missing a $ before date.
<?php
$date = $_POST['date'];
echo $date;
?>
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 5 years ago.
Improve this question
The current problem now is this, the below files are working perfectly well, but when using console.log(data), it prints out the result I wanted very well.
But what I want is to print out the result in a html tag (div) profoundly instead of console.log().
The Php File section
<?php
$transaction_id = $_POST['transaction_id'];//get the Transaction ID from Ajax request..
//get the full transaction details as an json from VoguePay
$json = file_get_contents('https://example.com/?v_transaction_id=' . $transaction_id . '&type=json');
$transaction=json_decode($json, true);
//header('Content-Type: application/json');
echo json_encode($transaction);
The Ajax Section...
//clear the display section
$("#id-input2").html('');
var data="";
//call the Ajax Request..
$.ajax({
url: "php/fetch_transaction_id.php",
type: "POST",
data: {transaction_id:transaction_id},
dataType: "json",
success: function (vp_response) {
$.each(vp_response, function(index, value) {
data=(index + ': ' + value);
console.log(data);
});
$('#searchID').val('Data Recieved!');
},
});
Here is the output for using console.log();:
cur: NGN
transaction_id: 5a3182d82a8c6
email: talk2awe2004#example.com
total_amount: 1016.7000
total: 1000
merchant_ref:
memo: MLM Bank Union Creation
status: Approved
date: 2017-12-13 20:49:26
method: MasterCard & Verve (Naira)
referrer: https://www.mlmbank.net/Leaders/create.html
total_credited_to_merchant: 1001.450000
extra_charges_by_merchant: 16.700000
charges_paid_by_merchant: 15.250000
fund_maturity: 2017-12-15
merchant_id: 3362-0054095
total_paid_by_buyer: 1000
process_duration: 0.000395
I want same using a html tag instead.
You can return your array in JSON format with application/json content-type
<?php
if(isset($_POST['transaction_id'])) {
//get the full transaction details
$json = file_get_contents('https://example.com');
//create new array to store our transaction
$transaction = json_decode($json, true);
// Here you can do something with $transaction
// And return it in JSON format for ajax request
header('Content-Type: application/json');
echo json_encode($transaction);
}
After that you can get this json in ajax like this
<script>
$.get('transaction.php', {transaction_id: 'SOME ID'}, function(data) {
console.log(data);
});
</script>
jQuery will parse data as javascript object because of content-type
If the link you reach out to has raw json, you can simply echo it out :
PHP
if (isset($_POST['transaction_id']))
{
$TransactionId = $_POST['transaction_id'];
echo file_get_contents('https://example.com');
}
You want to add the success event handler in your ajax call and then $.each through your response. :
*Note : Make sure you have dataType: 'json', this tells jquery to parse the json response.
Javascript :
$.ajax({
type: 'POST',
url: '/yourphpfile.php',
data : {
transaction_id: '0'
},
dataType: 'json',
success : function(response) {
$.each(response, function(key, value) {
console.log(value.whatever);
});
}
});
Read more on accessing json values : jQuery Ajax: get specific object value
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 7 years ago.
Improve this question
$.ajax instead $.post
$.ajax converter $.post
Instead I want the use Ajax post
my js code
$.ajax({
type: "POST",
url: "signup.php",
data: "name="+name+"&email="+email+"&password="+password+"&username="+username,
success : function(login){
if(login=='ok') {
window.location="index.php";
}else{
$("#message").html(login);
}
}
If you want to use the $.post use this:
$.post('signup.php', {name:name,email:email,password:password,username:username}, function(login){
if(login=='ok') {
window.location="index.php";
}else{
$("#message").html(login);
}
});
$.ajax({
type: "POST",
url: "signup.php",
data: {name:name,email:email,password:password,username:username},
success : function(login){
if(login=='ok') {
window.location="index.php";
}else{
$("#message").html(login);
}
}
});
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
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 8 years ago.
Improve this question
I have a simple <input> and when user writes there something I need to refresh php inside div so it will look like some.php?key=thevaluefrominput. How would I do that? I guess I need to use query but I'm not sure how.
I want something like this when you write something to Type to find tags it changes the the tags bellow.
Thanks for the answers.
This sounds like a job for AngularJS :)
However this is jQuery solution:
$(function () {
$('form').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'some.php',
data: 'key=' + escape($.trim($('#myinputfield').val())),
dataType: 'html'
}).done(function (data) {
if (data) {
$('#divtopresent').html(data);
}
});
});
});
If you mean that while user types (before submission), div content changes? Then remove
$('form').submit(function (e) {
e.preventDefault();
and instead put
$('#myinputfield').keydown(function () {
There is a setInterval method that I mentioned in comment, so the first chunk of code I posted, replace it with this one:
$(function () {
var old_val = '';
var curr_val = '';
setInterval(function () {
curr_val = $.trim($('#myinputfield').val());
if (old_val != curr_val) {
old_val = curr_val;
$.ajax({
type: 'POST',
url: 'some.php',
data: 'key=' + escape(curr_val),
dataType: 'html'
}).done(function (data) {
if (data) {
$('#divtopresent').html(data);
}
});
}
}, 2000);
});
It checks if value of the field changed every 2s, please replace amount in ms (2000) if you like.