Passing variable from PHP file to php file using Jquery - php

I want to pass a variable from file1.php to file2.php using jquery.
file1.php
<?php
$user_rank = $rank;
?>
file2.php
<?php
$user_rank = $_GET['user_rank'];
?>
AJAX
function getRank()
{
$.ajax({
type: "GET",
url: "file2.php",
data: ?????,
success: function(result){
$("#TargetRank").html(result);
}
});
};
Can anyone help me with this?

The passing part can happen in the script where the variable is defined, so in file1.php. Then you get the following files:
file1.php:
<?php
$user_rank = 123;
?>
<script>
function getRank()
{
$.ajax({
type: "GET",
url: "file2.php?user_rank=<?php echo $user_rank; ?>",
success: function(result){
$("#TargetRank").html(result);
}
});
};
</script>
file2.php:
<?php
$user_rank = $_GET['user_rank'];
echo $user_rank;
?>

I'm guessing the Javascript code is used in file1.php. Then your question becomes more like "How do I pass a PHP variable to Javascript?". The best way I have seen is with a "data element" in the DOM.
Add this to file1.php (somewhere logicalish)
<span id="user-rank" data-rank="<?= $user_rank ?>"></span>
Then you can grab that value in your JS
function getRank()
{
var rank = $("#user-rank").attr("data-rank");
$.ajax({
type: "GET",
url: "file2.php?user_rank="+rank,
success: function(result){
$("#TargetRank").html(result);
}
});
};

Assuming your AJAX is in file1.php you could do this:
file1.php
<?php
$user_rank = $rank;
?>
<script>
function getRank()
{
$.ajax({
type: "GET",
url: "file2.php",
data: {user_rank: '<?php echo $user_rank; ?>'},
success: function(result){
$("#TargetRank").html(result);
}
});
}
</script>

On file1.php output the variables as JSON (see Returning JSON from a PHP Script )
Then on the JavaScript do an ajax call to read the variables to an object (let's call it data).
Then do your call placing the data variable where you have the ????? .
Example:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
$(function(){
$.getJSON( "http://dev/json.php", function( data ) {
$.ajax({
type:'GET',
url:'json2.php',
data: data,
success: function(data){
console.log(data);
}
});
});
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
json.php
<?php
header('Content-Type: application/json');
echo '{"name":"Telmo Dias"}';
json2.php
<?php print_r($_GET); ?>
Result:

Related

how to pass JavaScript value to PHP page

It is showing undefined function in PHP page.And i made every possible way it is not working for me.Please guys help me with this.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<div id="myDiv">Welcome to stackoverflow</div>
</body>
<script>
$(document).ready(function(){
var mb = $('#myDiv').text();
$.ajax({
action : "six-cuf.php",
type: 'POST',
data: { 'mb': mb},
success:function(data)
{
alert(mb);
}
});
});
</script>
</html>
PHP CODE
<?php
$fname = $_POST['mb'];
?>
Hello
$(document).ready(function(){
var mb = $('#myDiv').text();
$.ajax({
action : "six-cuf.php",
type: 'POST',
data: { mb: mb},
success:function(data)
{
alert(mb);
}
});
});
You can also try this JQuery Post Method
Javascript Code
$.post("six-cuf.php", {'mb': mb}, function (data) {
console.log(data);
});
PHP code
<?php
$fname = $_POST['mb'];
echo $fname;
?>

define a string as POST request using AJAX in the same page onclick

for the PHP is just simple check:
All in the same page named **
page.php
if(isset($_POST['XYZ']){
echo "WORKING";
}
then for my HTML:
<h1 id='XYZ'>CLICK ME</h1>
now at the same page i'm trying to do the AJAX POST request like this
$('#XYZ').click(function(){
var XYZ = 'XYZ';
$.post('page.php',{
XYZ: XYZ
})
})
and the request didn't work, How do i just pass the $_POST data? I removed success function since i didn't think it is useful in this case.
What i want is when i click on the <h1> the echo appears.
try this:
var my_string = 'xyz';
$.ajax({
url: 'ajax.php',
type: 'post',
data: { "action":my_string},
dataType: "json",
success: function(response) {
if(response['state'] == 'ok'){
console.log("ok");
}
}
});
ajax.php
<?php echo $_POST['action']; ?>
<head>
<script>
var my_string = 'xyz';
$.ajax({
method: 'POST',
url: './giveposts',
dataType: "text",
contentType: "application/json; charset=utf-8",
data:my_string,
success: function(data) {
{
$("#test").html(data);
}
}
});
</head>
</script>
<body>
<div id="test>
/*echo
</div>
</body>

php variable to ajax data

I have edited my code and it goes something like this and it is not working.. Please help me..
<?php
$variable = "krishna";
?>
<script>
$.ajax({
type:"POST",
url:"ajax.php",
data:{
variable:<?php echo $variable; ?>
},
success:function(msg){
$("#val").html(msg);
}
});
</script>
<div id="val"></div>
ajax.php
<?php
echo $_POST['variable'];
?>
thank you all
Try this
<script type="text/javascript">
$.ajax({
type: "POST",
url: "",
data: 'var=<?php echo $variable;?>',
success: function(){
}
});
</script>
<script>
$.ajax({
type:"POST",
url:"",
data:{data:'<?php echo $data; ?>',data1:'<?php echo $data1; ?>'}
success: function(data)
{ }
});
</script>
You can add any number of varibles using data{data1:data1, data2:data2, data3:data3} and it's stand like {variablename:value}
Use an echo statement inline with the javascript. Because PHP executes on the server all the PHP processing will be done by the time the javascript runs.
<?php
$variable = "php";
?>
<script>
$.ajax({
type:"POST",
url:"",
data:{
variable:"<?php echo $variable; ?>"
},
success:
});
</script>

AJAX call success but data not retrieved at server

I have an ajax call that passes data to another php file, createTest2.php, as below.
But the createTest2.php file throws error
"Notice: Undefined index: aaa in C:\xampp\htdocs\TestProj\Test\createTest2.php on line 2
I have no clue how to fix it.
caller.php
$(document).ready(function(){
$("#button_submit").click(function()
{
$.ajax({
type:"POST",
url:"createTest2.php",
data:{aaa : "UNIT_TEST"},
success:function()
{
alert("success");
}
});
});
});
createTest2.php
$test_name = $_POST['aaa'];
if you are using
$test_name = $_POST['aaa'];
you have to call ajax like this
$(document).ready(function(){
$("#button_submit").click(function()
{
$.ajax({
type:"POST",
url:"createTest2.php",
data:"aaa=UNIT_TEST",
success:function()
{
alert("success");
}
});
});
});
the main thing is that " data:"aaa=UNIT_TEST", "
Try this:
//sample.php
<?php
if(isset($_POST) && isset($_POST['aaa'])){
echo json_encode("hello world! Your data was: " . $_POST['aaa']);
}
?>
//your client side page
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function send(){
$.ajax({
type:"POST",
url: 'sample.php',
data:{aaa:"UNIT_TEST"},
dataType: 'json'
}).done(function(data){
alert(data);
});
}
</script>
</head>
<body>
Send
</body>
</html>
Try this in your, for example, index.html:
<script>
$(document).ready(function(){
$("#button_submit").click(function()
{
$.ajax({
global: false,
type:"post",
dataType: "html",
cache: false,
url: "createTest2.php",
data: {aaa:'test'},
success:function(html) { alert(html); },
error: function(e) {alert(e);}
});
});
});
</script>
And in your createTest2.php just put this to see if ajax calls are received:
<?php
echo $_POST['aaa'];
?>

Call PHP Function by Ajax

I'm new for Web sites.I have a text box in my page and what i need is when onBlur that text box, call a php method that include sql query.I found something like on the web, but still does net work it.Am i doing wrong?
<?php
if(isset($_POST['Greet']))
{
echo $_POST['Greet'];
}
?>
<html>
<body>
<script type="text/javascript">
function sayHi()
{
var value = $.ajax({
type: "POST",
url: "page.php",
data: "Greet="+$("#Greeting").val(),
async: false
}).responseText;
}
</script>
<input type="text" name="Greeting" id="Greeting" onblur="sayHi()">
</body>
</html>
exit your php for usage in same script, ajax will return entire html.
<?php
if(isset($_POST['Greet']))
{
echo $_POST['Greet'];
die;
}
?>
value will have your response alert(value)
later edit:
<?php
if(isset($_POST['Greet']))
{
echo $_POST['Greet'];
die;
}
?><html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
function sayHi()
{
var value = $.ajax({
type: "POST",
url: "index.php",
data: "Greet="+$("#Greeting").val(),
async: false
}).responseText;
$('#result').html(value);
}
</script>
<span id="result"></span>
<input type="text" name="Greeting" id="Greeting" onblur="sayHi()">
</body>
</html>
You should include the jQuery script to your page to make $.ajax work.
Like this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

Categories