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>
Related
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:
I'm trying to counting a SESSION variable, but i don't want that the user seeing any refreshes.
my problem with the code below is that, it change only ones and then its needed a refresh. How can i do this without any refreshes?
test page:
<?php session_start(); ?>
<script src="../../../common/js/jquery-1.11.2.min.js"></script>
<script src="../../../common/js/jquery.touchwipe.min.js"></script>
<?php
if(empty($_SESSION['counter'])){
$_SESSION['counter'] = 1;
}
$count = $_SESSION['counter'];
?>
<div id="main"><?= $count; ?></div>
<button id="detailed">Link</button>
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','#detailed',function(){
var count = "<?= $count ?>";
count++;
$.ajax({
type: "POST",
url: "test.php",
data: {countertje: count},
success:function(data){
$('#main').html(data);
console.log(data);
}
});
})
});
</script>
do_ajax.php
<?php
session_start();
$_SESSION['counter'] = $_POST['countertje'];
echo $_SESSION['counter'];
?>
<script type="text/javascript">
var count = "<?= $count ?>";
$(document).ready(function(){
$(document).on('click','#detailed',function(){
count++;
$.ajax({
type: "POST",
url: "test.php",
data: {countertje: count},
success:function(data){
$('#main').html(data);
console.log(data);
}
});
})
});
</script>
count should be a global variable
Something you could use in javascript
var count = $('#main').val();
instead of following :
var count = "<?= $count ?>";
Writting PHP code inside javascript isnt good idea!
I am warking in phalcon framework, and i am trying to call controller's method to get simple string using jquery-ajax. When i placed my ajax call inside $(document).ready(function() ajax call worked, but when i placed same code inside $('#dugme').click(function() ajax call reported error. I am confused. Here is my view code:
<script type="text/javascript">
$(document).ready(function(){
//alert($("#nesto").val());
//atr = $(".klasa").attr("id");
//alert("Id je: " + atr)
$.ajax({
url: '<?php echo $this->url->get("xml/posalji");?>',
type: 'POST',
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("Neuspjesan JSON zahtjev!");
}
});
$('#dugme').click(function(){
$.ajax({
url: '<?php echo $this->url->get("xml/posalji");?>',
type: 'POST',
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("Neuspjesan JSON zahtjev!");
}
});
}) ;
});
</script>
<h2>Basic example</h2>
<?php echo Tag::form("xml/pretraga"); ?>
<p>
<label for="name">Title</label>
<?php $opt = array('title', 'id'=>'nesto', 'size'=>'10');
$buttopt = array('Show', 'id'=>'dugme','class'=>'klasa');
?>
<?php echo Tag::textField($opt) ?>
</p>
<p>
<?php echo Tag::SubmitButton($buttopt) ?>
</p>
</form>
and here is my action code:
public function posaljiAction(){
$this->view->disable();
$data = "My name is Nedimo";
echo json_encode($data);
}
Please, can anyone tell me what is wrong in my code.
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'];
?>
<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
var content = $('#content').html();
var data = {"content":content};
$.ajax({
type: "POST",
dataType: "json",
url: "ajax.php",
data: {content:content},
success function (data) {
alert('Hello!');
}
});
});
});
</script>
<div id="content"><?php echo $content; ?></div>
ajax.php
echo json_encode($_POST['content']); ?>
Nothing happens... WhatI really want to achieve is to get that alert box and get the return data, but I am lost since I don't get any errors or nothing.
You miss " : " after success
<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
var content = $('#content').html();
var data = {"content":content};
$.ajax({
type: "POST",
dataType: "json",
url: "ajax.php",
data: {content:content},
success: function (data) {
alert('Hello!');
}
});
});
});
</script>
<div id="content"><?php echo $content; ?></div>
As #sofl said, if you change it to success:function (data) { it will work!
Just remember that the $("a") from $("a").click(function() { called when click in a link tag like <a href"">.
If you are using an input ou button with a class="a" you should change the code to $(".a").click(function() {
(just add a . before a)
PS: If you're using a link, you should set the href="" to href="#" to work.