Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts

    javascript tells php

    Here's a way to get javascript to tell php what it needs to know. tested in Firefox, Chrome and Opera.
    PHP Code:
    <?php @session_start(); ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>javascript&amp;php</title>
    <style type="text/css">
        body
        {
        width: 960px;
        margin: 0 auto;
        background:#202020;
        color: #ffffff;
        font-size: 24px;
        }
    </style>
    </head>
    <body>
    <pre>
    <?php if(isset($_SESSION['wh'])) print_r(explode(",",$_SESSION['wh'])); ?>
    </pre>
    <?php if(!isset($_SESSION['wh'])) include "./getdata.txt"?>
    </body>
    </html>

    And this is getdata.txt

    <form id="thisform" action="<?php echo $_SERVER["PHP_SELF"?>" method="post">
    <input type="hidden" name="thisinput"></form>
    <script type="text/javascript">
    document.getElementsByName("thisinput")[0].value=[screen.width,screen.height];
    thisform.submit();</script>
    <?php if(isset($_POST['thisinput'])){$_SESSION['wh']=$_POST['thisinput'];} ?>
    Notice that the information submitted in the form is in the nature of a javascript array, and that it could be made much larger than the two pieces of info that we send here. It could just as well have been an array built by push, or any other method. When it appears on the php side, it's just a comma separated list, and can be exploded into an identical php array, and used as such.

    If you want to send a php array to javascript, the way that I've used is to push the array on the JS side by echoing push statements. That method seems to be immune to differences in data types.
    Last edited by DrDOS; 08-17-2013 at 10:35 PM. Reason: Thought of better way.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    If you just need to send data from JavaScript to PHP and the PHP is a separate script (a different situation to what your script covers) then the simplest JavaScript is:

    Code:
    img = new Image();
    img.src = 'process.php?height=' + screen.height + '&width=' + screen.width;
    img = undefined;
    The main difference from the above solution is that the PHP script then runs completely independently of the current page which stays visible. The only other requirement is that the PHP be set to create a dummy image.

    See http://javascriptexample.net/ajax09.php for an example use where it regularly tells the server that the page is still open.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    That's very interesting and I'll look into it, but right now I'm still improving my script since it does exactly what I want it to do, namely give php all the information it needs to build proper web pages, right up front. Just as a test I rewrote it so that it build the array in the submit process, and I took the style information from the index page and put it in a php page which used the information sent to set part of the style. It would be really nice to be able to particularize the style for the user without having to use javascript with inline code, and this lets you do that.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by DrDOS View Post
    It would be really nice to be able to particularize the style for the user without having to use javascript with inline code, and this lets you do that.
    The screen width and height has nothing to do with the viewport area in which the web page is to be displayed. If all you are trying to do is to style the page differently for different screen or viewport sizes then CSS can do that without needing any JavaScript at all.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Quote Originally Posted by felgall View Post
    The screen width and height has nothing to do with the viewport area in which the web page is to be displayed. If all you are trying to do is to style the page differently for different screen or viewport sizes then CSS can do that without needing any JavaScript at all.
    I may be slightly prejudiced by the fact that my own pages are actually web applications for processing images, some of them very large, such as 4096px, so the % thing won't cut it. I have to know specific values, and so may other people. If you go to my Home page and download this month's calendar, you will see what I'm doing. Maybe next month I put up a calender from Australia?
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by DrDOS View Post
    so the % thing won't cut it.
    What % thing? I thought we were talking about testing the screen or viewport size for example:

    <link rel="stylesheet" media="(max-width: 800px)" href="example.css" />

    to attach specific styles only if the viewport is 800px wide or smaller
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Quote Originally Posted by felgall View Post
    What % thing? I thought we were talking about testing the screen or viewport size for example:

    <link rel="stylesheet" media="(max-width: 800px)" href="example.css" />

    to attach specific styles only if the viewport is 800px wide or smaller
    Well, I haven't tried that, but still would it return anything to php, which would still need to know in my case? I'll learn about it, if it can re-size an image to 1/4 or 1/2 depending on screen size and tell php about it, it might be useful.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

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