passing php $variable to external javascript

how to pass a varable from php to external javascript

Page 1 of 1

9 Replies - 19003 Views - Last Post: 25 March 2007 - 05:15 PM

#1 rpjd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 08-March 07

passing php $variable to external javascript

Posted 19 March 2007 - 05:54 AM

My problem. I have javascript calling a php file which queries an database and retrieves a value. The php works fine. Now I have a php $variable which I want to send back to the orihinal javascript which called the php.file. This is the javascript I am working with:
<script type="text/javascript" language="javascript">
function selectedItem()
{
action="desc.php";
}



and this is the php script in a file called desc.php
<?php
$connect = pg_connect("dbname=allied_auto_parts user=user host=localhost password=password");
$description = pg_query($connect, "select description from parts");
$desc = pg_fetch_array($description);
?>



how do I get the javasript to access php variable $desc?

Can anyone help me with this?

RPJD :)

Is This A Good Question/Topic? 0
  • +

Replies To: passing php $variable to external javascript

#2 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: passing php $variable to external javascript

Posted 19 March 2007 - 11:49 AM

You can go from PHP -> Javascript, but not Javascript -> PHP. So a variable can be set from php to javascript, but javascript can't obtain a variable set in php, since they don't work that way.
Was This Post Helpful? 0
  • +
  • -

#3 rpjd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 08-March 07

Re: passing php $variable to external javascript

Posted 19 March 2007 - 12:12 PM

View PostStyx, on 19 Mar, 2007 - 11:49 AM, said:

You can go from PHP -> Javascript, but not Javascript -> PHP. So a variable can be set from php to javascript, but javascript can't obtain a variable set in php, since they don't work that way.


Well my onclick() event is not causing any errors, so I am assuming desc.php executes with action="desc.php";, but I need to send $value back to javascript to verify this. How to acomplish this is my question?

RPJD :)
Was This Post Helpful? 0
  • +
  • -

#4 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: passing php $variable to external javascript

Posted 19 March 2007 - 09:48 PM

Javascript can't read php variables directly, but you can do something like:

<?php
$var = 'foo';
?>
<script type="text/javascript" language="javascript">
var val = '<?=$var?>';
document.write(val);
</script>


If $value can be read in the current script, you can use its content in javascript. Though php will be unable to read from javascript unless the page is reprocessed.

This post has been edited by Styx: 20 March 2007 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 rpjd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 08-March 07

Re: passing php $variable to external javascript

Posted 25 March 2007 - 09:59 AM

View PostStyx, on 19 Mar, 2007 - 09:48 PM, said:

Javascript can't read php variables directly, but you can do something like:

<?php
$var = 'foo';
?>
<script type="text/javascript" language="javascript">
var val = '<?=$var?>';
document.write(val);
</script>


If $value can be read in the current script, you can use its content in javascript. Though php will be unable to read from javascript unless the page is reprocessed.


I have a php array which I want to change into a javascript array. This php code queries a database and holds values as an array called $parts.
$connect=pg_connect("dbname=databasename host=localhost user=user password=password");
$parts_query=pg_query($connect, "select * from parts");
$parts=pg_fetch_array($parts_query); 



Now I want to assign each value of the array into individual var variables.
Can I do this
<script>
var partnumber = <?php= $parts["partnumber"] ?>;
var description = <?php= $parts["description"] ?>;
var level = <?php= $parts["level"] ?>;
var vatrate = <?php= $parts["vatrate"] ?>;
var price= <?php= $parts["price"] ?>;
</script>


or something similar?

RPJD :)
Was This Post Helpful? 0
  • +
  • -

#6 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: passing php $variable to external javascript

Posted 25 March 2007 - 11:26 AM

You should put them within quotes but yes, you can do that.
Was This Post Helpful? 0
  • +
  • -

#7 rpjd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 08-March 07

Re: passing php $variable to external javascript

Posted 25 March 2007 - 11:45 AM

[quote name='rpjd' post='211569' date='25 Mar, 2007 - 09:59 AM']

View PostStyx, on 19 Mar, 2007 - 09:48 PM, said:

Javascript can't read php variables directly, but you can do something like:

<?php
$var = 'foo';
?>
<script type="text/javascript" language="javascript">
var val = '<?=$var?>';
document.write(val);
</script>


If $value can be read in the current script, you can use its content in javascript. Though php will be unable to read from javascript unless the page is reprocessed.


I have a php array which I want to change into a javascript array. This php code queries a database and holds values as an array called $parts.
$connect=pg_connect("dbname=databasename host=localhost user=user password=password");
$parts_query=pg_query($connect, "select * from parts");
$parts=pg_fetch_array($parts_query); 
[code]

Now I want to assign each value of the array into individual var variables.
Can I do this
[code]
<script>
var partnumber = <?php= $parts["partnumber"] ?>;
var description = <?php= $parts["description"] ?>;
var level = <?php= $parts["level"] ?>;
var vatrate = <?php= $parts["vatrate"] ?>;
var price= <?php= $parts["price"] ?>;
</script>
[code]
or something similar?

RPJD :)
[/quote]

now that I have php variables assigned to javascript var, how do I assign a vaiable to a form text field.  Using php, no matter what I put in the "", everthing with the shows up on the text field.  For some reason the php with the "" is not getting parsed eg
[code]
<input type="text" name="name" value="<?php blah blah blah ?>"/>


<?php blah blah blah ?> is showing in the text field. I have short_open_tags enabled in php.ini Don't know what else I can do.
This why I am looking at jsp/java as well. trying to connct to postgresql with jsp is another can of worms.

RPJD:)
Was This Post Helpful? 0
  • +
  • -

#8 max302  Icon User is offline

  • Proud supporter of the lulz
  • member icon

Reputation: 2
  • View blog
  • Posts: 1,281
  • Joined: 05-March 06

Re: passing php $variable to external javascript

Posted 25 March 2007 - 12:40 PM

OR, you can get more fashion and hype by starting something AJAXy.

Whatever data it is that you want to take from you php page, just make a php script that echos XML, which is very easy to make, then use the JS XML functions to read the information.

If you want to move large amounts of data, this is the ideal way, but if it's only for one variable, it' just useless, got with a PHP tag inside a JS tag with an echo statement, it will do the job.
Was This Post Helpful? 0
  • +
  • -

#9 rpjd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 08-March 07

Re: passing php $variable to external javascript

Posted 25 March 2007 - 01:57 PM

View Postmax302, on 25 Mar, 2007 - 12:40 PM, said:

OR, you can get more fashion and hype by starting something AJAXy.

Whatever data it is that you want to take from you php page, just make a php script that echos XML, which is very easy to make, then use the JS XML functions to read the information.

If you want to move large amounts of data, this is the ideal way, but if it's only for one variable, it' just useless, got with a PHP tag inside a JS tag with an echo statement, it will do the job.


Already ahead of you there max302.
gettting to grips with this http://www.webpastie...tutorial_1.html

RPJD :)
Was This Post Helpful? 0
  • +
  • -

#10 snoj  Icon User is offline

  • Married Life
  • member icon

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

Re: passing php $variable to external javascript

Posted 25 March 2007 - 05:15 PM

RPJD, Like stated in the other thread, you're doing the php short tags wrong. To output something with short tags you do <?=$someVariable ?> or <? echo $someVariable; ?>.

Second thing is that when your are outputting things like $parts["description"], you're not putting it within quote marks since it's (I'm guessing) a string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1