0

I'm trying to get clients viewport size inside php code, via jquery ajax, but something is wrong:

index.php

<body>
<script>
var h = $(window).height() - 83;
alert (h); // works
$.ajax({
    type: "POST",
    url: 'pass.php',  // I also tried index.php directly
    data: {h : h}
});
</script>

<?php
include ("pass.php");
$h = $_POST['h'];
echo $h; // doesn't work
?>

There is no echo in index.php.

1
  • success:(function(data){alert(data);}) works. why you include "pass.php"? Commented Jul 19, 2014 at 12:42

3 Answers 3

3

Index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
$(document).ready(function(){
    var h = $(window).height() - 83;
    //alert (h); // works
    $.ajax({
        type: "POST",
        url: 'pass.php',  // I also tried index.php directly
        data: {h : h},
        success:(function(data){
            alert(data);
        })
    });
});
</script>

And pass.php

$h = $_POST['h'];
echo $h;

it gives me the output. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Now alert is there, but there is no echo ?
2

You are posting data to pass.php But including pass.php will not give you data sent to this file. You have to consider pass.php as being requested separately. From this file do your echo and to get the output you can add to your jQuery code (after .ajax()) this:

.done(data) {alert(data);} 

Comments

1

it seems that you include pass.php but this is the url called, so your code have to be in pass.php add it to see if your request is well sent

<script>
var h = $(window).height() - 83;
alert (h); // works
$.ajax({
type: "POST",
url: 'pass.php',  // I also tried index.php directly
data: {h : h},
success : function(data, status){
       console.log("success");
       console.log(data);
       console.log(status);
   },

   error : function(data, status, error){
       console.log("error");
       console.log(data);
   }
});
</script>

2 Comments

Thomas, I placed $h = $_POST['h']; inside pass.php but there is no echo, again.
try to add it to your JS and see in the console if the request is well sent

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.