Jump to content

Refreshing Variables On Screen


Distortion
 Share

Recommended Posts

This is probably a simple question. But I have no idea what keywords to use in google so had to post here.What I'm trying to do is this:I want to show a variable in a loop. In that loop the variable changes untill the user ends the loop.Now I want to refresh the variable but not the whole page. So obviouslywhile 1 {print("variable: $value");$value = $value + rand(2,5);}will not work. What will?

Link to comment
Share on other sites

You can't do that - PHP is server side and resolves before the user gets to see the output of it. You can't interact with it unless it's by calling a script and sending it some kind of input. I guess you could recursively refresh a page that had the php at the top and use javascript to detect user input and AJAX to send that input to the php script.

Link to comment
Share on other sites

Is there something special about the variable that you couldn't do this whole thing in JavaScript? You'll be using JavaScript to run your loop and update the screen, anyway. The way you change the variable's value (that little randomizing thing) can certainly be done in JavaScript.Could you use PHP to embed the initial value of the variable in a javascript script and then let javascript take over when the document arrives at the browser? (Where do you get the initial value of this variable?) Could the initial value itself be generated by JavaScript?When you write "the user ends the loop" what do you mean? The user submits a form, or something else?The only reasons for the browser to interact with the server are to submit data and to fetch data. And the only reason to fetch data after a page has downloaded is because it's stored in a database so big (or so dynamic) that you can't embed the whole thing in your document to begin with. Sending a special request to your server to increment a variable is like visiting a doctor to blow your nose. Too expensive, too slow, and more easily done at home.EDIT: or is this a school exercise and you're supposed to be learning AJAX?

Edited by Deirdre's Dad
Link to comment
Share on other sites

Oh shoot, I could've made that up myself. Now you said it like this I feel a bit stupid.Ok here is the whole plan:I want to make a page where you see fake values of stocks go randomly up and down.When you think it is the right time to sell or buy them you click somewhere and it sells or buys them.I wanted to do that with php by sending the costs to the next page with an url and then checking with the database wether you had enough money and stocks. Well then, I learned some java at school. But I've been told that a java applet is very different from javascript. Also I did'nt know that java could interact with my database. My question is, where do I use javascript and where do I use other languages, and where can I learn about interacting between them?PS: The website is not for school, I'm making a simple browserbased game as a practise for making a better site with a friend. I already made the login/register system and a message system with php.EDIT: The initial values can be generated randomly in javascript.

Edited by Distortion
Link to comment
Share on other sites

Java and JavaScript are two completely different languages. You use JavaScript when you want to modify the page instantly, or provide a dynamic effect. You use a server-side language when you want to interact with server-side resources or generate actual pages. You can use a JavaScript technique known as AJAX to send HTTP requests in the background.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...