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" ""> 
<html xmlns=""> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="css/slideshow.css" type="text/css" /> 

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


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


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



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
answered 7 Months ago

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

Demo here: — 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 = '';

// Trick from
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
answered 7 Months ago

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 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();
    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);
        url:"<?php echo site_url('fixed/modification/tes');?>",
        dataType : 'json',
        cache : false,

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


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.

$data = json_decode(file_get_contents('php://input'), true);

which will result in something like this:

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

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

Wednesday, March 31, 2021
answered 7 Months ago

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
answered 5 Months ago

AJAX is suitable for sending large amounts of code. Take a look at 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
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 :