v2.5.2
Giriş yap

javascript içinden php ye değişken değeri atama

pcmemo
2,389 defa görüntülendi

Merhaba arkadaşlar aşağıdaki javascript anlık olarak tarayıcının boyutunu gösteriyor.
Bu javascript içinde yükseklik ve genişlik değerlerini birer php değişkenine atayabilir miyiz?

<p id="ViewPortSize"> </p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
function GetViewPortSize() {
$('#ViewPortSize').text("Width = " + $( window ).width() + " / Height = " + $( window ).height());   /* <!doctype html> may be necessary */
}

/* Execute "GetViewPortSize" function at page load */
$(GetViewPortSize);

/* Execute "GetViewPortSize" function everytime the browser viewport is resized */
$( window ).resize(GetViewPortSize);
</script>
makifgokce
1982 gün önce

JQuery ile post veya get yöntemiyle halledebilirsin.

var post = function() {
        $.ajax(
            {
                url: "ajax.php",
                type: "POST",
                data: { width: $( window ).width(), height: $( window ).height()}
            })
            .then(function (data)
            {
                console.log( data );
            },
            function (xhr, status, error)
            {
                console.log(status + ": " + error);
            });
    }

ajax.php

if(isset($_POST['width']) && isset($_POST['height'])){
    $width = $_POST['width'];
    $height = $_POST['height'];
    echo "width = $width, height = $height";
}