SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Getting data from PHP into JavaScript

    I need to transfer up to some 15 items of data from PHP into JavaScript. I've normally done this by creating hidden inputs with PHP and then using 'getElementById' to extract the value. But it's getting a bit cumbersome, with loops to create the inputs, and more loops to extract. It works, but I wonder if there's a better way ?

    I have the information available as a GET string. There are (three) sub-arrays in the GET, and the number of values which may be present in each sub-array varies from 0-9. The GET array keys are strings, but the sub-array keys are numeric.

    Here's a typical GET string:
    Code:
    ?ac[1]=HO&ac[2]=GH&ac[3]=BB&loc[1]=NW&loc[5]=TB&fac[1]=WiFi&fac[8]=card&accn=Search
    The GET is derived from check-boxes on a search filter form, and the purpose of all this is to transfer the checks from one page to the next (which has the the search results) so that the filter form can be re-presented.

    Any suggestions, please ?
    Tim Dawson
    Isle of Mull, Scotland

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just want to have a way to supply javascript with some data, take a look at php's json_encode() function. Then you have an object/array structure to use.

    But, it sounds like you shouldn't be using javascript for this. Why don't you have php just generate the appropriate html? It's a very common thing to prepopulate values for form fields, including state flags like checked and selected etc...

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply. I'll look into JSON.

    I believed I needed to use JavaScript because the values I'm bringing forward are those entered by the visitor. The search form is currently 'pure' HTML. I expect I could rewrite it with PHP to set 'checked' as per the GET string, I just hadn't thought of it. I guess I should have done, but sometimes one gets a bit blinkered.

    That's what forums are for ! Thanks again.
    Tim Dawson
    Isle of Mull, Scotland

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You need to use a server side language for this or it will not work for those visitors without JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you. Yes, I recognise this, but there are plenty of other things that don't work without JS (like being able to roll up the search filter forms). It's a matter of where you draw the line.

    However, as this can be done with PHP, that's what will have to happen.
    A prototype may be seen at http://www.holidaymullandiona.com/index.php Should you go there, please note that only the accommodation search has been implemented.
    Tim Dawson
    Isle of Mull, Scotland

  6. #6
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, it wasn't nearly as difficult as I thought it would be to do it all in PHP. Thank you for prodding me in that direction. (The prototype site is still working with JS, but will get fixed in the next couple of days).
    Tim Dawson
    Isle of Mull, Scotland


Bookmarks

Posting Permissions

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