Author

Topic: div value to jquery or php variable (Read 2001 times)

legendary
Activity: 2088
Merit: 1015
March 06, 2016, 06:13:42 AM
#7
It looks like you've got pretty much what you need already however you are directly placing you're update into the div with jQuery instead of saving it.

Change
Code:
var updateInterval = setInterval(function() {
 $('#thealert').load('alertprice.php');
},1*1000);

To something like
Code:
var alertPriceVariable;
var updateInterval = setInterval(function() {
$.get( "alertprice.php")
  .done(function( data ) {
    alertPriceVariable = data;
   $('#thealert').text(alertPriceVariable);
  });
},1*1000);
newbie
Activity: 51
Merit: 0
March 05, 2016, 02:01:21 PM
#6
Not sure if this will help but this is how I get a changing php value into javascript.

Code:



453.33



// alertprice.php

$resultmysqli_query($conn"SELECT price FROM latestprice");
while($row mysqli_fetch_array($result)){ 
$latest_price $row['price'];
}

echo 
$latest_price;



?>


And to get the div back into javascript

Code:
latestPrice = document.getElementById("thealert").innerHTML;

or

latestPrice = xhttp.responseText;
sr. member
Activity: 658
Merit: 257
March 05, 2016, 01:23:06 PM
#5
up
newbie
Activity: 14
Merit: 0
March 02, 2016, 03:49:36 PM
#4
Edit for a better title and we will get more responses!
sr. member
Activity: 658
Merit: 257
February 28, 2016, 12:39:32 AM
#3
Not quite sure I follow...

If you have the number in php, you can send it to javascript like this:
Code:

But, this would only run when the page first loads... if you are updating via $.ajax() or curl(), I would send a callback function to update the variable with the response data



If you are trying to update a div using jquery, I use something like:

Code:
$('#latestPrice').change(function() {
  $("#latestPriceDisplay").html($(this).val());
});


Problem is that my div or variable is updating when price changes  and when you do this

var latestPrice = ;

you will not get updated price until page refresh.I want the variable to update with price change without refresh
hero member
Activity: 798
Merit: 722
February 26, 2016, 01:06:40 PM
#2
Not quite sure I follow...

If you have the number in php, you can send it to javascript like this:
Code:

But, this would only run when the page first loads... if you are updating via $.ajax() or curl(), I would send a callback function to update the variable with the response data



If you are trying to update a div using jquery, I use something like:

Code:
$('#latestPrice').change(function() {
  $("#latestPriceDisplay").html($(this).val());
});
sr. member
Activity: 658
Merit: 257
February 26, 2016, 04:28:29 AM
#1
In my js file i have this:

var updateInterval = setInterval(function() {
 $('#thealert').load('alertprice.php');
},1*1000);

In my php file i SELECT the latest price from MYSQL database for example 453.33

In html it shows at time 14:00:01 :

453.33


Then at time 14:00:02 :

463.33


How do i get the updated value in a jquery or php variable so i can use it for an alert script ?

I want always to have in variable:

$latestprice = "463.33";

Or what ever latest price is at current time.

I want to store the updated value inside the div in a php variable or jquery or javascript variable to be used by other scripts ,if div value changes then $latestprice should change also.

Update:$latestprice needs to go in here:

   

Jump to: