I have 2 divs: one is a data from php variable:
<div id="data"><?php echo "$strstr"?></div>
Second is the div for ajax request:
<div id="divButton">[CLICK ME]</div>
Here is js code:
$("#divButton").on("click", function() {
$.ajax({
type: "POST",
url: "test.php",
dataType: "json",
data: {simpledata:12345},
success : function(data) {
if (data.code == 200)
console.log("Success!");
else console.log("Error!");
}
});
And finally php code:
<?php
$strstr = "A";
if ($_POST) {
$strstr = $strstr."A";
}
?>
As you can see I need dynamic update of first div, by clicking on second div.
What am I doing wrong?
You can't change the value of $strstr variable by just updating the value of it in you if condition. You need to make an action in your ajax when return is success. Like this
$("#divButton").on("click", function() {
$.ajax({
type: "POST",
url: "test.php",
dataType: "json",
data: {simpledata:12345},
success : function(data) {
$('#data').text(data)
}
});
});
And in your php if condition you must return a string like this
<?php
$strstr = "A";
if ($_POST) {
$strstr = $strstr."A";
echo json_encode($strstr );
}
?>
HTML code Should be
<div id="data"><?php echo "$strstr"?></div>
<div id="divButton">[CLICK ME]</div>
Ajax Call Code
$("#divButton").on("click", function() {
$.ajax({
type: "POST",
url: "test.php",
data: {simpledata:12345},
success : function(data) {
$("#data").html(data);
},
error : function() {
console.log("Error!");
}
});
test.php Code Should be
<?php
$strstr = "A";
if ($_POST) {
$strstr = $strstr."A";
}
echo $strstr;
//It is advisable that do not close php tag
Related
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">
I am trying to get value of div where ID is Class when Div is clicked.
function caller(){
$.ajax({
url: "test.php",
type: "get",
data: {
Class_info: $('#Class').val()
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
Looks like it isn't reading the value of Div when clicked. Am I using AJAX wrongly?
test.php
<?php
$a = $_GET['Class_info'];
echo($a);
?>
div tag dont have value attribute. So you can use $('#Class').attr('value');
check snippet below..
function caller(){
alert($('#Class').attr('value'));
$.ajax({
url: "test.php",
type: "get",
data: {
Class_info: $('#Class').attr('value')
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">test</div>
You can use this little trick:
<div id "" class="Class_ticket" onclick="caller()" value="1">
<input type="hidden" value="1" id="Class" />
</div>
note that the input is hidden!
And here is your ajax:
function caller() {
$.ajax({
url: "test.php",
type: "get",
data: {
Class_info: $('#Class').val()
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
Try this:
function caller(){
$.ajax({
url: "test.php?Class_info=test",
type: "get",
data: {
Class_info: $('#Class').val()
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
I added ?info=test which in theory PHP should be able to take
if PHP takes the value, then instead of test put a variable containing the desired value
I am trying to send a JS variable to a PHP file but it does not seem to be working. In the console its showing an error as an show.php file. I am not able to understand What I am doing wrong.
function details(id) {
var id = id;
// alert(id);
$.ajax({
type: 'POST',
url: 'show.php',
data: id,
success: function(data) {
alert("hi");
}
});
}
<button onclick="details(<?php echo $id ; ?>)" class="btn btn-rounded btn-primary">Details</button>
show.php:
<?php
if (isset($_POST['id']))
{
$uid = $_POST['id'];
echo json_encode($uid);
}
?>
Write data in json object
see code below
function details(id)
{
var id = id;
// alert(id);
$.ajax({
type: 'POST',
url: 'show.php',
data: {id:id},
success: function(data)
{
alert("hi");
}
});
}
Check your network tab and check the sending parameters list . You have to mention datatype json
Try this
function details(id)
{
var id = id;
// alert(id);
$.ajax({
type: 'POST',
url: 'show.php',
dataType: 'json',
data: {'id':id},
success: function(data)
{
alert("hi");
}
});
}
In your show.php
<?php
if (isset($_POST['id']))
{
$uid = $_POST['id'];
echo json_encode($uid);
}
?>
I am having a problem with my code as my php variable is not being echoed on page when using ajax jquery. Here is my code...
<script>
function refresh_div() {
$('.loader').show();
var username = "<?php echo $user; ?>";
jQuery.ajax({
type: "POST",
url: "load.php",
data:{user : username},
success:function() {
jQuery("#load_msgs").append(response+'<br>');
},
complete: function(){
$('.loader').hide();
}
});
}
t = setInterval(refresh_div,1000);
</script>
i am trying to send "username" to page url "load.php"... i called it this way but nothing is being echoed...
if(isset($_POST['user'])) {
echo $_POST['user'];
}
pls help out thanks... :)
edited...
when i tried using this code i.e adding passing response as parameter in success function like this ...
<script>
function refresh_div() {
$('.loader').show();
var username = "<?php echo $user; ?>";
jQuery.ajax({
type: "POST",
url: "load.php",
data:{user : username},
success:function() {
jQuery("#load_msgs").append(response+'<br>');
},
complete: function(){
$('.loader').hide();
}
});
}
t = setInterval(refresh_div,1000);
</script>
.... the data (username) gets displayed every second.. like a blink toggling on and off the page... how do i make the data display static in order to use the variable on the ajax page. Thanks :)
Where the response variable you get? Should be :
success : function( response ) {
jQuery("#load_msgs").append(response+'<br>');
},
please take a datatype as json in ajax call
and in loads.php function add two line as below..
if(isset($_POST['user'])) {
$data['user'] = $_POST['user'];
}
echo json_encode($data);
also change in success function.
success:function(response) {
jQuery("#load_msgs").append(response.user);
}
A bit more simplified would be:
$.post(URL, { data: somefield }, function(result) {
result = JSON.parse(result);
If(result && result.status === 'true'){
}
)};
Depends a bit on the return at server side.
You missed response in success function.
Try this:
<script>
function refresh_div() {
$('.loader').show();
var username = "<?php echo $user; ?>";
jQuery.ajax({
type: "POST",
url: "load.php",
data:{user : username},
success:function(response) {
$("#load_msgs").append(response+'<br>');
},
complete: function(){
$('.loader').hide();
}
});
}
t = setInterval(refresh_div,1000);
</script>
I am using ajax, php and json to store click counts in json file...Let's say the file is this:
count.json
{"countbtn1":28,"countbtn2":25,"countbtn3":39}
downloadsCount.php
<?php
$buttonName=$_POST["buttonName"];
$file = "count.json";
$json = json_decode(file_get_contents($file), true);
echo $json['count'.$buttonName] ;
?>
downloads.php
<?php
$buttonName=$_POST["buttonName"];
$file = "downloads.json";
$json = json_decode(file_get_contents($file), true);
$json['count'.$buttonName] = $json['count'.$buttonName] + 1;
file_put_contents($file, json_encode($json));
echo 'success';
?>
And I need to place each btn value inside these on page load:
<div class='smallbtn1'>0</div>
<div class='smallbtn2'>0</div>
<div class='smallbtn3'>0</div>
Counting and updating the json file with this:
$('.download').click(function() {
//get name of button
var name= $(this).prop('name');
$.ajax({
url: "downloads.php",
data:{buttonName:name},
method: "POST",
success: function(response) {
if (response = 'success') {
//get count download
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response);
}
});
}
}
});
return true;
});
..and I tried updating the count on page load with this:
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response); }
});
});
...but it doesn't update the values on load, just after the first click.
PS: The whole thing IS wrapped in $(document).ready(function(){..});
After seeing the code on your server, I'd say the issue is how you are calling the AJAX. You are doing this:
$(document).ready(function(){
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response); }
});
});
But you don't have defined what name is (it is defined on the click event, but not when the page is loaded for the first time). You should do something like this:
$(".download").each(function() {
var name = $(this).attr("name");
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name },
method: "POST",
success: function(response){
$('.small'+name).html(response);
}
});
});
So the AJAX is called for each one of the buttons, and you use the attribute name by doing $(this).attr("name").
Try sending the AJAX request after the page completely loads using $(document).ready():
$(document).ready(function() {
$.ajax({
url: "downloadsCount.php",
data: { buttonName:name },
async: false,
method: "POST",
success: function(response) {
$('.small' + name).html(response);
}
});
});
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