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>