/    Sign up×
Community /Pin to ProfileBookmark

AJAX jQuery – POST non-form data more specifically screen width

I want to POST the screen width of the device the user is using (client side) to a PHP page which in return would use an if block to say if less than 768px use this layout else another. Even though I’m very ne to jQuery AJAX I’ve been able to put together the following using tutorials on YOUTUBE and others but have come to a dead-end. for the data.

[code]
$(document).ready(function() {
if (window.orientation == 0) {
var width;
$(window).bind(‘orientationchange’, function() {
if ($(window).width() < 768) {
width = $(window).width();
$.post(‘test1406.php’,
{ : width}, <–
function() {

});
} else {
width = $(window).width();
}
});
if ($(window).width() < 768) {
width = $(window).width();
} else {
width = $(window).width();
}
}
});
[/code]

I’ve marked exactly where I’m stuck. Since its not a form value, I don’t exactly have a say for example a textbox name.
I could wrong

Any Ideas, Solutions/ Answers, pointers/tips would be very much appreciated

to post a comment

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 19.2019 — Check if this helps you:
$.post('test1406.php',
{ width: width },
// the width will be available in test1406.php
// in $_POST['width']
function (output) {
// output of test1406.php is available
// in parameter output <br/>
});
Copy linkTweet thisAlerts:
@nsathauthorJun 19.2019 — for some reason the page throws out a HTTP 500 error

from testscript.js

<i>
</i> $(document).ready(function() {
if (window.orientation == 0) {
var width;
$(window).bind('orientationchange', function() {
<br/>
<i> </i> if ($(window).width() &lt; 768) {
<i> </i> width = $(window).width();
<i> </i> $.post('test1406.php',
<i> </i> {width : width},
<i> </i> function(data) {
<i> </i>
<i> </i> });
<i> </i>
<i> </i> } else {
<i> </i> width = $(window).width();
<i> </i>
<i> </i> }
<i> </i> });
<i> </i> if ($(window).width() &lt; 768) {
<i> </i> width = $(window).width();
<i> </i>
<i> </i> } else {
<i> </i> width = $(window).width();
<i> </i>
<i> </i> }
<i> </i> }
<i> </i> });


and on receiving end (PHP) from test1406.php

<i>
</i> &lt;?php
if(isset($_POST['width'])) {
width = $_POST['width'];
echo width;
}
Copy linkTweet thisAlerts:
@SempervivumJun 19.2019 — The $ is missing for variable width:
&lt;?php
if(isset($_POST['width'])) {
$width = $_POST['width'];
echo $width;
}
Copy linkTweet thisAlerts:
@nsathauthorJun 19.2019 — Oh yeh snap thanks for that. But what I understand of the code so far, it should output the width to the page? right? the reason why I ask is because nothing shows
Copy linkTweet thisAlerts:
@SempervivumJun 19.2019 — The output of your script is not displayed on your page unless you initiate this, e. g. by writing it to a container:
$.post('test1406.php',
{width : width},
function(data) {
// output of test1406.php is available in parameter "data"
// you can, e. g., write it to a container:
$('#your-container').text(data); <br/>
});
Copy linkTweet thisAlerts:
@nsathauthorJun 20.2019 — sweet thanks
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...