This code is not working properly. What i want is just send the variable $something to the page.php
What is the correct way to do this ? : data: <? php $something; ?>,
script
$something = "text";
$.ajax({
url: "page.php",
type: "post",
dataType: "html",
data: <? php $something; ?>,
success: function (data) {
$('#total').load('xxx.php');
}
});
myFile.php:
<?php $something = 'text'; ?>
<script>
$.ajax({
url: "page.php",
type: "post",
dataType: "html",
data: '<?php echo $something; ?>',
success: function (data) {
$('#total').load('xxx.php');
}
});
</script>
First of all, I think you have mistakenly mixed PHP and JavaScript. In your code the line:
$something = "text";
may be understood in two ways. If this is the whole code you have, then you are actually initializing JavaScript variable called $something. Later in the code you are trying to use the value of PHP variable called $something.
What you need to do is to change the code into (assuming you want to pass variable from PHP):
<?php $something = "text"; ?>
$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: '<? php $something; ?>',
success: function (data) {
$('#total').load('xxx.php');
}
});
or into (assuming you want JS variable):
var $something = 'text';
$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: $something,
success: function (data) {
$('#total').load('xxx.php');
}
});
Related
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'];
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);
}
});
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've an element <div id="search_result"></div>, and then I used $.ajax to retrieve some data (search result).
$.ajax({
url: "url to server",
dataType: "json",
data: keyword,
type: "post",
success: function(data){
/* load searchResult.php with data as passing parameter to searchResult.php */
$("#search_result").load("searchResult.php");
}
});
but I wanna load those data of search result to searchResult.php, how it's it,.?
and how I access those parameter in searchResult.php,.?
thanks,.
In your JS:
// ...
dataType: "json",
// pass key/value pairs
data: {keyword: "foobar"},
type: "post",
// ...
In your PHP:
if(isset($_POST['keyword']) && !empty($_POST['keyword'])) {
echo $_POST['keyword']; // echoes "foobar"
}
var value = "value of the data here";
$.ajax({
url: "serchResult.php",
data: "key="+value,
type: "post",
success: function(data){
$('#search_result').html(data);
}
});
var variable=10 ;
$("#search_result").load("searchResult.php", {"variable": variable});
On searchResult.php :
<?php echo $_POST['variable'];?>
I am sending this variable to other page, to make a validation.
However print_r($_POST); only show Array ().
Normally we use serialize, is sent the name attribute, but in this case only is sent the variable with the text. So something like $form = $_POST['data']; only works to
in firebug:
data="something"
but in my case i just send
something
code
<?php $page = "someText"; ?>
JS
default:
$("#msg").fadeTo(200, 0.1, function() {
$(this).html('success').fadeTo(900, 1);
$.ajax({
url: "page_validation.php",
type: "post",
dataType: "json",
data:'<?php echo $page; ?>',
success: function(data) {
$('#one').load('page.php');
}
});
});
break;
try it like
$page = "someText";
$.ajax({
url: "page_validation.php",
type: "post",
dataType: "json",
data:"postedvariable=<?php echo $page; ?>",
success: function(data) {
$('#one').load('page.php');
}
});
and acces the varible in page_validation.php as $_POST['postedvariable']
also another option will be to use
$.post("page_validation.php",
{postedvariable:'<?php echo $page; ?>'},
function(data) {
$('#one').load('page.php');
});
whic looks even simpler than the $.ajax