HighDots Forums  

Headline: transfer javascript-variable to php

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Headline: transfer javascript-variable to php in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Henrik Hartig
 
Posts: n/a

Default Headline: transfer javascript-variable to php - 01-05-2006 , 05:03 AM






Sorry should have maked a headline:
My question is still:


Hey NG.
Is it possible to transfer at javascript varible into at php-script?


working:
function ziptest()
<?php
........
.....
$sql = "select city from Postnr where Postnr=2720;
// herafter put into mysql

?>

The problem is that zipcode (2720) is an javascriptvariable and should be
used in a php mysql.
Thought this would work - but it don't.
Please help me
--------------------------------------------------------------
<script type="text/javascript">

var postnummer = "2720";
<?php $sql1 = 'select city from Postnr where Postnr=';
$sql2 = ?> document.write(bynavn) <?php
$sql = $sql1 .$sql2;
echo $sql; ?>

</script>
----------------------------------------

Best regards

Henrik





Reply With Quote
  #2  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: Headline: transfer javascript-variable to php - 01-05-2006 , 06:36 AM






"Henrik Hartig" <hha (AT) scandinavianteleconsult (DOT) dk> wrote

Quote:
Sorry should have maked a headline:
My question is still:


Hey NG.
Is it possible to transfer at javascript varible into at php-script?


working:
function ziptest()
?php
.......
....
$sql = "select city from Postnr where Postnr=2720;
// herafter put into mysql

?

The problem is that zipcode (2720) is an javascriptvariable and should be
used in a php mysql.
Thought this would work - but it don't.
Please help me
--------------------------------------------------------------
script type="text/javascript"

var postnummer = "2720";
?php $sql1 = 'select city from Postnr where Postnr=';
$sql2 = ?> document.write(bynavn) <?php
$sql = $sql1 .$sql2;
echo $sql; ?

/script
----------------------------------------

My dear child, it just doesn't work that way. You've completely
misunderstood how php and javascript work. You see, php is executed at the
server, when the page is being generated. Then the page is sent to client,
where you have the javascript. If the php was executed at the server, and
the javascript at the client - in two completely different places - how do
you expect javascript to pass any variables that way. It works the other way
though, you can pass variables FROM php TO javascript, but not the other way
around.

This works:
<script>
var x = <?php echo rand() ?>;
</script>
And why this works? Well, that's because the php was executed before the
page was sent to client, therefore the client recieved
<script>
var x = 32389; // or something else
</script>

But the other way, it's just not gonna happen. I don't know how to explain
this clearer and simpler, but the thing is that you need to SUBMIT the data
to the server using GET or POST, you can't pass it as a variable (unless
you're using AJAX, but you're not).

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam (AT) outolempi (DOT) net | Gedoon-S @ IRCnet | rot13(xvzzb (AT) bhgbyrzcv (DOT) arg)




Reply With Quote
  #3  
Old   
Henrik Hartig
 
Posts: n/a

Default Re: Headline: transfer javascript-variable to php - 01-05-2006 , 07:01 AM



Thank you father, I know what you mean.
/henrik
"Kimmo Laine" <spam (AT) outolempi (DOT) net> skrev i en meddelelse
news:tq8vf.2380$gm3.1660 (AT) reader1 (DOT) news.jippii.net...
Quote:
"Henrik Hartig" <hha (AT) scandinavianteleconsult (DOT) dk> wrote in message
news:43bcfce5$0$15794$14726298 (AT) news (DOT) sunsite.dk...
Sorry should have maked a headline:
My question is still:


Hey NG.
Is it possible to transfer at javascript varible into at php-script?


working:
function ziptest()
?php
.......
....
$sql = "select city from Postnr where Postnr=2720;
// herafter put into mysql

?

The problem is that zipcode (2720) is an javascriptvariable and should be
used in a php mysql.
Thought this would work - but it don't.
Please help me
--------------------------------------------------------------
script type="text/javascript"

var postnummer = "2720";
?php $sql1 = 'select city from Postnr where Postnr=';
$sql2 = ?> document.write(bynavn) <?php
$sql = $sql1 .$sql2;
echo $sql; ?

/script
----------------------------------------


My dear child, it just doesn't work that way. You've completely
misunderstood how php and javascript work. You see, php is executed at the
server, when the page is being generated. Then the page is sent to client,
where you have the javascript. If the php was executed at the server, and
the javascript at the client - in two completely different places - how do
you expect javascript to pass any variables that way. It works the other
way though, you can pass variables FROM php TO javascript, but not the
other way around.

This works:
script
var x = <?php echo rand() ?>;
/script
And why this works? Well, that's because the php was executed before the
page was sent to client, therefore the client recieved
script
var x = 32389; // or something else
/script

But the other way, it's just not gonna happen. I don't know how to explain
this clearer and simpler, but the thing is that you need to SUBMIT the
data to the server using GET or POST, you can't pass it as a variable
(unless you're using AJAX, but you're not).

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam (AT) outolempi (DOT) net | Gedoon-S @ IRCnet | rot13(xvzzb (AT) bhgbyrzcv (DOT) arg)




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.