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'];
Related
How can i send data like this to php using ajax
["{"title":"mr","fname":"john","lname":"Annah","oname":"Clement","staffid":"123"}"]
try json_encode
for more refer -
http://php.net/manual/en/function.json-encode.php
Do it like so, using jQuery(which you need to include in your script):
<script>
var data={};
data= {
"title":"mr",
"fname":"john",
"lname":"Annah",
"oname":"Clement",
"staffid":"123"};
$.ajax({
url:"somwhere.php",
type:"POST",
dataType:"JSON",
data:data,
async: true});
</script>
And on the page where you want to catch this data, do it like this:
<?php
$title=$_POST['title'];
$fname=$_POST['fname'];
?>
And so on.
stringify before sending
Eg :
var postData = [
{ "id":"1", "name":"bob"},
{ "id":"2", "name":"jonas"}]
this works,
$.ajax({
url: Url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(postData) //stringify is important,
});
Try this
$(document).on("click", "#your element", function () {
$.ajax({
type: 'POST',
url: "your_url",
data : {"title":"mr","fname":"john","lname":"Annah","oname":"Clement","staffid":"123"},,
success: function (result) {
### your action after ajax
},
})
})
you can pass it in data like this,
$.ajax({
url: 'url',
type: 'GET',
data: { title:"mr",fname:"john",lname:"Annah",oname:"Clement",staffid:"123" } ,
contentType: 'application/json; charset=utf-8',
success: function (response) {
//your success code
}
});
Some code I want to call from ajax is in a separate file.php:
<?php
session_start();
$email1 = $_POST['email1'];
//some code here processing $email1
$response = 'some text';
?>
This is how I call it from ajax:
$.ajax({ url: 'file.php',
data: {email1: $("#user_email").val()},
type: 'post'
});
I'd like to be able to do something like this after the call to file.php:
alert($response);
How do I do that?
In your PHP you have to echo the $response, and in your JS you have to specify the callback function like so:
$.ajax({
url: 'file.php',
data: {
email1: $("#user_email").val()
},
type: 'post',
success: function(data) {
alert(data);
}
});
Inside the ajax call, include a success.. ex:
success: function(data) {
alert(data);
},
This will pop an alert up with your response.
Try:
$.ajax({ url: 'file.php',
data: {email1: $("#user_email").val()},
type: 'post',
success: function(data) {
alert(data);
}
});
Check out the documentation
You also need to echo out the response in your PHP file:
echo $response;
Something like this?
$.ajax({
type: "POST",
url: "file.php",
data: {email1: $("#user_email").val()},
success: function(data) {
alert(data);
}
});
I send an array from PHP with json_encode, and I trying to get with AJAX and jQuery.
Every thing is ok.
JSON structure is :
names{"p1":"John","p5":"Smith"}
jQuery code is :
$.ajax({
type: "POST",
url: "return.php",
dataType: "json",
data: "id=56",
success: function(data) {
$(data.names).each(function(key, txt) {
alert(txt);
});
}
}
this code don't return any thing! I think browser don't enter in each
what should I do ?
instead this:
$(data.names).each(function(key, txt) {
alert(txt);
});
use this:
$.each(data.names, function(key, txt) {
alert(txt);
});
and your json seems to be incorrect as you mentioned: names{"p1":"John","p5":"Smith"}
this should be like this:
{
"names": {
"p1": "John",
"p5": "Smith"
}
}
you can check your json here: http://jsonlint.com/
I'd suggest you use jQuery's $.getJSON(); http://api.jquery.com/jQuery.getJSON/
But to answer your question directly; you didn't close your ajax() function.
$.ajax({
type: "POST",
url: "return.php",
dataType: "json",
data: "id=56",
success: function(data) {
$(data.names).each(function(key, txt) {
alert(txt);
});
}
});
In your code you could just use parseJSON().
$.ajax({
type: "POST",
url: "return.php",
dataType: "json",
data: "id=56",
success: function(data) {
var d = jQuery.parseJSON(data);
// ... do stuff
}
});
This is my jQuery code:
$.ajax({
type: "POST",
url: "process.php",
success: function(msg){
}
});
In process.php page I have more than one function. One function is sendmail().
How can I call this function through ajax? I wrote the code as:
url: "process.php/sendmail",
but nothing happens.
this is your script file
$.ajax({
url: "process.php",
type: "POST",
data: "functionName=sendmail",
cache: true,
success: function(response){
}
and this is your process.php file
<?php
if(isset($_POST))
{
if($_POST["functionName"] == "sendmail")
{
sendmail();
}
}
?>
With Ajax call pass some data to process.php.
$.ajax({
type: "POST",
url: "process.php",
data: {method: 'one'},
success: function(msg){
}
});
In php page,
if(isset($_POST["method"])) {
$method = $_POST["method"];
if($method=="one") {
//call methods/functions here
}
}
May below code helpful to you..
$.ajax({
type: "POST",
url: "process.php",
data: {action: 'sendmail'},
success: function(msg){
}
});
Try this..
Thanks.
There is no automatic means of telling PHP to call your functions, but you can accomplish this with a simple check. Since you are trying URLs like "process.php/sendmail" you can test on the PHP variable $_SERVER['PATH_INFO']
if ('sendmail' == $_SERVER['PATH_INFO']) {
sendmail();
}
Try using if else and process.php?fun=sendmail
if($_GET['fun'] == "Sendmail")
sendmail()
else
// something else
to call send mail function pass a parameter to process.php file, and detect that parameter like:
$.ajax({
type: "POST",
url: "process.php",
type: "POST",
data: {sendMail: "1"},
success: function(msg){
}
});
now in process.php USE:
$_REQUEST['sendMail']
to detect the value..
I am trying to send data to my PHP script to handle some stuff and generate some items.
$.ajax({
type: "POST",
url: "test.php",
data: "album="+ this.title,
success: function(response) {
content.html(response);
}
});
In my PHP file I try to retrieve the album name. Though when I validate it, I created an alert to show what the albumname is I get nothing, I try to get the album name by $albumname = $_GET['album'];
Though it will say undefined :/
You are sending a POST AJAX request so use $albumname = $_POST['album']; on your server to fetch the value. Also I would recommend you writing the request like this in order to ensure proper encoding:
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.post('test.php', { album: this.title }, function() {
content.html(response);
});
and if you wanted to use a GET request:
$.ajax({
type: 'GET',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.get('test.php', { album: this.title }, function() {
content.html(response);
});
and now on your server you wil be able to use $albumname = $_GET['album'];. Be careful though with AJAX GET requests as they might be cached by some browsers. To avoid caching them you could set the cache: false setting.
Try sending the data like this:
var data = {};
data.album = this.title;
Then you can access it like
$_POST['album']
Notice not a 'GET'
You can also use bellow code for pass data using ajax.
var dataString = "album" + title;
$.ajax({
type: 'POST',
url: 'test.php',
data: dataString,
success: function(response) {
content.html(response);
}
});
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});