Bitcoin Forum
November 19, 2017, 10:01:27 PM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: div value to jquery or php variable  (Read 1860 times)
spy100
Sr. Member
****
Offline Offline

Activity: 240


View Profile
February 26, 2016, 09: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 :

<div id="thealert">453.33</div>

Then at time 14:00:02 :

<div id="thealert">463.33</div>

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:

   <script>
    $(document).ready(function(){



    $( "#myHigh" ).keyup(function(e) {

    var key = e.which || e.keyCode;

    if ( key == Cool{}else{


    nHigh = $( "#myHigh" ).val();
    $('#myHigh').append(nHigh);

    if (nHigh <= <?php echo $latestprice; ?> ) {
    document.getElementById('play').play();
    }else
    {
    document.getElementById('play').pause();
                return false;
    }}
    });


    $( "#myLow" ).keyup(function() {
    nLow = $( "#myLow" ).val();
    if (nLow >= <?php echo $latestprice; ?>

) {
    document.getElementById('play').play();
    }else
    {
    document.getElementById('play').pause();
    }
    })


    $( '#cleara' ).click(function() {
            $('input:text').val('');
        });






    });
    </script>


1511128887
Hero Member
*
Offline Offline

Posts: 1511128887

View Profile Personal Message (Offline)

Ignore
1511128887
Reply with quote  #2

1511128887
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1511128887
Hero Member
*
Offline Offline

Posts: 1511128887

View Profile Personal Message (Offline)

Ignore
1511128887
Reply with quote  #2

1511128887
Report to moderator
1511128887
Hero Member
*
Offline Offline

Posts: 1511128887

View Profile Personal Message (Offline)

Ignore
1511128887
Reply with quote  #2

1511128887
Report to moderator
Moloch
Hero Member
*****
Offline Offline

Activity: 686


Moloch.net


View Profile WWW
February 26, 2016, 06: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:
<script>
var latestPrice = <?php echo $latestPrice?>;
</script>

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());
});

spy100
Sr. Member
****
Offline Offline

Activity: 240


View Profile
February 28, 2016, 05: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:
<script>
var latestPrice = <?php echo $latestPrice?>;
</script>

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 = <?php echo $latestPrice; ?>;

you will not get updated price until page refresh.I want the variable to update with price change without refresh

benATstib
Jr. Member
*
Offline Offline

Activity: 48

Founder/CEO of StiB.co


View Profile WWW
March 02, 2016, 08:49:36 PM
 #4

Edit for a better title and we will get more responses!

Founder/CEO of StiB.co, Free Escrow!
spy100
Sr. Member
****
Offline Offline

Activity: 240


View Profile
March 05, 2016, 06:23:06 PM
 #5

up

music8mycomputer
Jr. Member
*
Offline Offline

Activity: 51


View Profile WWW
March 05, 2016, 07:01:21 PM
 #6

Not sure if this will help but this is how I get a changing php value into javascript.

Code:
<script>
function latestprice() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("thealert").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "alertprice.php?random=" + Math.random(), true); // random for IE
xhttp.send();

setTimeout(latestprice, 2500);
  
}
latestprice();
</script>


<div id="thealert">453.33</div>


<?php // 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;
bitspill
Legendary
*
Offline Offline

Activity: 1484



View Profile
March 06, 2016, 11: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);

{ BitSpill }
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!