Results 1 to 7 of 7

Thread: JavaScript pass variable to PHP and back?

  1. #1
    aka .:iGi:. Calcutter DannyM's Avatar
    Join Date
    Feb 2007
    Location
    Location Location!
    Posts
    915
    Thanks
    111
    Thanked
    125 times in 97 posts
    • DannyM's system
      • Motherboard:
      • Gigabyte Z68MA-D2H-B3
      • CPU:
      • Intel Core i5-2400
      • Memory:
      • 8GB Corsair Vengeance DDR3 - PC-12800
      • Storage:
      • 120GB A-Data SSD
      • Graphics card(s):
      • 1GB Nvidia ASUS 560Ti DirectuII
      • PSU:
      • Corsair 620W HX Modular PSU
      • Case:
      • Fractal Design Define Mini
      • Operating System:
      • Windows 7 Pro 64bit
      • Monitor(s):
      • 23" Dell UltraSharp U2311H
      • Internet:
      • 50Mb Virgin Media Cable Broadband

    JavaScript pass variable to PHP and back?

    I'm currently working on a project and I'm a little lost, I've done simple development in JS and PHP before.

    My issue is this:

    I have a webpage, solely based on JS, I need to take two pieces of information, one piece is provided by the current system, however the other piece of information needs to be taken manually from the user.

    I then need to take the piece of information provided by the user, do a database read find the values that match that piece of information, then I need pass the information from the database read back into my JS.

    So basically: JS var -> PHP variable -> PHP MySQL read - if true store values -> pass values to JS

    Now instantly see a problem in that, JS is client side and PHP is server side, so I imagine it's not possible to do on a static page. (unless I use AJAX but I have no experience with AJAX)

    Advice would be apprciated

  2. #2
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: JavaScript pass variable to PHP and back?

    What are you using the js for in the original page? I assume you've got some HTML in there as well, otherwise - web design, you're doing it wrong

    The simplest solution would be to use an AJAXy solution where the user inputs their value and the JS uses an XMLHttpRequest to get either XML or JSON (personally I'd recommend JSON because it's (IMNSHO, of course) a lot easier and quicker to implement for a simple task) from a php script. If you're doing interesting things on the page with javascript already you'll find that pretty simple, and if you only need a relatively simple piece of information from the server it's probably the better way to do things. There's plenty of advice online about doing simple AJAX retrievals, and it *is* a very simple code model: I picked it up in an hour or so when I first had to use it!

    An alternative to using AJAX / JSON is to create a dynamic javascript. That is, you use js to create a "script" element, set its source property to the php script, and have the php create a short javascript that creates and assigns variables, which can then be used in the page. It's a bit trickier and messier than using AJAX though, as you need to use callback methods to ensure the script has loaded fully before you can process the data you've retrieved from the server

    Two other options spring to mind. You could rewrite the whole thing to do the dynamic stuff in php: it kind of depends on what the javascript in your page is actually doing. Or you could implement a handling script and only use it for this one task, but that would mean reimplementing some parts of the page in php anyway. Without knowing more about the structure of the pages you're working with it's hard to give more definite advice. As I said earlier, refering to a page as being "based solely on JS" rings a little alarm bell, but that could just be a confusion over wording. My guess is that AJAX is the right solution for you, and as I say read up quickly on JSON which is (again, IMNSHO) much easier and quicker to create manually than XML, and also has simpler, lightweight, open-source parsers available.
    Last edited by scaryjim; 16-03-2011 at 02:45 PM.

  3. Received thanks from:

    DannyM (16-03-2011)

  4. #3
    aka .:iGi:. Calcutter DannyM's Avatar
    Join Date
    Feb 2007
    Location
    Location Location!
    Posts
    915
    Thanks
    111
    Thanked
    125 times in 97 posts
    • DannyM's system
      • Motherboard:
      • Gigabyte Z68MA-D2H-B3
      • CPU:
      • Intel Core i5-2400
      • Memory:
      • 8GB Corsair Vengeance DDR3 - PC-12800
      • Storage:
      • 120GB A-Data SSD
      • Graphics card(s):
      • 1GB Nvidia ASUS 560Ti DirectuII
      • PSU:
      • Corsair 620W HX Modular PSU
      • Case:
      • Fractal Design Define Mini
      • Operating System:
      • Windows 7 Pro 64bit
      • Monitor(s):
      • 23" Dell UltraSharp U2311H
      • Internet:
      • 50Mb Virgin Media Cable Broadband

    Re: JavaScript pass variable to PHP and back?

    Sorry, I'd assume people would of known what I mean about the JS comment, but I gather, you may get a few people who don't know you need HTML to display JS on a page.

    I'm using the sencha framework, and for the sake of examples, this is the original framework I am using: http://finalproject.majord.me.uk/examples/map/

    As you can see, it's a simple HTML file using a few JS files and the Google Maps JavaScript API.

    I am wanting to give directions from one destination to another, for the basic plan, the original start point will be provided via GPS on the mobile device, but it's the destination I want the user to provide and then do a db query to get the specific coordinates (I will get coordinates for each location and store in a DB).

    The JSON approach does seem best, but if you know of any other way based on this information provided, I would appreciate insight.

  5. #4
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: JavaScript pass variable to PHP and back?

    Quote Originally Posted by DannyM View Post
    Sorry, I'd assume people would of known what I mean about the JS comment, but I gather, you may get a few people who don't know you need HTML to display JS on a page.
    Hmmmmm. That page isn't really HTML though, is it: it's a body tag with all the content dynamically generated by Javascript (and technically you're not displaying the javascript, you're dyanmically manipulating the DOM with javascript to display other elements, but that's being exta-picky on my part ). And this is kind of what I was worried about. Your actual page will need some way of getting information from the user, which means working something into the framework to display form elements, at the very least. I assume that's possible, but having not worked with the framework directly I don't know how easy you'll find it. Given that everything's based on a js/DOM approach to display (rather than HTML), you're probably better off asking questions at forums targetted specifically at the sencha framework, rather then general web design forums.

    That said: based on your requirements, creating an XMLHttpRequest and using it to obtain JSON from a php script on the server would definitely be the way to go. It will give you by far the most straightforward job in terms of writing the code. JSON.org used to host a sample parser themselves, but they now seem to be recommending Douglas Crockford's JSON parser. However, you'll have to hunt through your framework very carefully to make sure that the javascript you write to handle the AJAXy side isn't going to conflict in any way with the dynamic-display code that handles the rendering.

  6. Received thanks from:

    DannyM (18-03-2011)

  7. #5
    aka .:iGi:. Calcutter DannyM's Avatar
    Join Date
    Feb 2007
    Location
    Location Location!
    Posts
    915
    Thanks
    111
    Thanked
    125 times in 97 posts
    • DannyM's system
      • Motherboard:
      • Gigabyte Z68MA-D2H-B3
      • CPU:
      • Intel Core i5-2400
      • Memory:
      • 8GB Corsair Vengeance DDR3 - PC-12800
      • Storage:
      • 120GB A-Data SSD
      • Graphics card(s):
      • 1GB Nvidia ASUS 560Ti DirectuII
      • PSU:
      • Corsair 620W HX Modular PSU
      • Case:
      • Fractal Design Define Mini
      • Operating System:
      • Windows 7 Pro 64bit
      • Monitor(s):
      • 23" Dell UltraSharp U2311H
      • Internet:
      • 50Mb Virgin Media Cable Broadband

    Re: JavaScript pass variable to PHP and back?

    Google and you seem to agree that JSON would be the best way to go.

    The framework does support the displaying of HTML but I'm not sure if it supports HTML forms.

    There is however a form system in place that stores the forms data into variables, so using that I imagine it wouldn't be too difficult to pass on the value gathered from the form, use JSON to retrieve the necessary data and then pass it back into the variable.

  8. #6
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: JavaScript pass variable to PHP and back?

    Code:
    var myVar = 'foo';
    $.ajax({
       data: myVar,
       url: '/mypage.php'
    });
    http://jquery.com/
    To err is human. To really foul things up ... you need a computer.

  9. Received thanks from:

    DannyM (18-03-2011)

  10. #7
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: JavaScript pass variable to PHP and back?

    Using a 29k library to do something that takes 15 lines of code seems a little excessive to me, though. If your existing framework already uses jquery then fair enough; personally I wouldn't recommend adding an entire library for one function call...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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