2

How do I reload this input once after 3 seconds has passed after the webpage has loaded. using javascript

<input type="text" name="steamID" id="steamID" value="<?php echo $steamid; ?>">
2
  • 2
    Are you using any Javascript frameworks like jQuery, Prototype, or MooTools? Commented Nov 30, 2012 at 20:01
  • 1
    Please check this answer: stackoverflow.com/a/4671060/563049 Commented Nov 30, 2012 at 20:02

4 Answers 4

4

Try looking at this answer on SO:

Reload random div content every x seconds

If that doesn't work for you, you will have to use ajax to get new content. Look at jQuery's API here:

http://api.jquery.com/jQuery.ajax/

Or if you're not using jQuery, look at this for a tutorial on AJAX:

http://www.w3schools.com/ajax/default.asp

Otherwise, for more help, please post more of your code -- but ajax will have to be used

Sign up to request clarification or add additional context in comments.

Comments

2

If you want the PHP in your code to be run again, you need to make you code a little more complicated.

You will need the following components

  1. a php file that will lookup and print $steamid only.

  2. a javascript function that uses AJAX to get the information from the php file, sets the value of your input

  3. call the javascript on page load, then set an interval for 3 seconds and call it again.

But based on this...

The problem i have that the PHP var $steamid are set after the input has been created so all i need todo is reload the input so the $steamid will show.

... I think you just need to re-order your PHP code.

Comments

1

By reset, I am assuming you mean set the value to null.

$(window).load(function(){
   setTimeout( function() { 
    document.getElementById("steamID").value = "";
    },3000);
});

EDIT: Based on your further description, wait until steamID is set, then put this on the page:

<script type="text/javascript">
  document.getElementById("steamID").value = "<?php echo $steamid; ?>";
</script>

5 Comments

How can i make so the .value = ""; will change the value to the PHP var $steamid
You will have to do an ajax call
If you want it to be set to the intial value, you could set a javascript variable to the value and use it in the layout I specified above.
@JamesKleeh Now that's a good idea/point. (S)he doesn't really say if the value could change on the server side between the page initial load and reload (or why it's being reloaded), but if that would be acceptable, it would sure be the easy way to go.
The problem i have that the PHP var $steamid are set after the input has been created so all i need todo is reload the input so the $steamid will show.
0
<?php
    echo "<script type='text/javascript'>";
    $steamid = "testing 1,2,3";
    echo "    var sID = '".$steamid."';";    
?>
    function setupRefresh() {
       setInterval("refreshVal()", 3000);
    }
    function refreshVal() {
       var e = document.getElementById('steamID');
       e.value = sID;   
    }
</script>

3 Comments

I should make me a macro for this...^^ Don't use strings as the first parameter of setInterval/setTimeout developer.mozilla.org/en-US/docs/DOM/window.setInterval
@Andreas in Firefox removing quotes throws an error: "useless setInterval call (missing quotes around argument?)"
Then you didn't follow the link... Just pass the function itself setInterval(refreshVal, 3000) - You should really check the link I've posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.