Pass $ _POST via javascript and redirect

1

I have a page that takes the user's screen size via Javascript and the other page that receives the data for display. But I can not pass $_POST to another page, since it does not redirect.

Width.php

<script> 
var wi = screen.width;
$.post("Index-m.php", {screen: wi} );
window.location.replace("Index-m.php");
</script>'

Index-m.php

if(isset($_POST['screen'])){
    $resolution  = $_POST["screen"];
    if ( $resolution >= 768)
        echo "teste";
            else{
                if ( $resolution >= 480 and $resolution <= 767)
                    echo "teste 2";
                        else{
                            if ( $resolution <= 479)
                                echo "teste 3";
                            }
                }
    } else
echo 'oi';

NOTE: I can not go through $_GET because this resolution will be to load images or videos and solve the problem of responsive design in the mobile version (Low speed).

    
asked by anonymous 17.06.2015 / 05:00

1 answer

2

You are using ajax (I believe), ajax is asynchronous, so it only works with "callbacks", if you redirect the page with location soon after calling ajax the redirect will abort the Ajax request. p>

Is this jQuery? If it is the code should look like this:

var wi = screen.width;
$.post("Index-m.php", { screen: wi }).done(function() {
    window.location.replace("Index-m.php");
}).fail(function() {
    alert("error");
});

There also seems to be a problem with your PHP, note that here you use the variable $resolution :

$resolution  = $_POST["screen"];

But here you use the variable $wi

if ( $wi>= 768)
Another thing, using if and else without { and } requires a lot of attention with the indentation and with the amount of line breaks, I recommend to always use this way (if you have any errors in the logica let me know ):

if(isset($_POST['screen'])) {
    $resolution  = $_POST["screen"];
    if ($resolution >= 768) {
        echo "55";
    } else if ($resolution >= 480 && $resolution <= 767) {
        include "";
    } else if ( $resolution <= 479)
        include "";
    }
} else {
    echo 'oi';   
}

I did not understand the use of% empty%, but I'll assume you just removed the file names in the example.

Note that when you send a request through ajax and redirect later the same content will not be displayed (it will be two different requests), if you actually need to send a POST at the same time that it redirects (ie just a request) you will have to use include instead of <form> , following example:

<form id="meuForm" action="Index-m.php" method="POST">
    <input name="screen" id="screen" type="hidden">
</form>

<script>
function testCase() {
    var screenField = document.getElementById("screen");
    var meuForm = document.getElementById("meuForm");

    screenField.value = screen.width;
    meuForm.submit();
}
</script>

<button onclick="testCase()">Testar</button>

In the example I used a button, but if you need to "automate" you can use $.post :

<form id="meuForm" action="Index-m.php" method="POST">
    <input name="screen" id="screen" type="hidden">
</form>

<script>
window.onload = function() {
    var screenField = document.getElementById("screen");
    var meuForm = document.getElementById("meuForm");

    screenField.value = screen.width;
    meuForm.submit();
};
</script>

or onload (jQuery):

<form id="meuForm" action="Index-m.php" method="POST">
    <input name="screen" id="screen" type="hidden">
</form>

<script>
$.ready(function() {
    var screenField = document.getElementById("screen");
    var meuForm = document.getElementById("meuForm");

    screenField.value = screen.width;
    meuForm.submit();
});
</script>
    
17.06.2015 / 15:21