ajax calling of php function from a php page - php

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..

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 to Send Jquery Ajax data on Same php Page?

hi i want to send javascript data via ajax to the same page in wordpress. Cannot fetch the data in testajax.php page. its shows Undefined index: name. And how to send the data on same php page rather than testajax.php ?
<script>
jQuery.ajax({
type: "POST",
url: 'http://localhost/projects/xxx/wordpress/wp-content/themes/xxx/testajax.php',
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});
var foo = 'somename';
jQuery.ajax({
url:'yourUrl',
type: "POST",
data: {'name':foo},
success: function(data)
{
alert('success');
}
});
php
<?php
if(isset($_POST['name'])){
//Do whatever you want
}else{
//Do whatever you want
}
?>
jQuery.ajax({
url: "./_FILE_" // ./index.php for example
type: "POST",
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});

ajax call php code in a file and get result

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

jQuery is not sending data to codeigniter

I am trying to get ajax working with a codeigniter installation.
This is my PHP function:
function test() {
return print_r("hey");
}
This is the JS:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
success: function(data) {
alert(data);
}
});
This works perfectly but, as soon as I add data it doesn't work.
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: {bar:"foo"},
success: function(data) {
alert(data);
}
});
Thanks in advance!
please check the following
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "&bar=foo&isAjax="+true,
success: function(data) {
alert(data);
}
});
And controller...
function test() {
if($this->input->post('isAjax')){
return print_r("hey");
}
else{
//do another thing
}
}
And another thing if you want to add data in json format then you have to add another property in your $.ajax object that is datatype: "json"
Use following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
dataType: 'json',
data: {'bar':'foo'},
success: function(data) {
alert(data);
}
});
Or you can use shorthand version:
$.post("http://localhost/code/test", {'bar':'foo'}, function(data) {
alert(data);
});
And your php code should be:
function test() {
echo "hey";
}
Try the following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "bar=foo&name=cyberbob",
success: function(data) {
alert(data);
}
});

problem with ajax call of json and php

i have one AJAX function getting results from php file as bellow
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
now i want to use the value of g_foo but i can't i try using console.log but i am facing strange problem. like
if i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
console.log(g_foo);
}
});
now i can see the value return from php file
if now i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
now i got error undefined g_foo;
thanks
As ajax is asynchronous, there's no way of making sure that g_foo is available as soon as you call the $.ajax() request, hence it's not available outside that callback.
You could however, specify async:false to that .ajax() call, but it will most likely block the browser, attempting to wait for the request, something I don't think you want to happen.
To use it outside you have two ways:
1)make the call syncronous like this (this is not a best practice as the browser has to wait for the call to continue the flow):
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
async: false,
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
2) call a function on success and continue the flow from there using whatever data has been returned from the function (this is the best practice)
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json'
success: function(json)
{
g_foo = json.foo;
yourfunction(g_foo);
}
});
function yourfunction(g_foo){
//here g_foo is set
}

Categories