alternative to send data obtained with JS to a new page PHP use the data [duplicate]

0

Hello

My knowledge in PHP is basic, but I will understand the explanations in the best way.

My goal is to create a homepage index.php where it gets the screen resolution of the user and this data X and Y are stored in variables and thus a form. The page would automatically call another page where the JS variable values are used by PHP.

I have not yet learned Mysql to formulate a database. I have already seen several options to send variables up per link, but none worked properly.

The little I know about JS is enough to automatically get the result through this code below:

<script>
var x = screen.availWidth;
var y = screen.availHeight;

alert("LARGURA: "+ x +"\nALTURA: " + y)
</script>

Of course without the alert for the page to be able to call another without user intervention.

In my searches I know that in a single file you can not use the JS variables in PHP because PHP runs on the server and JS on the user

As you can see I thought of using a form that would fill itself with the value of the JS variables. The problem that in JS I am more than noob. So in the other file named below the php would use the data that is stored in the form.

I saw a few alternatives here, but none of them I actually managed to use properly and it occurred to me that this alternative would be faster.

Thanks in advance.

    
asked by anonymous 18.04.2015 / 16:45

2 answers

0

In JavaScript you can call the URL of the new page by passing the two values in the URL Query String. Ex.

http://seudominio.com.br/teste.php?width=" + screen.availWidth + "&height=" + screen.availHeight

In the php these values will be available in the $ _GET ["height"] and $ _GET ["width"] variable.

Now if these values are the only ones that need the ideal tool for this is to use Cookies, because they serve exactly for this, to record small values that need to be sent to the server in each call of the page.

An example in php here. link

Now ... if you can improve the question will become easier to respond more specifically. What kind of application is it running on the server or running on the client, what is the purpose of those values, etc ... For example, In my applications I use xmlHttpRequest, but it's just to call a service in Php that returns a JSON and from hence I continue in JavaScript. Your need may be different.

    
18.04.2015 / 23:31
0

JAVASCRIPT jquery:

$(function() {
    $.post('alerta.php', { width: screen.width, height:screen.height }, function(json) {
        if(json.outcome == 'success') {
            // do something with the knowledge possibly?
        } else {
            alert('Impossível deixar o PHP saber a resolução da tela!');
        }
    },'json');
});

PHP (alert.php)

<?php
// Você pode fazer algo parecido com isto:
if(isset($_POST['width']) && isset($_POST['height'])) {
    $_SESSION['screen_width'] = $_POST['width'];
    $_SESSION['screen_height'] = $_POST['height'];
    echo json_encode(array('outcome'=>'success'));
} else {
    echo json_encode(array('outcome'=>'error','error'=>"Não é possível definir a resolução."));
}
?>

Source: link

Blz

    
18.04.2015 / 22:43