Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I was going to use php, but they told me I needed java

    Any help would be nice.

    I want to make a box on my site that shows a current number for all site members. This number is stored in a mysql table, and I need it to update automatically when it is changed.

    I started asking the php guys, but they told me I needed java to make it update without the user updating their page.

    Is there a simple java code to do this?

    This is more in depth:

    http://www.codingforums.com/showthre...396#post328396

    Thanks

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    think you need to look at

    http://www.i18nguy.com/markup/metatags.html

    no Cache

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for pointing me in the right direction.

    I am now thinking I will just display the value as text by taking it from mysql and making it a php variable, then make it so the page cannot be cached so that when they refresh it will not be a saved/old number.

    It is very important the users see what the current number is. The above plan I wrote will make it so the number is current from when they refresh the page, but I would like it so that the number can update on the page without the refreshing the page.

    I could make the entire page automatically refresh, but is there anyway to only have the value of the number update every second, or everytime it changes?

    As I typed that I wanted the number to change with out the page refreshing, I discovered the plan at the top of using php wouldn't work b/c as someone told me in:

    http://www.codingforums.com/showthre...323#post328323

    "php is a static language, so you cannot create new variables to send until the users page updates."

    Any ideas?

    Thanks

    Edit: Is this even possible to do with javascript?... Query a mysql table for a value and display it everytime it changes. Because I need the number amount stored in the database to be read and updated if changed, but I thought php was the only thing that can query the database, and it can only do that by refreshing the page.

    Edit: edit: I found this page:

    http://www.webdeveloper.com/forum/sh...ad.php?t=70054

    Where if I understood the guy, he wasnted to do the exact same thing as me,(load text from mysql dynamicly without the page refreshing).

    The other people pointed him to: XMLHttpRequest

    Can anyone else verify if this is what I want? (XMLHttpRequest)
    Last edited by cowkiller6; 07-02-2005 at 05:46 PM.

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    xmlhttprequest is the way to go, if u want to update only a certain part of a page n not refresh the entire page.

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, thanks. I am trying to learn it right now...

    Thank you

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Step 1: Make sure that all your MySQL stuff is working (which I'm sure it is)

    Step 2: Write a PHP function that reads the value from the table and returns the value when you call the function.

    Step 3: Use the uber-simple Sajax library to have the value auto-update every 60 seconds or so.

    Oh, so simple.

  • #7
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for that list. I am now on step 3.

    However, I have no idea what to do with SAJAX. I downloaded it, and have pulled up the SAJAX.php file that was in the php folder, however I do not understand how I can use this to make the value autoupdate (I need it for every second or less).

    Thanks

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just visited the Sajax library page and, without javascript enabled, their multiplier doesn't work. So, I figure javascript would be a fine substitution for anything they have. I also had some other thoughts on this topic. If cowkiller6 puts his code from step 2:
    Quote Originally Posted by Skyzyx
    Step 2: Write a PHP function that reads the value from the table and returns the value when you call the function.
    on a separate page (let's call it count.php) displayed on his page(s) through an iframe or in a frame. He should be able to use a meta tag on count.php:
    Code:
    <META HTTP-EQUIV = "REFRESH" CONTENT = "1">
    so that it reloads every 1 second. Finally, he could include a script in the head of count.php:
    Code:
    <script type="text/javascript">
    setTimeout("window.location.href=window.location.href",750)
    </script>
    This will reload the page every 3/4 second, making the meta tag a backup for non javascript enabled browsers.

  • #9
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscheuer1
    ...without javascript enabled, their multiplier doesn't work.
    Well, duh. It's an Ajax library. (didn't mean to be rude there ).

    If you go through the instructions at http://www.modernmethod.com/sajax/faq.phtml , it walks you through how to make your PHP function (from step 2) accessible from JavaScript, which you can then write to an element (using innerHTML) every so-many milliseconds.

    This way, you don't have to refresh the page constantly (which is a horrible, horrible, horrible practice), and the counter stays up-to-date constantly.

  • #10
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, that will help me. I will try it all out in around 2 hours when I get home.

    thanks

  • #11
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Skyzyx
    Well, duh. It's an Ajax library. (didn't mean to be rude there ).

    If you go through the instructions at http://www.modernmethod.com/sajax/faq.phtml , it walks you through how to make your PHP function (from step 2) accessible from JavaScript, which you can then write to an element (using innerHTML) every so-many milliseconds.

    This way, you don't have to refresh the page constantly (which is a horrible, horrible, horrible practice), and the counter stays up-to-date constantly.
    Well, I still do not really understand the Sajax stuff. However I think I manually did whatever it does. Now I have: an html page that when you perform an action it does the xmlhttprequest function

    example: onblur="updateCityState();"

    This function uses a php file and gets the current number from the database.

    Now, I need to change the way the function is called. Removing the onblur, and making it do the function every... say .75 seconds.

    You told me to:

    "then write to an element (using innerHTML) every so-many milliseconds"

    Is there a simple way to do this given my functions name is function updateCityState()???

    Thank you!

  • #12
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm all for xmphttprequests, but for something this small I think you'd be better of with dynamic script elements.

    Here an example of how it works:
    test.html
    Code:
    <html>
    <head>
    
    <script id="dynamic_script" src="update.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    function update(){
      var old=document.getElementById("dynamic_script");
      var s=document.createElement("script");
      s.id="dynamic_script";
      s.type="text/javascript";
      s.src="update.js?"+new Date().getTime();
      old.parentNode.replaceChild(s,old);
      }
    
    setInterval("update()",1000);
    
    </script>
    
    </head>
    <body>
    <div id="out">&nbsp</div>
    </body>
    </html>
    update.js
    Code:
    if(document.getElementById("out"))
      document.getElementById("out").firstChild.nodeValue=Date();
    The static .js file I've used is pretty pointless -- in reality you'd set the src to point to a php script that grabbed your value from the db and inserted that value where I currently have Date(). This nice thing about this technique is that it doesn't require ActiveX to work with IE. And the freshly fetched script element simply runs its content script as soon as it loads. No waiting around like with synchronous xmlhttprequests, no messy callback functions like with async xmlhttprequests.

    If you want to send data to your server, you can add it to the query part of src URL (I've used a timestamp to eliminate caching); if you want to change more of the page, return more script.

  • #13
    New Coder
    Join Date
    Jun 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got it to continuously update by adding the line:

    setInterval("updateCityState()",1000);

    updateCityState is my function.
    However, doing it this way, it updates once every second, but it blinks. Is there another way to do this so it doesn't blink. Also, I tried the above approach and got it to show the date but at the bottom of the screen in the toolbar it says transfering data to... and blinks like that.

    So, is there anyway to make this update without blinking. When I change the time from 1000 to about 100, you cannot even see the number b/c it blinks too fast.

    This is my html file:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>ZIP Code to City and State using XmlHttpRequest</title>
    <script language="javascript"  type="text/javascript">
    var url = "getCityState.php"; // The server-side script
    function handleHttpResponse(){ // Split the comma delimited response into an array 
    results = http.responseText; 
    document.getElementById('state').value = results; 
    }
    function updateCityState() {
    http.open("GET", url, true); 
    http.onreadystatechange = handleHttpResponse; 
    http.send(null);
     }
     setInterval("updateCityState()",1000); 
    
     
    function getHTTPObject() {
      var xmlhttp;
      /*@cc_on
      @if (@_jscript_version >= 5)
        try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (E) {
            xmlhttp = false;
          }
        }
      @else
      xmlhttp = false;
      @end @*/
      if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
          xmlhttp = new XMLHttpRequest();
        } catch (e) {
          xmlhttp = false;
        }
      }
      return xmlhttp;
    }
    var http = getHTTPObject(); // We create the HTTP Object
    </script>
    </head>
    <body>
    <form action="post">
      <p>
      ZIP code:
      <input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
      </p>
      City:
      <input type="text" name="city" id="city" />
      State:
      <input type="text" size="2" name="state" id="state" />
    </form>
    </body>
    </html>
    and this is the php file:

    PHP Code:
    <?php
    /**
    * Connects to the database.
    * Return false if connection failed.
    * Be sure to change the $database_name. $database_username , and
    * $database_password values  to reflect your database settings.
    */
    function db_connect() {
      
    $database_name ='----'// Set this to your Database Name
      
    $database_username ='-----'// Set this to your MySQL username
      
    $database_password ='-----'// Set this to your MySQL password
      
    $result mysql_pconnect('localhost',$database_username$database_password);
      if (!
    $result) return false;
      if (!
    mysql_select_db($database_name)) return false;
      return 
    $result;
    }
    $conn db_connect(); // Connect to database
    if ($conn) {
      
    $query "SELECT * FROM Bank WHERE id = '1'";
      
    $result mysql_query($query,$conn);
      
    $count mysql_num_rows($result);
      if (
    $count 0) {
        
    $city mysql_result($result,0,'amount');
      }
    }
    if (isset(
    $city)) {
      
    $return_value $city;
    }
    else {  
      
    $return_value "invalid"// Include Zip for debugging purposes
    }
    echo 
    $return_value// This will become the response value for the XMLHttpRequest object
    ?>
    Thank you
    Last edited by cowkiller6; 07-03-2005 at 05:42 AM.

  • #14
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Echo the value into the javascript
    Code:
    if(document.getElementById("out"))
    document.getElementById("out").firstChild.nodeValue=<?php echo $amount; ?>;
    An external javascript file doesn't have the <script ... > and </script> tags inside it.

    It's also worth setting the proper content-type header at the beginning of your script:

    header("Content-type: text/javascript");

    Code:
    <?php
    header("Content-type: text/javascript");
    
    /**
    * Connects to the database.
    * Return false if connection failed.
    * Be sure to change the $database_name. $database_username , and
    * $database_password values  to reflect your database settings.
    */
    function db_connect() {
      $database_name ='test'; // Set this to your Database Name
      $database_username ='---------'; // Set this to your MySQL username
      $database_password ='---------'; // Set this to your MySQL password
      $result = mysql_pconnect('localhost',$database_username, $database_password);
      if (!$result) return false;
      if (!mysql_select_db($database_name)) return false;
      return $result;
    }
    $conn = db_connect(); // Connect to database
    if ($conn) {
      $query = "SELECT * FROM Bank WHERE id = '1'";
      $result = mysql_query($query,$conn);
      $count = mysql_num_rows($result);
      if ($count > 0) {
        $amount = mysql_result($result,0,'amount');
      }
    }
    ?> 
    if(document.getElementById("out"))
      document.getElementById("out").firstChild.nodeValue=<?php echo $amount; ?>;

  • #15
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks like cowkiller6 is getting things sorted out with lots of help, just wanted to reassure Skyzyx that you have to do more than tell me that something that sounds like a kitchen cleanser, is javascript, to piss me off.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •