www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: PHP reloading scripts without reloading the page

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30

    Unhappy PHP reloading scripts without reloading the page

    I'm writing Analytical software that uses javascript but mainly PHP to log things

    The javascript posts some result to $_GET variable like this

    document.location.href = "?screenwidth=" + screen.width + "&screenheight=" + screen.height; but this requires the page to reload for PHP to process it.

    However the page CANNOT reload every time someone visits, because it disrupts the user experience too much.

    Is there a way, in javascript or PHP, of opening dynamically the PHP file, not in an iframe, cos that messes up the formatting, so PHP can operate on it and write it to a MYSQL database.

    I don't mind if this requires some complicated code - I'm hapy to give it a bash.

    Thanks,

    Jonathan

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    You could send all this data via an asynchronous post request using javascript (AJAX). I'd recommend using one the frameworks such as jquery, they make this very simple.

  3. #3
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Quote Originally Posted by Mindzai View Post
    You could send all this data via an asynchronous post request using javascript (AJAX). I'd recommend using one the frameworks such as jquery, they make this very simple.
    How would I do this? Is it possible without reloading the page?

    Thanks for the replies,

    Ronnie268

  4. #4
    Join Date
    May 2009
    Posts
    21

    ajax suxx

    hi there =)


    i'm at work now, so i can't try the code, but if you're little skilled in debugging, you can rewrite this =)


    so, ajax is powerfull, but easy to hack, so i do not recommand ajax for such things.

    try something like that:

    Code:
    <input type = button onclick = 'this.name = "openeer"; window.open("the.address.and/get?goes=here", "title", "format");' />
    in the opening window write

    Code:
    openeer.functionToCall(variablesToSend);
    i'm not sure if it was parent.openeer or openeer (the name we have given)


    this is much secure than ajax =)



    have phun



    maxx

  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by STREETRULEZ View Post
    so, ajax is powerfull, but easy to hack, so i do not recommand ajax for such things.
    What an absolutely nonsense statement. AJAX is just a technique used to send a request to the server, just the same as if you typed the url and hit enter yourself. Your solution of opening another window with a form which the user then has to submit anyway is far, far less graceful than sending the request automatically, and precisely no more secure.
    Last edited by Mindzai; 06-03-2009 at 12:23 PM.

  6. #6
    Join Date
    May 2009
    Posts
    21
    Quote Originally Posted by Mindzai View Post
    What an absolutely nonsense statement. AJAX is just a technique used to send a request to the server, just the same as if you typed the url and hit enter yourself. Your solution of opening another window with a form which the user then has to submit anyway is far, far less graceful than sending the request automatically, and precisely no more secure.
    hi there =)

    first, i want to scream out, that my daughter was born on 2009-06-02 23:15:00 and we named her 'michelle' =) and yes, i'm proud bout this little girl XD


    so to this quote you should read something more on some official sites =)

    http://www.usatoday.com/money/indust...ack-usat_x.htm

    is one of the sites i've in mind =)

  7. #7
    Join Date
    May 2009
    Posts
    21
    hi there again =)

    this.name was wrong - sorry, but here is an examplecode, i've wrote to get an login-window, returning the POST to the caller

    Code:
    function zeigeLogin()
    {
    	self.name = 'openeer';
    	var wind = window.open("", "Login", "width=550,height=290");
    	wind.document.write("<html><head>"+
    	"<style type = 'text/css'>.login_legend{font-size:18px;font-weight:bolder;color:#555555;border:0px;}"+
    	""+
    	"</style>"+
    	"</head><body><div id='suche_container' class='suche_container' style='width:540px;height:230px;background-image:url(images/popup_hg.jpg); padding-bottom: 6px;  border-bottom: 1px solid #D2D1D2;'>"+
    					"<form action='' target = 'openeer' method='post' onsubmit = 'window.close()'>"+
    					"<fieldset>"+
    						"<legend class='login_legend' style='padding-left: 13px;'>IMMOBLOG LOGIN</legend>"+
    						"<div style = 'margin-top:6px;'>"+
    						"<label for = 'login_user' class = 'login_text_linemargin8px' style = 'color:#555555;width:100px;'>Benutzername</label>"+
    						"<input type ='text' name = 'login_user' id = 'login_user' class='input_text' style = 'width:270px;' />"+
    						"</div><div style = 'margin-top:4px;'>"+
    						"<label for = 'login_password' class = 'login_text_linemargin4px' style = 'color:#555555;width:100px;'>Passwort</label>"+
    						"<input type = password name = 'login_password' id = 'login_password' class='input_text' style = 'width:270px;' /></div>"+
    						"<div style = 'margin-top:4px;margin-left:96px;'><input type = checkbox class = 'login_checkbox input_checkbox' style = 'color:#555555;' name = 'rememberme' id = 'rememberme' />"+
    						"<label for = 'rememberme' class = 'login_text_linemargin4px'>Ich m&#246;chte aus diesem Computer einge-<br>loggt bleiben, bis ich mich aktiv abmelde</label></span></div>"+
    						"<div style = 'margin-top:4px;'><span class = 'login_text_linemargin4px' style = 'margin-left:280px;'><input type='submit' value='Anmelden' class='input_submit'></span></div>"+
    						"<div></div><br /><br />"+
    						"<legend class = 'login_legend'>Registrierung</legend>"+
    						"<div>Um einen Kommentar abzugeben, k&#246;nnen Sie sich hier registrieren.</div>"+
    						"<div  style = 'margin-left:280px;'><input type = button class = 'input_button' value = 'Registrieren' onclick = 'this.form.action=\"?register=true\";this.form.submit();window.close();'/></div>"+
    				"</fieldset></form></div></body></html>");
    }

    for example you can call openeer.foo(bar) for value-passing =)

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Yes, the following is an example using jQuery:

    HTML Code:
    $.post(
        "stats.php", // script which saves stats
        {
            // the data to send
            foo: "someValue",
            bar: 123,
            baz: "some other value" // etc
        },
        function(response) {
            // callback function from your php script
            alert(response.message);
         }, 
        "json" // response format
    );
    stats.php:

    PHP Code:
    <?php

    if (!isset($_POST)) die();

    // save the stats or whatever here, they are all in $_POST, eg $_POST['foo'] == "someValue" 

    // send a response if you want to
    echo json_encode(array('message' => 'Hooray! You sent: ' .  print_r($_POSTtrue))); 

    ?>
    Last edited by Mindzai; 06-03-2009 at 12:49 PM.

  9. #9
    Join Date
    Nov 2008
    Posts
    27
    surely you can squeeze an iframe in to make this really simple? the one I've just used to display nr realtime incoming info is

    <iframe width=”270″ height=”146″ scrolling=”no” frameborder=”0″ src=”...linkToFramePage...”></iframe>

    I add the following which reloads the page after 300000 milliseconds ie 5mins ...without disturbing the user

    <script>
    function reFresh() {
    location.reload(true)
    }
    window.setInterval(”reFresh()”,300000);
    </script>

    no scrollbars, border or anything visible. I assume I can shrink the width and height to 1px to make it disappear altogether

  10. #10
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Quote Originally Posted by sjk1000 View Post
    surely you can squeeze an iframe in to make this really simple? the one I've just used to display nr realtime incoming info is

    <iframe width=”270″ height=”146″ scrolling=”no” frameborder=”0″ src=”...linkToFramePage...”></iframe>

    I add the following which reloads the page after 300000 milliseconds ie 5mins ...without disturbing the user

    <script>
    function reFresh() {
    location.reload(true)
    }
    window.setInterval(”reFresh()”,300000);
    </script>

    no scrollbars, border or anything visible. I assume I can shrink the width and height to 1px to make it disappear altogether
    I use this for the nightly version but it won't pull out the title of the page, and invalidates Strict and Transitional doctypes.

    I've been thinking about a way of doing it with a 1px x 1px image something like this

    Code:
    <img src="log.php?<script type="text/javascript">document.write("screenheight = " + screen.height + "&screenwidth = " + screen.width);
    This would produce a url like log.php?screenheight=768&screenwidth=1024 etc.

    How could I use PHP to produce an image in the script?

    Thanks,

    Ronnie268

  11. #11
    Join Date
    Nov 2008
    Posts
    2,477
    All well and good until you want to use something other than a transitional doctype, not to mention the annoying click every time the iframe refreshes. Plus this will resend the stats every 5 mins, surely you want stats once per request? There is really no need to be using iframes in this day and age!

  12. #12
    Join Date
    Nov 2008
    Posts
    2,477
    Yes, that certainly seems "official"...

    Saying AJAX is responsible for security holes is utter rubbish. If the code requested has security holes, it makes no difference whether AJAX is involved or not. AJAX is just a technique, not a technology in itself. But you carry on using JS popups like it's 1998, it's no skin off my nose!

  13. #13
    Join Date
    Nov 2008
    Posts
    2,477
    You are really not going about this the right way. There is a technique designed for doing just what you are trying to do already. Please don't be put off by some of the rubbish posted in this thread.

  14. #14
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    If you wrote your html and css properly you wouldn't need to worry about the client's screen dimensions. What are you going to do for people with javascript disabled.

  15. #15
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Quote Originally Posted by bokeh View Post
    If you wrote your html and css properly you wouldn't need to worry about the client's screen dimensions. What are you going to do for people with javascript disabled.
    If you'll excuse me saying, I'm a web designer and I think I know how to write CSS and (X)HTML. As Mindzai correctly pointed out, I am writing an analytics package for distribution (free) and, although this is not something I require, Screen Resolution is something logged by Google Analytics, the most well-known free package. Many people may take this into account when considering using my software.

    For people without javascript, the main script runs in php, so that will pick most things up. Please do not flame, it is considered offensive on forums. The iframe was a temporary method of implementing javascript.

    To connect javascript and PHP, it is necessary either to set cookies or to compose a url using javascript. Both require a page reload for PHP to interpret them. This messes up detection of the referrer in PHP, so I used an iframe.

    I repeat on sending a url of a PHP script for the image, how do I get it do display the image?

    Thanks,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles