SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    310
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How do I match PHP and JavaScript values

    Hi,

    I've picked up a PHP value at random and I want to match it with the value in my Javascript array set. Is there anyway to do this?
    Please advice.

    Thanks

  2. #2
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I follow what you're trying to do.

    I guess a quick-hack could be to have PHP echo some script type="text/javascript" tags with the variable being defined.

    <?php
    // Make random value, x
    bla bla bla

    // Pass on to Javascript
    echo('<script type="text/javascript">var x = ' . $x . ';</script>');
    ?>
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    310
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    That what I want to do. But how do I check with something like this on the javascript side:

    if (x == $x) {
    // continue
    }

  4. #4
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    310
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    OK,
    I get it. sorry for stupid question.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ketting00 View Post
    Thanks,

    That what I want to do. But how do I check with something like this on the javascript side:

    if (x == $x) {
    // continue
    }
    That can't be done. When PHP runs, it builds on the server side the content of the HTML page and then sends the HTML content to the client. When the client runs it runs any javascript that is on the page. There is no communication between PHP and JavaScript.

    It can be possible to use Ajax techniques to send information to another PHP script, and at some later time receive a response, but that tends to be relatively slow. There can be better techniques, if you can share with us what you want to accomplish.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    310
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for suggestion.

    What I am trying to do is a live event (not chat). As you said, using Ajax is slow. So I gather who is online with JavaScript and I want to show them on front page. However, I only want people I set as featured to be appeared on the front page. I've picked them at random of limited number.

    As you can see, the problem is I want to match the IDs I picked with PHP and the IDs I picked with JavaScript to make sure that they are both featured and online.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ketting00 View Post
    Thanks for suggestion.

    What I am trying to do is a live event (not chat). As you said, using Ajax is slow. So I gather who is online with JavaScript and I want to show them on front page. However, I only want people I set as featured to be appeared on the front page. I've picked them at random of limited number.

    As you can see, the problem is I want to match the IDs I picked with PHP and the ID I picked with JavaScript to make sure that they are both featured and online.
    That sounds like a PHP database solution then. Have PHP get from the database a list of featured people who are currently logged in, and use that information when creating the page.
    That's how I'd approach the problem.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    310
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's more complicated. It's a real-time platform and I found JavaScript is far more effective than PHP (PHP can't do time interval). So the heart of the app is JavaScript and I use PHP just because JavaScript can't do it.

    Anyway, with Shaun suggestion I found it's easy to achieve this.


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
  •