#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    49
    Rep Power
    14

    Question passing var from javascript to PHP??


    Is it possible to pass a variable such as an argument at the end of a URL to PHP from javascript???

    Here is what I need to do:
    I am using the <SCRIPT LANGUAGE="JavaScript" SRC="populate.js"></SCRIPT> on a regular HTML page. On the populate.js I want to hit a database using PHP and grab some info then write that info into javascript format as an array. That array will then be used to populate the javascript on the regular HTML page.

    So what I am trying to do is pass a variable from the regular html page to the populate.js so that I will be able to use that variable to hit the database and get the necessary info and then use that necessary info to populate the javascript array. (taking a deep breath )

    Any help will be appreciated.

    Vince
    Last edited by phpnewbie3; July 4th, 2001 at 04:25 PM.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    465
    Rep Power
    13

    ho well


    You can always try to create hidden forms and set their values with js. Perhaps that if you create, let's say, one text box you could append the variables the same way it does with the GET-method URL style and use str_parse(), *I think*, to get the vars once your inside PHP.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    13
    Greetings Newbie...

    I am using the <SCRIPT LANGUAGE="JavaScript" SRC="populate.js"></SCRIPT> on a regular HTML page. On the populate.js I want to hit a database using PHP and grab some info then write that info into javascript format as an array. That array will then be used to populate the javascript on the regular HTML page.
    Yeah I've done some stuff with this and a bunch of cool dudes helped me from Devshed. Okay one thing you could do is this:

    populate.js:
    document.write("<SCR" + "IPT SRC='populate.php' type='text/javascript'> <\/SCR" + "IPT>");
    And then populate.php is your databasing code.

    This should do the trick. You were always going to have to call a PHP page because otherwise the server wouldn't know what to do with it. There's more you can do with this combo, but give that a try first. Remember JS evaulates client side and PHP server side so you have to be careful when using them together.

    Hope this helped,
    Zitan.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    13
    Vince,

    So what I am trying to do is pass a variable from the regular html page to the populate.js so that I will be able to use that variable to hit the database and get the necessary info and then use that necessary info to populate the javascript array.
    You can pass PHP variables back into Javascript with a echo... populate.js(<?echo("$var_to_Pass)";?>. But once again be careful because you'll find unusal things happen because the server does all the PHP first, and then does the JS when its gets to the browser.

    Z.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    49
    Rep Power
    14
    Hi Zitan,
    Thanks for your reply. Here is what I did:

    On the regular HTML page, I have this code:
    <SCRIPT LANGUAGE="JavaScript" SRC="populate.js"></SCRIPT>

    On the populate.js, I placed your code:
    <SCRIPT LANGUAGE="JavaScript1.2">
    <!--
    document.write("<SCR" + "IPT SRC='populate.php' type='text/javascript'> <\/SCR" + "IPT>");

    // -->
    </SCRIPT>


    and on the populate.php I have it echo this javascript code by hard coding the CustID when I hit the database, I will worry about passing the variable in later:

    <script language="javascript">
    <!-- Hide from browsers
    var ar = new Array();
    ar[0] = "Get a <a href='http://www.seesantabarbara.com'>SeeSantaBarbara</a> business page today and receive an enhanced listing for just $9.95 per month.<br>";// end hiding -->
    </script>

    I need to get the Array "ar" from the populate.php page back to the regular html page. but it doesn't seem to be working. I am receiving the javascript error "ar is undefined" on the regular HTML page.

    Any help will be appreciated.

    Vince
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    13
    Okay Vince,

    You can't pass variables from javascript to PHP, because the PHP is all finished before the JS even starts (assuming var ar is defined in populate.php). You can however pass the Javascript Array into PHP.. and then Output from PHP to Html. Could you do the array stuff in PHP as well? If the information you populating the array with is from database, you don't even need JS here... just output straight from PHP to HTML... but I could be misunderstanding the problem ....

    If you tell me a little more about what the purpose of Javascript is here I might be able to help more, or not because I'm still just a newbie myself !

    Z.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    49
    Rep Power
    14
    Unfortunetly, I am limited to what I can say because my client has me under a strict confidentiality agreement.
    I can tell you this: the regular HTML page will be located on many different web sites(affiliate sites), all the other pages will be located on my clients server including populate.js and populate.php.

    The regular html pages will have to hit my clients server in order to populate the content on their pages. It would be similar to a news web feed to a site. Where my client is providing the news and the regular HTML pages will be displaying the news. But the news will be different on each site based on their preferences.

    This is the first time I have used javascript with PHP and it is killing me. Here is one client I should have turned down.

    Vince
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    13
    Hey Vince,

    All good man... yeah I did it to myself I though hey it would be cool if I did this with Javascript and then used PHP for this... it can be a nightmare but once I understand what happened when it made things a lot easier.

    Okay back to it: I still don't know why you need Javascript. You have the html forms on other sites which all loadup to your client's server. Why are you using that JS array, what we need to do is think of a way around that. Are you using MySQL and Html forms with this solution?

    If your really tied to the JS array you could load the javascript array into a hidden form in propogate.js which would then load into propogate.php. However I'd still recommend using PHP for the array stuff if you can.

    Keep it coming,
    Z.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    49
    Rep Power
    14
    I still don't know why you need Javascript. You have the html forms on other sites which all loadup to your client's server.

    ************************************************

    My client doesn't want the information to be displayed in html form textboxes, he wants it on the page like regular text, that is why I am using javascript to write the content directly to the other sites browsers.
    Unless I am missing something here. Is it possible to write dynamic content to another website without using javascript?

    Vince
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    13
    Is it possible to write dynamic content to another website without using javascript?
    Greetings Vince,

    Sorry but I haven't needed to do that before so maybe someone else can answer this question. Sorry to waste your time on that one.

    So are you okay getting information from PHP to Javascript (echos and such) its just getting information into PHP from Javascript right?

    The last problem was that the "ar" array couldn't load variables into PHP. You could do this by loading the array into a Hidden html form and submitting this to the PHP page. Also the Propogate.js could call the php page with some vars on it like propogate.php?ar[1]&ar[2] but I don't think that's real efficient. Where does your array data come from?

    Z.

IMN logo majestic logo threadwatch logo seochat tools logo