/    Sign up×
Community /Pin to ProfileBookmark

Javascript & MySQL

Every visitor on my site gets an automatic MySQL table with stats like screen size, ip, browser etc.

Now everything is saved in the user table, but there’s a little problem.
How can I update a javascript code like screen.width?

[code=php]Update user SET screen=screen.width WHERE ip=$ip;[/code]

That’s not working… Is there a PHP code for it? Or how can I fix it?
Greetz tAK

to post a comment
PHP

23 Comments(s)

Copy linkTweet thisAlerts:
@grailquester5May 22.2005 — JavaScript is a client-side language, PHP/MySQL are server-side, hence they don't speak directly to one another. The only way (I know of) to pass values from JavaScript to PHP is to create an invisible form and populate it with the information that you'd like to pass (or pass it in a URL as in a GET statement and then use PHP to interpret the GET).
Copy linkTweet thisAlerts:
@bokehMay 22.2005 — JavaScript is a client-side language, PHP/MySQL are server-side, hence they don't speak directly to one another. The only way (I know of) to pass values from JavaScript to PHP is to create an invisible form and populate it with the information that you'd like to pass (or pass it in a URL as in a GET statement and then use PHP to interpret the GET).[/QUOTE]
Actually javascript can send information to php behind the scenes without a new page load being necessary.
Copy linkTweet thisAlerts:
@felgallMay 22.2005 — Actually javascript can send information to php behind the scenes without a new page load being necessary.[/QUOTE]

How can it do that when the PHP has finished running before the page reaches the browser and the Javascript doesn't start running until after that and so doesn't even know that there is PHP there to interact with?

You can embed PHP into Javascript so that the PHP can generate the Javascript content but the other way around - I don't think so.
Copy linkTweet thisAlerts:
@grailquester5May 22.2005 — bokeh, I'm sorry, but no it can't. felgall is right - PHP runs first, on the server, and sends data to the browser. PHP can be used to create Javascript (just like HTML), but not vice-versa.

PHP has 3 ways (one which I didn't mention previously) of getting data from Javascript:

  • 1. Use Javascript to set a cookie and retrieve the cookie on another page using PHP.


  • 2. Append variables to a URL with Javascript, then use Javascript to redirect the browser to said URL (or have the user redirect it).


  • 3. Have Javascript set the value(s) of hidden input fields in a form and have the user submit the form.


  • That's all there is to it.
    Copy linkTweet thisAlerts:
    @The_Anime_KingauthorMay 22.2005 — I think I'm going to solve it with this line:
    [code=html]<iframe src=stats.php?screen=<script>document.write(screen.width+"x"+screen.height);</script>&colors=<script>document.write(screen.colorDepth);</script>></iframe>[/code]

    But now I have another question. Is there a way to auto-refresh PHP code?

    Let's say Online() is a function that reads the SQL-database for online visitors, but after ten minutes of reading the info, maybe all visitors are gone and still it gives the same number... Is there a way to autorefresh only a little bit of PHP-code?

    Greetz tAK
    Copy linkTweet thisAlerts:
    @bokehMay 22.2005 — I'm sorry my friends but your technical knowledge is out of date. Full bi-directional communication is possible between javascript and the server (behind the scenes, ie no page loads being involved) with all modern browsers except Opera. If you dont believe me take a look at [URL=http://developer.apple.com/internet/webcontent/xmlhttpreq.html]this[/URL] and [URL=http://www.xml.com/pub/a/2005/02/09/xml-http-request.html]this[/URL] and [URL=http://www.modernmethod.com/sajax/]this[/URL]. [B]The XMLHttpRequest Object[/B]

    This object, first implemented by Microsoft as an ActiveX object but now also available as a native object within both Mozilla and Apple's Safari browser, enables JavaScript to make HTTP requests to a remote server without the need to reload the page. In essence, HTTP requests can be made and responses received, completely in the background and without the user experiencing any visual interruptions.[/QUOTE]
    Copy linkTweet thisAlerts:
    @grailquester5May 22.2005 — bokeh, that's great - but that's XML - invoking an XML object (XMLHttpRequest). And although it can make use of Javascript and PHP, it is NEITHER Javascript NOR PHP. Hence - for those of us who don't know/use XML (yet), it is useless. The question was can Javascript send information to PHP? The answer is NO. XML, however, via their new object (XMLHttpRequest) can maintain an open connection and invoke Javascript on the client side and PHP on the server side (which IS great) - but again - it is neither PHP nor Javascript DOING this.
    Copy linkTweet thisAlerts:
    @bokehMay 22.2005 — That's like saying a client cannot communicate directly with a server because the internet is a middleman between the two.
    Copy linkTweet thisAlerts:
    @The_Anime_KingauthorMay 22.2005 — Okey, I'm not using XML, I'm not gonna learn ANOTHER language... got twenty of 'm already... anyway is this possible:
    But now I have another question. Is there a way to auto-refresh PHP code?

    Let's say Online() is a function that reads the SQL-database for online visitors, but after ten minutes of reading the info, maybe all visitors are gone and still it gives the same number... Is there a way to autorefresh only a little bit of PHP-code?[/quote]
    Copy linkTweet thisAlerts:
    @grailquester5May 23.2005 — King,

    If you're using an iframe, you could (I think - never having tried it) just reload that iframe (the frame's source). Javascript has a setTimeout() function which you could use in a loop to reload the content of said iframe, which would obviously be your "visitors.php" file or something...

    bokeh,

    Not even going to respond to that.
    Copy linkTweet thisAlerts:
    @BurntHandOct 25.2007 — Wikipedia Quote:

    http://en.wikipedia.org/wiki/Ajax_(programming)

    "XML is sometimes used as the format for transferring data between the server and client, although any format will work, including preformatted HTML, plain text and JSON. These files may be created dynamically by some form of server-side scripting."
    Copy linkTweet thisAlerts:
    @bokehOct 25.2007 — Please don't reply to threads that are 2 1/2 years old already.
    Copy linkTweet thisAlerts:
    @meekythegeekMay 12.2009 — Why not reply to old threads? It helped me when I reached this page through a search. If you don't want older posts being read and replied to then remove replying or remove the old posts. If there is new helpful information, then I think posting it for those who may stumble upon it later will only be a benefit.

    I mean I hope it doesn't hurt anyone's virtual pride because they made a comment in good faith based on older technology and now their advice is seen as obsolete.
    Copy linkTweet thisAlerts:
    @bokehMay 13.2009 — Why not reply to old threads?[/QUOTE]Because it's not helpful to the OP.
    Copy linkTweet thisAlerts:
    @bebenzJul 25.2009 — Hello...im new in this room...

    hey guys...

    i wanna ask...something made me confused...

    everytime, when we try to create email account, we have to fill text box right..??

    in textbox USERNAME example,, when it lost it focus,, there are some notification about availability for the username we wrote right???

    my question is :

    what tools or script language to create something like that??Php?javascript?or what??

    thanks...
    Copy linkTweet thisAlerts:
    @bokehJul 25.2009 — my question is :

    what tools or script language to create something like that??Php?javascript?or what??[/QUOTE]
    Javascript on the client sends the username to the server. This request is handled by PHP or other serverside scripting language which checks the database to see if the username is vacant and returns the result to javascript on the client. All is done in the background without the need for a page reload.
    Copy linkTweet thisAlerts:
    @webStudent23Sep 10.2010 — After searching and putting things together, here is a handy script I'm using for my registration page. Whenever the user types anything into the HTML username field, it runs a JavaScript function which communicates with the server to check the availability of the given username using PHP and MySQL. The PHP script then writes the status back into the HTML page.

    All of this is done in the background without any page reloads or visual interuptions.

    My info came from the following URL:

    http://www.geekpedia.com/tutorial145_PHP-and-Javascript-Dynamic-update-with-MySQL.html



    File "HTMLPage.html":

    <html>

    <head>

    <title>HTML Page</title>

    <script type="text/javascript">

    // Javascript function to update the username "status" field

    function updateAvailabilityStatus(username) {

    // Set status field while availability check is being performed...

    document.getElementById('status').innerHTML="Please wait...";

    // Create new script element, set its relative URL, and load it
    // The script inside CheckAvailability.php will change this page's "status" field automatically...
    script = document.createElement('script');
    script.src = 'CheckAvailability.php?username=' +username;
    document.getElementsByTagName('head')[0].appendChild(script);
    }
    </script>

    </head>

    <body>

    <!-- Username input field

    When the user types anything into the field it automatically checks the username status from a MySQL database -->

    <input type="text" name="username" onkeypress="updateAvailabilityStatus(username.value);"/>

    <!-- Availability status field -->
    <span id="status"></span>

    </body>

    </html>



    File 'CheckAvailability.php':

    <?php


    if (isset($_GET['username'])) {

    $username = ($_
    GET['username']); // Setting The $username Variable

    $sqluser = "YOUR_USER_HERE"; // MySql Username
    $sqlpass = "YOUR_PASS_HERE"; // MySql Password
    $sqlhost = "YOUR_HOST_HERE"; // MySql Host Address
    $sqldb = "YOUR_DB_HERE"; // MySql Database Name

    mysql_connect($sqlhost, $sqluser, $sqlpass); // Connecting To MySql
    mysql_select_db($sqldb); // Selecting MySql Database

    $results = mysql_query("SELECT * FROM TblUsers WHERE USERNAME='$username'"); // Run A Query To See If The Username exists
    $numResults = mysql_num_rows($results); // Number Of Results Of The Query

    if ($numResults > 0) { // If username exists
    if ($username == "") {
    $status = "Username is invalid"; // Invalid username... checks could be more vigorous
    } else {
    $status = "Username not available"; // Unavailable username
    }
    } else {
    $status = "Username available"; // Available username
    }

    ?>

    document.getElementById('status').innerHTML = '<? echo $status; ?>';

    <?

    mysql_close(); // Closing The Connection

    }

    ?>
    Copy linkTweet thisAlerts:
    @johnywhyJan 12.2012 — old threads help new readers. Fora are not just for the benefit of the OP.
    Copy linkTweet thisAlerts:
    @spufiJan 13.2012 — old threads help new readers. Fora are not just for the benefit of the OP.[/QUOTE]

    Too bad yours was neither helpful to the OP, or new readers. If the OP wants to bump an old thread to ask more questions about the issue at hand, that's fine. If somebody has a question related to the issue, they are more than welcome to start their own thread and reference any previous ones covering the issue if need be.
    Copy linkTweet thisAlerts:
    @johnywhyJan 13.2012 — What do you mean "yours"? I did not post any questions or answers here. I just found this thread while researching this issue, and I found this thread, including the newer posts, relevant and helpful to my needs.

    Cheers.
    Copy linkTweet thisAlerts:
    @cookiimonstar1Jan 13.2012 — Ok i'm a total noob to Java Script, what i want to do is get a number from a php page an then put it into an if statement.


    So here goes the php page is completely blank no html tags or anything. The only thing on the page is an number for example number 4.

    And what I need the Java Script to do is every 3 mins without refreshing the page, compare the the numbers

    if (num_from_php_page > 6)

    {do this}

    I'm not sure if Java Script can even do this, so any help or advice would be much appreciated.
    Copy linkTweet thisAlerts:
    @cookiimonstar1Jan 13.2012 — Sorry mistake in posting


    Ok i'm a total noob to Java Script, what i want to do is get a number from a php page an then put it into an if statement.


    So here goes the php page is completely blank no html tags or anything. The only thing on the page is an number for example number 4.

    And what I need the Java Script to do is every 3 mins without refreshing the page, compare the the numbers

    if (num_from_php_page > 6)

    {do this}

    I'm not sure if Java Script can even do this, so any help or advice would be much appreciated.[/QUOTE]
    Copy linkTweet thisAlerts:
    @spufiJan 14.2012 — What do you mean "yours"? I did not post any questions or answers here. I just found this thread while researching this issue, and I found this thread, including the newer posts, relevant and helpful to my needs.

    Cheers.[/QUOTE]


    If you found it useful, then great. Making commentary on the thread at this point is like trying to win a snowball fight that ended a year ago. Do you even know if the person you are referencing posts on this forum anymore? Get the info you need and walk away.
    ×

    Success!

    Help @The_Anime_King spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.28,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...