Author Topic: Passing variables....  (Read 658 times)

Offline seafarer

  • Level 3
  • *
  • Posts: 8
  • Reputation: +0/-0
    • View Profile
Passing variables....
« on: February 17, 2011, 07:10:36 PM »
so, I'm exploring a new method of refreshing data through js, rather than running ajax/mysql queries every second.  However, in order to do so, I need help solving this problem:

I have ajax call a php file from my HTML page, which echos info from my db.  I need to pass variables from this query to JS, so I can have JS give me a streaming calculation on this data.  I really don't want to use the $_GET function, because I don't like the idea of the address bar being all cluttered with garbage.
 
What is another way I can pass variables between pages and languages?

Thanks!

Offline CygnusX

  • Level 24
  • *
  • Posts: 304
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: Passing variables....
« Reply #1 on: February 17, 2011, 07:31:05 PM »
This isn't my specialty, but doesn't JSON do this?

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Passing variables....
« Reply #2 on: February 17, 2011, 10:59:31 PM »
use jquery, and then post it? Or us the browsers native api yourself. I personally would just end up using jquery for everything and then go from there since it'd be a lot easier. Also passing the variables as a post would prevent you from having to use get.

Offline seafarer

  • Level 3
  • *
  • Posts: 8
  • Reputation: +0/-0
    • View Profile
Re: Passing variables....
« Reply #3 on: February 18, 2011, 08:26:25 AM »
Can I use a post, if the variables are coming from PHP?  Basically, I'm going to run a php/mysql query, and assign variables.  Then I want to tell JS to use those variables and perform calculations...

Offline CygnusX

  • Level 24
  • *
  • Posts: 304
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: Passing variables....
« Reply #4 on: February 18, 2011, 09:23:25 AM »
There are many ajax/json/etc technologies that can do this.  If you use jQuery, it looks like this:

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
    myvar = msg;
   }
 });

You would place this code on the page sent to the user, and it will call the script/page that queries the db, and in echoing the result, would send the information back to JS.

I would not rely 100% on this code, but its pretty close to what you need. I am admittedly not a JS expert...

See here for more details:

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

Offline DV8

  • Level 10
  • *
  • Posts: 63
  • Reputation: +0/-0
    • View Profile
    • Shadowrun: Corrosion
Re: Passing variables....
« Reply #5 on: February 18, 2011, 10:18:47 AM »
Why are you going from one PHP page to another PHP by way of the client? Can't you just include the second page in the first, pass the variables on the server side and then send the response back to the client?

Offline CygnusX

  • Level 24
  • *
  • Posts: 304
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: Passing variables....
« Reply #6 on: February 18, 2011, 10:32:30 AM »
In jQuery/ajax/etc, you're not really 'going' anywhere.  The jQuery call runs in the background.  When a user loads a page, an event call (such as a link click, timer countdown, etc on that page) tells jQuery to ask the server for new information.  It can then either display that information it receives, store it in a variable or do nothing.  Either way, the user doesn't have to do anything, and it is possible to update information on the screen without a page change.

There are plenty of demos/tutorials/etc on how this works.  jquery is the technology I'm most familiar with.

« Last Edit: February 18, 2011, 10:35:02 AM by CygnusX »

Offline Barrikor

  • Level 21
  • *
  • Posts: 248
  • Reputation: +3/-0
    • View Profile
Re: Passing variables....
« Reply #7 on: February 18, 2011, 12:15:13 PM »
If all you need to do is pass PHP vars to JS, you can always resort to having PHP echo new JS code into the page.

Code: [Select]
<?php
echo '
<script type="text/javascript">
foo = 3;
bar = 7;
</script>
'
;
?>


Or you could have hidden fields somewhere on the page for JS to take data from, and you could have Ajax (maybe using jQuery's .load() method) overwrite the hidden fields when needed.

Or you could write a function in JS: setTheseVarsPlease(var1,var2,var3,var4,var5,var6,var7), and when you use Ajax have PHP echo a JS call to it, maybe you could have it call other JS functions to update stuff using the new values of the vars
« Last Edit: February 18, 2011, 12:42:35 PM by Barrikor »
Projects: Pith Framework (at 0.5), CactusGUI (at 0.3)

Offline DV8

  • Level 10
  • *
  • Posts: 63
  • Reputation: +0/-0
    • View Profile
    • Shadowrun: Corrosion
Re: Passing variables....
« Reply #8 on: February 18, 2011, 02:16:20 PM »
In jQuery/ajax/etc, you're not really 'going' anywhere.
I realise that. I was left with a sense that a PHP script was being run on the server that produced some variables that then would be pushed to the client and made part of a jQuery call to another PHP page, which seemed rather redundant to me. I might be completely off-base, though. :)

Offline CygnusX

  • Level 24
  • *
  • Posts: 304
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: Passing variables....
« Reply #9 on: February 18, 2011, 02:24:21 PM »
I c.  Thats definitely not what I was trying to describe.   ;D

I figured the OP wanted to update the strength of his army (or something similar) every x seconds, which is a complicated function based on a handful of variables.  The idea, I thought, was to offload server processing power onto the clients CPU while providing real time updates.  This would be a fantastic idea if there is a pressing need for it.  The initial variables can be defined in the 'main' php page.  Each update would be done via ajax/jquery/whatever. 

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal