Using AJAX to pass variable to PHP and get that variabel again
to send the some data to php and get the result of the php file to javascript synchronously, this is what you need
function sendData (data1,data2)
{
if (window.XMLHttpRequest)
AJAX=new XMLHttpRequest();
else
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
if (AJAX)
{
AJAX.open("POST", "url.php", false);
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.send("data1=" + data1 + "&data2=" + data2);
return AJAX.responseText;
}
else
return null;
}
now, on your php file, you just get your data like this
$data1 = $_POST['data1'];
$data2 = $_POST['data2'];
once you work with the data in your php file, and then you echo
the result, it is returned by the javascript function, so you just need to use it like this in your javascript file
var data_from_php = sendData(data1,data2);
Passing variables from javascript via ajax to php
It's just simple to pass the values to php file through AJAX call.
Change your AJAX call as shown in below
var message = $('#input-message').val();
var sender= $('#sender').val();
var receiver= $('#receiver').val();
$.ajax({
url: "scripts/load_messages.php",
method: "post",
//data: { "message":$('#input-message').val(),"sender":$('#sender').val(),"receiver":$('#receiver').val()},you can pass the values directly like this or else you can store it in variables and can pass
data: { "message":message,"sender":sender,"receiver":receiver},
success: function(data){
$('#chat-body').html(data);
},
error: function() {
alert('Not OKay');
}
});
and your load-messages.php could be like this`
$receiver = $_POST['receiver'];
echo $receiver;
jQuery AJAX: Clarification on How to Pass Variable into AJAX GET Request
In your javascript use this
$.ajax({ url: 'get.php',
data: {'ajaxid':altCheck},
type: 'post',
dataType:'json'
});
and in your get.php use
$ajaxid = $_POST['ajaxid'];
$sql = "SELECT * FROM table WHERE screeningId = $ajaxid";
How to pass variables back from php by ajax
Try to use Json as response:
PHP side
...
$json = new Services_JSON();
echo $json->encode("{a:1,b:2}");
JS
...
success:function(data){
if(data)
{
console.log(data.a);
console.log(data.b);
}
....
You can encode any array in PHP and pass it as ajax callback.
Json class you can get there
How to pass and get data from Ajax to PHP using Data-tables
Please use $_REQUEST
instead of $_GET
like this :
if(isset($_REQUEST["CurrentFlag"]))
{
$cf = $_REQUEST["CurrentFlag"];
}
echo $cf;
OR
If you want print data using $_GET
method please add type:GET
under ajax call
Cannot pass JS variable to PHP using AJAX
Nothing shows up.
And that's correct you will never get any thing by browsing index.php because there is no POST at this time , AJAX is internal and the only way to show a result from index.php is in the page that you send from it an ajax call.
At this :
success: function(data)
{
console.log(data);
}
you could control where to show your data that comes from index.php by , for example alert(data)
or document.getElementById("someelement").innerHTML=data;
and so on.
Related Topics
Wordpress: Loading Multiple Scripts with Enqueue
How to Compare Two Time in PHP
Zf2 - Get Controller Name into Layout/Views
PHP Function Imagettftext() and Unicode
Getting Multiple Checkboxes Names/Id's with PHP
Convert Exponential to a Whole Number in PHP
Http Response Code After Redirect
Run a Ffmpeg Process in the Background
Send Data from Android to Server via JSON
Remove Trailing Slash from String PHP
Strict Standards: Only Variables Should Be Assigned by Reference PHP 5.4
What Is the Maximum Size of an Array in PHP
PHP File Upload Error Tmp_Name Is Empty
Why PHP Variables Start with a $ Sign Symbol
Pass a Percent (%) Sign in a Url and Get Exact Value of It Using PHP