Pass $_POST via javascript and redirect

Asked

Viewed 5,945 times

2

I have a page that takes the screen size of the user via Javascript and the other that receives the data for display. I just can’t pass the $_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';

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

  • "Responsive Designer" -> "responsive design"

  • 3

    I don’t understand why you are performing server side operations if the layout uses responsive design. One of the proposals of the Responsive design is precisely to eliminate this cost of server-side processing.

  • 1

    Thanks @Sergio had not seen that $wi was in the code really should be $Resolution, but as I tried in other ways realize the code I ended up not changing before submitting the question.

  • @Danielomine I am performing this operation to check if it will be possible to improve the speed of mobile navigation because the responsive design even makes it much easier to have time that the site takes to load in 3G, my site is responsive but I have functions that use a lot of the network so I want to see if there is any possibility to improve it.

  • 1

    understand.. I imagined this.. I think I could do as I used to do where I directed to a. mobile.nomedosite... got it? In fact, this is still widely used because not every mobile device is compatible with html.. there are still many mobiles with outdated technologies like wml.

  • I thought this option but would have to recreate the content of entire pagians so it makes it easier because $_GET does not change and 90% of the content remains intact only part of it modifies for example videos and images or even php classes

Show 1 more comment

1 answer

3


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

Is this jQuery? If it’s the code it 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 in your PHP, note that here you use the variable $resolution:

$resolution  = $_POST["screen"];

But here you use the variable $wi

if ( $wi>= 768)

Something else, use if and else without { and } requires a lot of attention with indentation and with the amount of line breaking, I recommend to always use it like this (if you have any error in the logic 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 don’t understand the use of include empty, but I will assume that you removed the file names only in the example.

Note that when sending a request by ajax and redirecting then the same content will not be displayed (will be two different requests), if you actually need to send a POST at the same time that redirects (ie just a request) you will have to use <form> instead of $.post, 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 onload:

<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 $.ready (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>
  • It worked, yes the empty include was only in the example and the $wi was a test I did and forgot to change

  • @Eder I imagined this :)

  • Obg after all I am graduated in php/ html /css and not in java alias I do not know much difference jquery javascript I’m still learning, so I went to follow some tutorials and did not give very right your answer resolvel the problem

  • 1

    @Eder jquery is just a library for javascript, or jquery is javascript -- Java is something totally different from javascript, only names that are similar, actually java is a language maintained by Oracle, used in virtual machines for developing various types of software, such as Desktop, Android and web pages (server-side).

Browser other questions tagged

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