cha0sriderx Posted October 26, 2007 Share Posted October 26, 2007 im trying to figure out the width of the screen and in order to do that you have to use javascript or atleast thats the only way i know how to do it. well i want to take the width and then subtract 150 from it. well it threats the width as a str and not an int value. <? function getwidth(){ $swidth = '<script language="javascript">document.write(screen.width);</script>'; return $swidth; } $width = getwidth(); $middlewidth = "$width - 150"; echo "$width / $middlewidth"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/ Share on other sites More sharing options...
cha0sriderx Posted October 26, 2007 Author Share Posted October 26, 2007 well i know why that wont work, it displays the number after it does the command. i finally decided to view source:P. but is there any other way to go about doing this? for some reason it wouldnt let me edit my first post. sorry for the double post. Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-378352 Share on other sites More sharing options...
putty Posted October 26, 2007 Share Posted October 26, 2007 I use AJAX to send variables between Javascript and PHP, this is a good tutorial to get you started. http://www.tizag.com/ajaxTutorial/ Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-378355 Share on other sites More sharing options...
teng84 Posted October 26, 2007 Share Posted October 26, 2007 yah thats hard using php hmm maybe you can use GET variables function open_win() { var x =screen.width; window.open("http://www.teng84.com?width="+x) } Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-378359 Share on other sites More sharing options...
cha0sriderx Posted October 28, 2007 Author Share Posted October 28, 2007 ive never messed with ajax, do you think its possible you can give me an example code for sending a javascript variable to php? Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-379674 Share on other sites More sharing options...
putty Posted October 29, 2007 Share Posted October 29, 2007 Basically your script is in two parts the JavaScript and your PHP code JavaScript <html> <body> <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction(javaValue){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('ajaxDiv'); ajaxDisplay.innerHTML = ajaxRequest.responseText; } } var queryString = "?javaValue=" + javaValue; ajaxRequest.open("GET", "php.php" + queryString, true); ajaxRequest.send(null); } //--> </script> <form name='myForm'> <input type='button' onclick='ajaxFunction(28)' value='Sent Value To PHP' /> </form> <div id='ajaxDiv'>Your result will display here</div> </body> </html> PHP <?php $javaScript_value = $_GET['javaValue']; echo "The Value Past from PHP to jacaScript was $javaScript_value"; ?> ajaxFunction(28), the 28 will be passed to php the JavaScript value will be passed via var queryString = "?javaValue=" + javaValue; you can pass more than one variable var queryString = "?javaValue=" + javaValue + "&other=" + value; Sorry for the rough explanation but I’m a little shoer on time. The best way to learn ajax is to do a few tutorials. Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-380085 Share on other sites More sharing options...
kenrbnsn Posted October 29, 2007 Share Posted October 29, 2007 I use the Yahoo! User Interface libraries when I want to use AJAX techniques. Here's some very basic code that gets the screen width into PHP: <?php session_start(); if (isset($_POST['vpw'])) { // $_POST['vpw'] holds the value obtained in Javascript $_SESSION['vpw'] = $_POST['vpw']; exit('Ok'); } ?> <html> <head> <title>Your title here</title> <script src="http://yui.yahooapis.com/2.3.1/build/yahoo/yahoo-min.js"></script> <script src="http://yui.yahooapis.com/2.3.1/build/dom/dom-min.js"></script> <script src="http://yui.yahooapis.com/2.3.1/build/event/event-min.js"></script> <script src="http://yui.yahooapis.com/2.3.1/build/connection/connection-min.js"></script> <script type="text/javascript"> var cbnull = function(o) { var resp= o.responseText; alert(resp); // debug } var cb = { success:cbnull, failure:cbnull } function init() { pass_str = 'trace=1&vpw=' + YAHOO.util.Dom.getViewportWidth(); gc = YAHOO.util.Connect.asyncRequest('POST','<?php echo $_SERVER['PHP_SELF'] ?>',cb,pass_str); } YAHOO.util.Event.onDOMReady(init); </script> </head> <body> <?php echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?> </body> This code has been tested and works. Ken Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-380097 Share on other sites More sharing options...
cha0sriderx Posted October 29, 2007 Author Share Posted October 29, 2007 thanks both of you guys for your help, i used ken's since it didnt require a form. Quote Link to comment https://forums.phpfreaks.com/topic/74825-solved-trying-to-get-a-js-var-into-php/#findComment-380143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.