Click to See Complete Forum and Search --> : Using javascript variables in php


Samo
03-11-2006, 02:20 AM
now before you assume that i'm a impatient/lazy newb, i'm a php newb, not a forum newb, i've used the search function, but the only match i found didn't explain it to a point where i could understand.

anyways, i'm in the middle of making my first custom php script and have hit a bit of a snag. i need a piece of data that's only ubtainable via js, but i need to know how use the variable produced in a php if/else statement:


var monitors = app.monitors.primary();
var res = monitors[0].rect;

<?php
if( $res[2] < 1024 && res[3] < 768)
.....
?>

blue=php
red=javascript


can anyone help?

-Samo

bradgrafelman
03-11-2006, 02:26 AM
Well, first of all, I have to ask -

You realize that JS is client-side, and PHP is server-side, right? So anything you do in JS has to be transmitted back to the server before you can use it in a PHP script. This can be done through solutions such as AJAX, or simply reloading the page.

Samo
03-11-2006, 02:41 AM
hmmmmm, is there anyway to do it with out refreshing, like say, making the if statement javascript but the rest php?

bradgrafelman
03-11-2006, 02:48 AM
As I said, you'll need to look into methods that use Javascript to communicate with the server, such as AJAX. Otherwise, no. You're talking about two completely different ends of the connection here.

Samo
03-11-2006, 03:11 AM
ok, thx anyways

one last quuestion, can html be used in place of xml with AJAX?

FatalError
03-11-2006, 03:15 AM
ok, thx anyways

one last quuestion, can html be used in place of xml with AJAX?
HTML isn't a scripting language, therefore it can't actually do anything, all HTML does is changes how the page looks...so no, it can't.

I found this (http://rajshekhar.net/blog/archives/85-Rasmus-30-second-AJAX-Tutorial.html)tutorial very useful for learning AJAX.

Samo
03-11-2006, 03:19 AM
then does anyone know how i can make an if statement out of a javascript statement that will change the html, that's all i need, an if statement that will change the html of the page

bradgrafelman
03-11-2006, 03:35 AM
Depending on what you need to do, sure, I've used JS to alter the HTML of pages.

But now you start running into browser compatibility issues. The JS I originally made seemed to be compatible with IE only - not a good practice to code for.

Samo
03-11-2006, 03:38 AM
would it work in firefox if the code was just changing an image's dimension?

bradgrafelman
03-11-2006, 03:41 AM
Not sure. You'll have to give it a shot and see what happens.

Sorry I can't be of much help... my knowledge of JS is very limited. If I get time in the next few minutes, I can play around with FF and JS and see what I can come up with. If you're free right now though, you'll probably get results sooner by tinkering with it yourself.

Samo
03-11-2006, 03:44 AM
nah, i g2g2 bed, almost 3am but i'm free all day tommorrow, or uh, today i guess :p

i'll give it ago but i won't really be able to know if works unless i can test it on a computer with a res smaller than 800x600

thx though

Weedpacket
03-11-2006, 04:48 AM
Since this has ended up being about Javascript, I'm moving it to a different forum.

bradgrafelman
03-11-2006, 04:50 AM
'Morning weed!

I'm afraid that I'm going to have to give up on this one as well... sorry Samo. If you need help with the PHP side of it, feel free to re-post a PHP question! ;)

Samo
03-11-2006, 03:23 PM
k, thx for...........saving hours of frustration :p

i'll let ya know if works