alternative to send JS data to a new PHP page to use the data

Asked

Viewed 244 times

0

Hello!

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

My goal is to create a home page index.php where it gets the resolution of the user screen and this data X and Y are stored in variables and so in 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 seen several options to send variables even per link, but none worked properly.

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

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

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

of course without Alert for the page come to be able to call another without user intervention.

I know in my researches that in a single file you can’t use the JS variables in PHP because PHP runs on the server and JS runs on the user

As you can see I thought of using a form that auto fills with the value of JS variables. The problem that in JS I am more than Noob. So in the other file called then php would use the data that is saved in the form.

I saw some alternatives here, but none I could actually use properly and it came to mind that this alternative will be faster.

Thank you in advance.

  • When the user arrives at the page you want to redirect to another page whose PHP knows the screen size, right? which is the url of the other page?

  • I didn’t exactly decide, but I would link to a login page. The idea is that this first page does not take more than a few seconds and automatically loads to the next one already with the user screen resolution data. I put the first one as index.php for having already told me that the server always looks for this to run first.

  • If you explain better why you need to know the size of the screen.

  • Sergio. As I explained below is because of the css. When I was modeling the page I realized that when I minimized the browser screen everything went out of place. So I created a div that involved everything of the site and it worked. However I started to think that the page could model itself according to the user. I saw that you can use php variables in the stylesheet page, changing . css to . php and adding some details. So if I have the values right away the page can adapt automatically using php variables with the values obtained in this first call. Understands.

2 answers

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: https://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php

Blz

  • Do not refer to sites that have nothing to do with the answer. This is considered Spam

  • I will test and return later to comment! Anyway I thank you! =]

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 php these values will be available in the variable $_GET["height"] and $_GET["width"].

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, record small values that need to be sent to the server on each page call.

An example in php here. https://stackoverflow.com/questions/25775046/setcookie-with-get-method

Now... if you can improve the question, it will be easier to answer more specifically. What kind of application is it, if it runs on the server or if it runs on the client, what purpose of these values, etc... For example, in my applications I use xmlHttpRequest, but it is only to call a service in Php that returns a JSON and from there I continue in Javascript. Your need may be different.

  • the page is basically a form, the main part, however when using CSS I realized I needed a div that involved the entire site so that the page did not remodel to the new size taking everything out of place. Being so I figured that getting the screen resolution values I could make a move in php and css to these values the page would always be according to the user. I will test the method you showed and come back later to comment!

  • In this case, I believe that using pure CSS would be the best solution to solve your problem. If you don’t have much experience with CSS (My case) choose a simple and responsive CSS framework such as Pure CSS. This would be enough to create a page and put the correct CSS class that would already be sized correctly for the user’s resolution. Internally this is done with the CSS Media Queries feature.

  • I’ll look into this Media Queries CSS. I’m already looking at a page here and I’m finding it really interesting. Thankful!

  • I understood the use of Media Queries. In short I will make a stylesheet where inside it I will put for each type of resolution a solution. In fact a clean and simple option. One question only. Is there an exception to this type of CSS call?

  • I’m not sure, as I said I’m not a CSS expert. I’ve been using frameworks for responsiveness. Do not forget to give up vote on answers you have found useful, and if you can solve the problem accept one of the answers as correct.

  • I will explain why I chose this answer, even though I did not use the link method which allowed me to know a more interesting way to resolve the issue. Grateful @Vitor!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.