Asked  7 Months ago    Answers:  5   Viewed   25 times

How can I get the browser's height and width to PHP? Like a data transfer from JavaScript to PHP? With using innerHeight and InnerWidth, I think.

(I just need to show user small picture if he has small screensize and big if big and without a data about screensize I can't do it)

I have like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="css/slideshow.css" type="text/css" /> 

<script> 
  document.write('script.php?screen=' + screen.width + 'x' + screen.height'); 
</script> 

</head> 
<body> 

  <?php $lol=$_GET['screen']; 
  <?php echo "SIZE : $lol" ?> 

</body> 
</html> 

And it doesn't work. What am I doing wrong?

 Answers

96

You would have to make an AJAX call from JavaScript to a PHP script. That PHP script would save the screen resolution in the current session (you'll need to use sessions for this). A PHP script requested at a later point could then access the screen resolution as passed by the JavaScript snippet.

Wednesday, March 31, 2021
 
Vlad
answered 7 Months ago
18

Use this trick and bind an onerror event to an Image element.

Demo here: http://jsfiddle.net/g6LyK/ — works on the latest Chrome.

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'http://fonts.googleapis.com/css?family=Vast+Shadow';
document.getElementsByTagName('head')[0].appendChild(link);

// Trick from https://stackoverflow.com/questions/2635814/
var image = new Image;
image.src = link.href;
image.onerror = function() {
    ctx.font = '50px "Vast Shadow"';
    ctx.textBaseline = 'top';
    ctx.fillText('Hello!', 20, 10);
};
Wednesday, March 31, 2021
 
JustSteveKing
answered 7 Months ago
44

First, you need to change $.toJSON(asetid) to JSON.stringify(asetid). But, if, somehow you still want to use $.toJSON(asetid), you need to include the jquery-json plugin from http://code.google.com/p/jquery-json/ on your page.

Second, I think the ajax request part should be put outside of the iterator function, otherwise, you will be making an ajax request for each row of the table. Here is the revised code:

var Dataconvert;
var asetid = new Array();
$("#simpanmodifikasi").click(function(){
    var table = $('#tableasal tbody');
    table.find('tr').each(function (row, input) {
        // var coba = $(this).find('input'),
        // asetid = coba.eq(0).val();
        asetid[row] = {
            "asetid" : $(this).find('input:eq(0)').val(),
            "namabarang" : $(this).find('input:eq(1)').val(),
        }
    });
  
    //these should be outside the 'each' iterator
    Dataconvert = JSON.stringify(asetid);
    $.ajax({
        url:"<?php echo site_url('fixed/modification/tes');?>",
        type:"POST",
        data:Dataconvert,
        dataType : 'json',
        cache : false,
        success:function(html){
           alert(html);
         }
    })
})

Third, now the answer to your question. The ajax request will send the stringified JSON in the POST body. Example:

[{"asetid":"10","namabarang":"Buku"},{"asetid":"30","namabarang":"Laptop"}]

To read it from the PHP side (the fixed/modification/tes script), you can manually read it from the standard input and parse it using json_decode.

<?php
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
?>

which will result in something like this:

Array
(
    [0] => Array
        (
            [asetid] => 10
            [namabarang] => Buku
        )

    [1] => Array
        (
            [asetid] => 30
            [namabarang] => Laptop
        )

)
Wednesday, March 31, 2021
 
Freddie
answered 7 Months ago
60

If the browser doesn't have the file in cache, "skipping" causes the browser to send a new request, with a "range" header. Your PHP file needs to handle this header.

this means:

  1. get and parse the range header
  2. respond to the request with status code 206 and corresponding range headers
  3. output only necessary bytes
Saturday, May 29, 2021
 
bumperbox
answered 5 Months ago
50

AJAX is suitable for sending large amounts of code. Take a look at jQuery.post and jQuery.ajax. Special characters pass through POST-request unchanged.

Make sure you set php setting magic_quotes_gpc to off, as this option tells PHP to escape any special character with backslash.

Saturday, May 29, 2021
 
Fredy
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :