Query ajax converter post [closed] - php

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);
}
}
});

Related

How to Parse php json_decode data to Jquery Ajax request [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 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

Data doesn't transfer to PHP from Ajax [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 7 years ago.
Improve this question
I'm trying to pass data that comes from a prompt, but the PHP doesn't receive the data.
<script>
function test(){
var r=prompt("blaksasda");
if(r){
$.ajax({
type: "POST",
url: "file.php",
data: {
param:r,
}
});} };
</script>
PHP file:
<?php
echo "$_POST['param']";
?>
Are you sure PHP doesn't receive the data? Probably Ajax call works correctly. The problem is that you don't handle the echoed result.
Try the following:
$.ajax({
type: "POST",
url: "file.php",
data: {param: r}
}).done(function(result) {
alert(result);
});
Ok, let me add the whole thing.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function test(){
var r=prompt("blaksasda");
if(r){
$.ajax({
type: "POST",
url: "test.php",
data: { param: r }
}).done(function(result){
alert(result);
}).fail(function(result){
alert('failed: ' + result);
});
}
}
</script>
</head>
<body>
<button onclick="test()">Click me!</button>
</body>
</html>
test.php looks like this
<?php
if (isset($_POST['param'])) {
echo $_POST['param'];
} else {
echo 'Whoops';
}
So when I click on the button 'Click Me', it's going to call the test function and give you a prompt and you type the text in and when you submit, you get the result back from test.php. I tried it and it worked fine.
UPDATE:
Instead of doing an alert in the success handler, just set the result like this:
.done(function(result){
$('#result').html(result);
});
Your page will look something like this. Whatever you get from your page, your span will have that. Hope it makes sense.
<body>
<button onclick="test()">Click me!</button>
<br/>
Prompt result: <span id="result"></span>
</body>

refreshing php content inside div when input onchange [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 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.

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

Ajax request for PHP to return HTML [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 9 years ago.
Improve this question
I have an element on a page that when clicked will make an ajax request to a receiver.php file:
<script>
function send(id){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id}
});
}
</script>
<img src="foo.bar" onclick="send(id)"/> <!-- simplified -->
My idea is that this receiver.php file will receive the id, then output a whole page of HTML based on that id
However, when I click on the element, the new HTML page that I expect doesn't show up. When I go to the Chrome inspector, Network tab I can see the request and the response is exactly the HTML content I need, but why doesn't it change to that new page and stay on the old page instead?
EDIT: This is my receiver.php, for testing purpose:
<html>
<head></head>
<body>
<?php
echo "<p>".$_POST['comid']."</p>";
echo "<p> foo foo </p>";
?>
</body>
</html>
this is the response:
<html>
<head></head>
<body>
<p>3</p><p> foo foo </p> </body>
</html>
Perhaps in your receiver.php script you're doing something like this, where you determine which HTML page to output depending on the id that it is receiving.
switch ($_POST['id']) {
case 'foo':
$filepath = 'bar';
break;
...
}
readfile($filepath);
You would have to reflect that in your AJAX query, using the success function of $.ajax.
function send(id) {
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id},
success: function (data) {
// Replace the whole body with the new HTML page
var newDoc = document.open('text/html', 'replace');
newDoc.write(data);
newDoc.close();
}
});
}
You have to do something with the result that comes back from you AJAX call:
function send(id){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id},
success: function(data){
console.log(data);
}
});
}

Categories