Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  181] [ 3]  / answers: 1 / hits: 47714  / 10 Years ago, sat, may 10, 2014, 12:00:00

This is really basic. All I want to do is measure the html width of a browser window, and pass that data to a PHP script so that I can adjust what information is passed to the site.



I've got the HTML width part down. I'm just having trouble passing the Javascript/jQuery variable to the PHP script using AJAX.



Here's the Javascript/jQuery. It is written in PHP so that I can use the include() function later.



echo 
'<script>
htmlWidth = $(html).width();
$.ajax({
type: POST,
url: mobileView.php,
data:{ width: htmlWidth }
})
</script>';


And here's the PHP:



$width = $_POST['width'];
function mobileView(){
if ($width < 950){
return true;
} else{
return false;
}
}
mobileView();

More From » php

 Answers
1

Here is the code that can be function within a file:



For PHP part, you should pass value to the function and call the function.



I guess you need return data to the client from the php function return.
Then, for ajax part, you have to catch the return data.



<?
if(isset($_POST['width'])){
$width = $_POST['width'];

function mobileView($width){
if ($width < 950){
echo 'true';
} else{
echo 'false';
}
}
mobileView($width);

}else{
?>
<script src=//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js></script>
<script>
htmlWidth = $(html).width();
$.ajax({
type: POST,
url: mobileView.php,
data:{ width: htmlWidth },
success: function(data){
console.log(data);
}
})
</script>

<?
};

?>

[#71095] Thursday, May 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tinakimv

Total Points: 126
Total Questions: 90
Total Answers: 104

Location: Netherlands
Member since Mon, Jun 7, 2021
3 Years ago
;