How to pass value to PHP Script - php

I have code in my html file as below. I am using jQuery Mobile
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
// On success
}
});
owner_pickup.php returns me data by executing query. Now i need to pass a value which i would read in my owner_pickup.php file.
Kindly suggest me how would we pass the value

in your php file:
$value = array(
"dat_1" => "this is data number 1",
"dat_2" => "this is data number 2"
);
echo json_encode($value);
in your jquery finction:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
please look at this answers:
retrieve multiple values from ajax call
if you don't know how to use JSON please google it.
edit:
pass a value to the php:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
data: {
first_value:50,
second_value:55
}
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
in the php:
if(isset($_GET['first_value']))
$first = $_GET['first_value'];

$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data: {param1: 123, param2: "text value"},
cache: false,
dataType:'json',
success: function(data) { // On success }
});

$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data:{key1:value1}
cache: false,
dataType:'json'
success: function(data)
{
}
});
In php aaccept it as
<?php
$_REQUEST['key1'];
?>

Related

php jquery dynamic dropdown

I am having problem sending/reading ajax variable.
Ajax code is as below:
$.ajax({
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
I have tried to read it in another php file like this:
$InvoiceType = $_REQUEST['it'];
//$InvoiceType = $_POST['it'];
//$InvoiceType = $_GET['it'];
But none of above works. The variable $InvoiceType always stays empty.
What is the problem?
The best way is to use POST method like this :
$.ajax({
method: "POST",
url: ""/wp-content/themes/canvas-child/get-clients-dropdown.php",
data: { it: 1, param2: "value2" }
})
You can get your value in $_POST['it']
Please try it with full url of file with get_template_directory_uri().
$.ajax({
type: 'GET',
url: '<?php echo get_template_directory_uri(); ?>/get-clients-dropdown.php',
data: {it: 1},
success: function(data) {
$("#ClientID").html(data);
}
});
$.ajax({
type: "GET",
data: {it: "1"},
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
try this
You have to use it like this:
$.ajax({
type: "POST",
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
Then in PHP File use this:
$InvoiceType = $_POST['it'];

How can I use ajax result as a variable that I can print everywhere in a page

Here is my ajax code:
$("#to").change(function(){
$.ajax({
type: "POST",
url: "<?php echo base_url();?>/index.php/sales/getPrice",
dataType: "html",
data: {'from' : $('#from').val() , to: $('#to').val()},
success: function(response){
//$(".location").html(response);
}
});
});
I want to use ajax result instead of 40. (below code attached).
e: {
price : 40,
category: 'Economy'
}
You want to use the data the ajax call returned? So you can use it in the success part:
$("#to").change(function(){
$.ajax({
type: "POST",
url: "<?php echo base_url();?>/index.php/sales/getPrice",
dataType: "html",
data: {'from' : $('#from').val() , to: $('#to').val()},
success: function(response){
alert(respone.price);
console.log(response);
}
});
});
Use console.log(response); in the success function, to get the data in the developer console of your browser.
var result = '';
$("#to").change(function(){
$.ajax({
type: "POST",
async: false,
url: "<?php echo base_url();?>/index.php/sales/getPrice",
dataType: "html",
data: {'from' : $('#from').val() , to: $('#to').val()},
success: function(response){
result = response;
}
});
});
Now use that result where you want:
e: {
price : result,
category: 'Economy'
}

Set textfield value from object retrieved via AJAX

I have an object like this which I retrieve via an AJAX request:
{
"id": "1",
"name": "Eyes"
}
How can I get that data for my text field #id_attributes and #name_attributes? I tried this when a button was clicked, but it gives me undefined/blank?
$.ajax({
url: "to my json",
cache: false,
type: "POST",
data: 'id=' + id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
Can anyone help me?
Try:
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json'
data:{id:id},
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
or parse the result
success: function (result) {
var jsonresult = JSON.parse(result);
$("#id_attributes").val(jsonresult.id);
$("#name_attributes").val(jsonresult.name);
}
Add dataType: 'json':
type: "POST",
data: {"id": id},
dataType: 'json',
success: function (result) {
add a dataType option
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json',
data:'id='+id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});

how to convert $.post form to $.ajax form?

I am noob at using jquery and ajax. I need to change the form from $.post to $.ajax .
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.post("sendnotification", { comment: comment.id, post: $post->id,author:$author->id}, function(result){
alert(result);
});
}];
};
I know I need to end something like here but I am stuck how to use post datas(comment,post,author) inside this function
$.ajax({
url: 'sendnotification',
type: 'POST',
data: 'query=' + query ,
dataType: 'JSON',
async: true,
success: function(data){
process(data)
}
Thanks
Just use the same object literal you did for $.post, eg (gotta assume that's some PHP or something in there)
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: {$post->id}, author: {$author->id} },
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
I believe dataType: 'JSON' should be changed to dataType: 'json'
Also, use the same data array as you used in your $.post variant.
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: $post->id,author:$author->id } ,
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});

jQuery Ajax posting not working

My jQuery ajax posting is not working. Here is the javascript
function SocialButtons() {
var $buttonWrapper = jQuery('.WrapperDiv');
if ($buttonWrapper.length){
var postData = $buttonWrapper.html();
jQuery.ajax({
type: 'POST',
url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
data: postData,
cache: false,
success: function(data) {
console.log(data);
},
contentType: "application/json",
dataType: 'json'
});
}
}
I am saving the data to be posted inside a hidden div like
<div class='WrapperDiv hidden'>{"post_id":392,"url":"http:\/\/www.wordpress-site\/post\/post-title\/","title":"SEO Friendly title"}</div>
All I am getting in return from the post.php page is an empty array. Here is my code for post.php
<?php
if(isset($_POST)){
print_r($_POST);
} else {
echo "0";
}
?>
Any Idea whats wrong?
EDIT : Its working after I removed
contentType: "application/json",
dataType: 'json'
What about something like this:
var postData = "data=" + encodeURCIComponent($buttonWrapper.html());
Than in PHP:
echo $_POST["data"];
Than parse it or something....
Couple of things to try,
Try to pass the data directly in to the data object first. If it
works then you can debug and see why it's not ready your hidden div.
instead of $buttonWrapper.html try $buttonWrapper.text();
function SocialButtons() {
var $buttonWrapper = jQuery('.WrapperDiv');
if ($buttonWrapper.length){
var postData = $buttonWrapper.**text**();
jQuery.ajax({
type: 'POST',
url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
data: **{'id':1}**,
cache: false,
success: function(data) {
console.log(data);
},
contentType: "application/json",
dataType: 'json'
});
}
}
Inside your jQuery ajax call, your data is not set to $_POST variable names. Hence why nothing is showing
Try changing your function to this:
function SocialButtons() {
var buttonWrapper = jQuery('.WrapperDiv');
if (buttonWrapper.length){
var postData = buttonWrapper.html();
jQuery.ajax({
type: 'POST',
url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
data: {postData: postData},
cache: false,
success: function(data) {
console.log(data);
},
contentType: "application/json",
dataType: 'json'
});
}
}
Then you should have a $_POST['postData'] variable on your print_r or var_dump of $_POST.
Its working after I removed
contentType: "application/json",
dataType: 'json'

Categories