I am very unfamiliar with PHP and doing posting with Javascript. I have two .php
files that I am using - one is my main .php
file called page.tpl.php
and the other is the php file that has my Javascript in it called winWidth.php
. I want to POST the window width of the browser to my page.tpl.php
and then use it to conditionally sort of if a window width is larger than 768
.
I am getting the window width just fine in my winWidth.php
:
var w = window.innerWidth;
But I am not sure what I need to do with it next. Nor am I sure what to do with it in my page.tpl.php
.
Any help would be much appreciated, thanks!
I answered this on a simliar Question here
But what you need ist an Ajax Call from JavaScript with jQuery, it would look like this
$.ajax({
type: "POST",
dataType: 'json',
url: yourfile.php,
data: {data : data}
})
.done(function(response){
//do something with your code like document.wirte(response)
})
.fail(function(jqXHR, textStatus, errorThrown){
alert('Error : ' + errorThrown); //Error handling
});
In plain JavaScript you could do the following
EDIT
var xmlhttp;
var data; //The Data you need to transmit to the php file
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//do something
}
}
xmlhttp.open("POST",'yourfile',true);
xmlhttp.send(data);
}
You can take the given data in PHP then with
$data = $_POST['data'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.