473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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>
Jul 17 '05 #1
2 2808
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;

Jul 17 '05 #2
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" <ti**********@hotmail.com> wrote in message news:<bp**********@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>

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
10
by: Anton | last post by:
Hi, when accessing a secured 3rd party webservice i'm getting a 401 HTTP Statuscode (unauthorized). When entering the url in a browser and entering the username and password manually, the wsdl is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.