Два варианта:
1-й — возврат только данных
2-й — возврат json (status + message)
Основной файл:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Передача разрешения для php</title>
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function() {
$.ajax({
type: 'GET',
url: 'test.php',
data: {
width1: $(window).width(),
height1: $(window).height()
},
// Варинат с echo
/*success: function(data) {
// alert(data);
$('#php_return').html(data);
},*/
// EOF Варинат с echo
// Варинат с echo json_encode
dataType: 'json',
success: function(data) {
if (data.status =='1') {
$('#php_return').html(data.message);
}
else{
$('#php_return').html(data.message);
}
},
// EOF Варинат с echo json_encode
error: function(){
// Что-т пошло не так
$('#php_return').html('Что-то пошло не так');
}
});
});
</script>
<style type="text/css">
</style>
</head>
<body>
<div id="php_return">Тут контент при загрузке</div>
</body>
</html>
Файл test.php в котором выполняется php код
<?php
/* //Варинат с echo
$width = $_GET['width1'];
// $height = $_GET['height1'];
if(!isset($width) || !isset($height)){
echo 'Ошибка получения разрешения';
}
else {
if ($width > 768) {
echo '1111Тут один код при разрешении =' . $width.'x'.$height;
} else {
echo '2222Тут другой код при разрешении =' . $width.'x'.$height;
}
} */
?>
<?php
//Варинат с echo json_encode
$width = $_GET['width1'];
// $height = $_GET['height1'];
$echo_var = '';
$error_var = 1;
if(!isset($width) || !isset($height)){
$echo_var = 'Ошибка получения разрешения';
$error_var = 1;
}
else {
if ($width > 768) {
$echo_var = '1111Тут один код при разрешении =' . $width.'x'.$height;
$error_var = 0;
} else {
$echo_var = '2222Тут другой код при разрешении =' . $width.'x'.$height;
$error_var = 0;
}
}
echo json_encode(array('status' => $error_var,'message' => $echo_var));
?>