Viewed   56 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

2

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.

Saturday, November 26, 2022
2

json_encode will work. You just have to use it the right way:

<a onclick="javascript:window.location.href=<?php echo htmlspecialchars(json_encode($url)); ?>">

This will work since json_encode already returns an JavaScript expression with quotes. And htmlspecialchars is needed to escape possible HTML meta characters.

Saturday, August 20, 2022
 
5

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
        )

)
Monday, October 31, 2022
 
3

Neither html nor javascript can be encrypted, else the browsers would not be able to interprete it and your visitors would not be able to view your site. Dot. End. Compression tools may boost performance a little but will not really help against copyright infringement.

Your php-programs generate html, your visitors will always be able to see your html, but if your server is configured properly no one should ever see your php.

Sunday, November 20, 2022
 
meff
 
2

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.

Friday, October 7, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :