12 Replies - 3928 Views - Last Post: 29 October 2004 - 06:50 AM

#1 pioSko  Icon User is offline

  • still.dreaming
  • member icon

Reputation: 23
  • View blog
  • Posts: 1,888
  • Joined: 06-June 03

Php And Javascript Variable

Posted 12 May 2004 - 12:12 AM

so.. i'm trying to get the index.php file to check the screen resolution and then depending on the result, get php to do something...

how can I get PHP to "read" that Javascript variable?? :/


<script language="Javascript">
<!--

if (screen.width < 1024) screenWidth = false;
if (screen.width >= 1024) screenWidth = true;

//-->
</script>
<?
  if (screenWidth == false) {
      echo ("something");
  } else if (screenWidth == true){
      echo ("something else");
  }
?>


... can I join the two together in a different way? something like...

<script language="Javascript">
<!--

if (screen.width < 1024) <?  echo ("something"); ?>;
if (screen.width >= 1024) <? echo ("something else"); ?>

//-->
</script>



hrm.. i can already see that that's not gonna work.. but.. you get my point.

This post has been edited by Red|5: 12 May 2004 - 12:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Php And Javascript Variable

#2 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 342
  • Joined: 14-May 02

Re: Php And Javascript Variable

Posted 12 May 2004 - 03:48 AM

PHP cant read Javascript because PHP is server side and so PHP is changed before the Javascript

The only way you will be able to do what your trying is by echo'ing the if and else for the javascript when the page is requested

eg
<script language="Javascript">
<!--

if (screen.width < 1024) {
<? echo "window.alert('UPGADE your resolution');"; ?>
} else {
<? echo "window.alert('NICE resolution');"; ?>
}
//-->
</script>



i dont know if that code will work as i am no javascript guru, but you get the idea right?

This post has been edited by MathewS: 12 May 2004 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 pioSko  Icon User is offline

  • still.dreaming
  • member icon

Reputation: 23
  • View blog
  • Posts: 1,888
  • Joined: 06-June 03

Re: Php And Javascript Variable

Posted 12 May 2004 - 11:55 PM

hrmm... a lightbulb just poped above my head and i understood the problem...

ok.. thanx... i might be back :P
Was This Post Helpful? 0
  • +
  • -

#4 pioSko  Icon User is offline

  • still.dreaming
  • member icon

Reputation: 23
  • View blog
  • Posts: 1,888
  • Joined: 06-June 03

Re: Php And Javascript Variable

Posted 13 May 2004 - 04:28 AM

ok.. first problem i've now done just with Javascript and document.write().

now i have a problem with PHP variables and Strings...

i have something like this:

<?

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$message = "You are from " + $hostname;
echo ($message);

?>


.. i get a result of 0 (zero), instead of "You are from whatever.com"


Why's that?
Was This Post Helpful? 0
  • +
  • -

#5 caffinephreak  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 229
  • Joined: 08-July 02

Re: Php And Javascript Variable

Posted 13 May 2004 - 01:23 PM

echo "$message";


from my experience, echo doesn't like parenthises(sp?)

or you could use print
print("$message");


thats really odd too...echo isn't supposed to return a value

This post has been edited by caffinephreak: 13 May 2004 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Php And Javascript Variable

Posted 13 May 2004 - 03:38 PM

Red|5, on May 13 2004, 04:28 AM, said:

Why's that?

is $_SERVER['REMOTE_ADDR'] defined? Run phpinfo(); to find out.
Was This Post Helpful? 0
  • +
  • -

#7 pioSko  Icon User is offline

  • still.dreaming
  • member icon

Reputation: 23
  • View blog
  • Posts: 1,888
  • Joined: 06-June 03

Re: Php And Javascript Variable

Posted 14 May 2004 - 12:13 AM

well.. this is the actuall code I'm using:

Quote

<?
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$message = $hostname;
mail("red5@red5.attu.pl", "Visitor to www.red5.attu.pl", $message,
    "From: red5@red5.attu.pl\r\n" .
    "Reply-To: red5@red5.attu.pl\r\n" .
    "X-Mailer: PHP/" . phpversion());
?>


in this form it's fine.. i get $hostname as whatever.com

however if I change it to:

$message = "User from " + $hostname + " visited your site";

... i get an email with a 0 (zero).

So i don't have a problem with echo, and gethostbyaddr($_SERVER['REMOTE_ADDR']); does work by itself...

$_SERVER['REMOTE_ADDR'] is defined.
$_SERVER['REMOTE_HOST'] is not... that's why the gethostbyaddr()

This post has been edited by Red|5: 14 May 2004 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 pioSko  Icon User is offline

  • still.dreaming
  • member icon

Reputation: 23
  • View blog
  • Posts: 1,888
  • Joined: 06-June 03

Re: Php And Javascript Variable

Posted 14 May 2004 - 01:30 AM

nevermind... got it

$message = "Visitor from $hostname";

*sigh* :)
Was This Post Helpful? 0
  • +
  • -

#9 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 342
  • Joined: 14-May 02

Re: Php And Javascript Variable

Posted 14 May 2004 - 04:08 AM

or you could use

$message = "User from ". $hostname ." visited your site";

i dont think php likes "+" when connecting strings together.
Was This Post Helpful? 0
  • +
  • -

#10 pioSko  Icon User is offline

  • still.dreaming
  • member icon

Reputation: 23
  • View blog
  • Posts: 1,888
  • Joined: 06-June 03

Re: Php And Javascript Variable

Posted 14 May 2004 - 05:34 AM

ooo.. thanks for the tip... :)
Was This Post Helpful? 0
  • +
  • -

#11 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Php And Javascript Variable

Posted 14 May 2004 - 07:33 AM

MathewS, on May 14 2004, 04:08 AM, said:

i dont think php likes "+" when connecting strings together.

Doh! I can't believe I missed that. I, too, was thinking in Java, not PHP.

The + operator is a numeric operator in PHP, unlike Java where it will concatenate strings as well. Use dot notation to concatenate strings or, as you have discovered, the power of double-quotes which will evaluate variables (but not functions).
Was This Post Helpful? 0
  • +
  • -

#12 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Php And Javascript Variable

Posted 14 May 2004 - 08:36 AM

caffinephreak, on May 13 2004, 01:23 PM, said:

echo "$message";


from my experience, echo doesn't like parenthises(sp?)

or you could use print
print("$message");


thats really odd too...echo isn't supposed to return a value

It doesn't like quotation marks for you since you arn't using brakets to define varibles ('{' and '}'). So your echo should look like either of these if putting a varible(s) in a string.

echo "Your ip is: {$_SERVER["REMOTE_ADDR"]}.";

echo "Your ip is: ".$_SERVER["REMOTE_ADDR"].".";



Using
echo "$message";

is just bad coding IMHO. You might as well just do this
echo $message;

Was This Post Helpful? 0
  • +
  • -

#13 mstyle  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 25-October 04

Re: Php And Javascript Variable

Posted 29 October 2004 - 06:50 AM

hehe, php never uses + for concatting text.

The plus is for doing math stuff.

$message = 2 + 2;
$message ="2" + "2"; (not very sure)

both should echo 4

$newvar = "hello";
$message = "text ".$newvar;
$message = $newvar.$newvar;

both will output

text hello
hello hello


also, make sure u know when to use " " and ' '
' ' qotes directly so echo ' $var' will echo $var while
echo "$var" echo's the value of var.

Just some notes on concatting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1