accessing PHP in JS code

This is a discussion on accessing PHP in JS code within the PHP Language forums, part of the PHP Programming Forums category; i have some php code pulling some data from a database and i need to access some of hte variables ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-24-2003
Tim Blackwell
 
Posts: n/a
Default accessing PHP in JS code

i have some php code pulling some data from a database and i need to access
some of hte variables in java script. i've never done this before. how can
i do it.

eg. i'm loading a combo box from the database (this works fine). but, i'd
like to send some description data to a text area when user selects
something, from the combo using, JS. when they click a button they would go
to a website (this works fine for me as well)

any help would be appreciated,

tim


<?
require_once("login.php");
login();


mysql_select_db("krra");
$options = "select linkname,linkaddress, linkdesc from links order by
linknum";
$optres = mysql_query($options);
$opt_results = mysql_num_rows($optres);

$selsizequery = "select * from links";
$selsize = mysql_query($selsizequery);
$selectsize = mysql_num_rows($selsize);
echo "$selectsize links in this list: ";

echo "<select name = example size = $selectsize onChange=showtext()>";

for ($i=0; $i <$opt_results; $i++)
{
$optrow = mysql_fetch_array($optres);
$optionval = stripslashes($optrow["linkname"]);
$optiongoto = stripslashes($optrow["linkaddress"]);
echo "<option value=$optiongoto>$optionval</option>";
echo "<br>";

}
echo "</select>";

?>
</p>
<p align="left">
&nbsp;<script language="javascript">


<!--

var shortcut=document.combowithtext
var descriptions=new Array()

//extend this list if neccessary to accomodate more selections

<?
require_once("login.php");
login();

mysql_select_db("krra");
$options = "select linkdesc from links order by linknum";
$optres = mysql_query($options);
$opt_results = mysql_num_rows($optres);

for ($i=0; $i <$opt_results; $i++)
{
$optrow = mysql_fetch_array($optres);
$descript = stripslashes($optrow["linkdesc"]);

}
?>

descriptions[0]="description of data from combo selection 1"
descriptions[1]="description of data from combo selection 2."
descriptions[2]="description of data from combo selection"
descriptions[3]="description of data from combo selection ."


shortcut.text.value=descriptions[shortcut.example.selectedIndex]
function gothere(){
location=shortcut.example.options[shortcut.example.selectedIndex].value
}

function showtext(){
shortcut.text.value=descriptions[shortcut.example.selectedIndex]
}
//-->
</script></p>

</form>
<p><input type="button" value="Go!" onClick="gothere()" style="float:
left"></p>

</body>
</html>


Reply With Quote
  #2 (permalink)  
Old 11-25-2003
Terence
 
Posts: n/a
Default Re: accessing PHP in JS code

Tim Blackwell wrote:

> i have some php code pulling some data from a database and i need to access
> some of hte variables in java script. i've never done this before. how can
> i do it.
>
> eg. i'm loading a combo box from the database (this works fine). but, i'd
> like to send some description data to a text area when user selects
> something, from the combo using, JS. when they click a button they would go
> to a website (this works fine for me as well)
>
> any help would be appreciated,
>
> tim
>
>
> <?
> require_once("login.php");
> login();
>
>
> mysql_select_db("krra");
> $options = "select linkname,linkaddress, linkdesc from links order by
> linknum";
> $optres = mysql_query($options);
> $opt_results = mysql_num_rows($optres);
>
> $selsizequery = "select * from links";
> $selsize = mysql_query($selsizequery);
> $selectsize = mysql_num_rows($selsize);
> echo "$selectsize links in this list: ";
>
> echo "<select name = example size = $selectsize onChange=showtext()>";
>
> for ($i=0; $i <$opt_results; $i++)
> {
> $optrow = mysql_fetch_array($optres);
> $optionval = stripslashes($optrow["linkname"]);
> $optiongoto = stripslashes($optrow["linkaddress"]);
> echo "<option value=$optiongoto>$optionval</option>";
> echo "<br>";
>
> }
> echo "</select>";
>
> ?>
> </p>
> <p align="left">
> &nbsp;<script language="javascript">
>
>
> <!--
>
> var shortcut=document.combowithtext
> var descriptions=new Array()
>
> //extend this list if neccessary to accomodate more selections
>
> <?
> require_once("login.php");
> login();
>
> mysql_select_db("krra");
> $options = "select linkdesc from links order by linknum";
> $optres = mysql_query($options);
> $opt_results = mysql_num_rows($optres);
>
> for ($i=0; $i <$opt_results; $i++)
> {
> $optrow = mysql_fetch_array($optres);
> $descript = stripslashes($optrow["linkdesc"]);
>
> }
> ?>
>
> descriptions[0]="description of data from combo selection 1"
> descriptions[1]="description of data from combo selection 2."
> descriptions[2]="description of data from combo selection"
> descriptions[3]="description of data from combo selection ."
>
>
> shortcut.text.value=descriptions[shortcut.example.selectedIndex]
> function gothere(){
> location=shortcut.example.options[shortcut.example.selectedIndex].value
> }
>
> function showtext(){
> shortcut.text.value=descriptions[shortcut.example.selectedIndex]
> }
> //-->
> </script></p>
>
> </form>
> <p><input type="button" value="Go!" onClick="gothere()" style="float:
> left"></p>
>
> </body>
> </html>
>
>


what's wrong with

<?php
// get "items" from MySQL database
// javascript source code
print "var arrayFoo = {'item2','item3','item4','item5','item6'};";
?>

You could put all the data in a separate javascript file if you want to
and call it from the HTML page like so

<script language="JavaScript" src="js_vars.php"
type="text/javascript"></script>



Even easier, use the "Heredoc" syntax
http://www.php.net/manual/en/language.types.string.php

$str = <<<EOD

var arrayFoo = {'item2','item3','item4','item5','item6'};
// more javascript goes here

EOD;

Reply With Quote
  #3 (permalink)  
Old 11-27-2003
Chung Leong
 
Posts: n/a
Default Re: accessing PHP in JS code

A simple way is to dump the text into a bunch of hidden fields.
Inserting the variable into the Javascript is a little harder, since
you need to correctly escape slashes, quotes, and line-breaks.

<?

$js_descr_array = array();
for ($i=0; $i <$opt_results; $i++)
{
$optrow = mysql_fetch_array($optres);
$descript = stripslashes($optrow["linkdesc"]);
$js_descr_array[i] = '"' . addcslashes($descript, "\r\n\"\\") .
'"';
}


?>

<script>

var descriptions = [<?php implode(',' $js_descr_array); >];

</script>


"Tim Blackwell" <timblackwell@hotmail.com> wrote in message news:<bptuko$qv4$1@knot.queensu.ca>...
> i have some php code pulling some data from a database and i need to access
> some of hte variables in java script. i've never done this before. how can
> i do it.
>
> eg. i'm loading a combo box from the database (this works fine). but, i'd
> like to send some description data to a text area when user selects
> something, from the combo using, JS. when they click a button they would go
> to a website (this works fine for me as well)
>
> any help would be appreciated,
>
> tim
>
>
> <?
> require_once("login.php");
> login();
>
>
> mysql_select_db("krra");
> $options = "select linkname,linkaddress, linkdesc from links order by
> linknum";
> $optres = mysql_query($options);
> $opt_results = mysql_num_rows($optres);
>
> $selsizequery = "select * from links";
> $selsize = mysql_query($selsizequery);
> $selectsize = mysql_num_rows($selsize);
> echo "$selectsize links in this list: ";
>
> echo "<select name = example size = $selectsize onChange=showtext()>";
>
> for ($i=0; $i <$opt_results; $i++)
> {
> $optrow = mysql_fetch_array($optres);
> $optionval = stripslashes($optrow["linkname"]);
> $optiongoto = stripslashes($optrow["linkaddress"]);
> echo "<option value=$optiongoto>$optionval</option>";
> echo "<br>";
>
> }
> echo "</select>";
>
> ?>
> </p>
> <p align="left">
> &nbsp;<script language="javascript">
>
>
> <!--
>
> var shortcut=document.combowithtext
> var descriptions=new Array()
>
> //extend this list if neccessary to accomodate more selections
>
> <?
> require_once("login.php");
> login();
>
> mysql_select_db("krra");
> $options = "select linkdesc from links order by linknum";
> $optres = mysql_query($options);
> $opt_results = mysql_num_rows($optres);
>
> for ($i=0; $i <$opt_results; $i++)
> {
> $optrow = mysql_fetch_array($optres);
> $descript = stripslashes($optrow["linkdesc"]);
>
> }
> ?>
>
> descriptions[0]="description of data from combo selection 1"
> descriptions[1]="description of data from combo selection 2."
> descriptions[2]="description of data from combo selection"
> descriptions[3]="description of data from combo selection ."
>
>
> shortcut.text.value=descriptions[shortcut.example.selectedIndex]
> function gothere(){
> location=shortcut.example.options[shortcut.example.selectedIndex].value
> }
>
> function showtext(){
> shortcut.text.value=descriptions[shortcut.example.selectedIndex]
> }
> //-->
> </script></p>
>
> </form>
> <p><input type="button" value="Go!" onClick="gothere()" style="float:
> left"></p>
>
> </body>
> </html>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:04 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0