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>
"Responsive Designer" -> "responsive design"
– Daniel Omine
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.
– Daniel Omine
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.
– Eder
@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.
– Eder
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.
– Daniel Omine
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
– Eder