Jump to content

[SOLVED] trying to get a js var into php


cha0sriderx

Recommended Posts

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";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.