Stuffing a PHP array variable into a javascript array variable
| From: | Jason Hoover | Date: | Fri, 05 Jan 2001 00:00:40 +0000 |
| Subject: | Stuffing a PHP array variable into a javascript array variable | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-4821@lists.php.net to get a copy of this message | ||
Is it possible to stuff a php array variable into a javascript array
variable so I can loop through it to display things dynamically on my page?
example:
I have a form that has a data-driven select list that a user can select a
clientname from. Upon doing so, I run a javascript function called
displayValues() that populates a number of text boxes in this form with
data-driven information based on the id of the client name selected.
I think if there was a way to stuff my $array variable into a javascript
array variable, I would be able to pull this off.
Any Ideas?
SAMPLE CODE:
//GRAB DATA
$sql = "SELECT * FROM CLIENT";
$connection = mysql_connect(localhost);
$result = mysql_db_query($database, $sql, $connection);
$numClients = mysql_num_rows($result);
// DEFINE VARIABLES
$array = mysql_fetch_array($result);
$clientID = $array[ID];
$address1 = $array[ADDRESS1];
$address2 = $array[ADDRESS2];
$city = $array[CITY];
$state = $array[STATE];
$zip = $array[ZIPCODE];
$phone = $array[PHONE1];
$email = $array[EMAIL];
$contact = $array[MAINCONTACT];
$title = $array[MAINCONTACTTITLE];
echo "
<SCRIPT LANGUAGE=\"JavaScript\">
<!--
function displayValues()
{
for(count=0; count<$numClients; count++)
{
if (document.form.clientName.selectedIndex == 0)
{
clientAddress1 =
document.form.clientName.options.text = '';
clientAddress2 =
document.form.clientName.options.text = '';
clientCity =
document.form.clientName.options.text = '';
clientState =
document.form.clientName.options.text = '';
clientZip =
document.form.clientName.options.text = '';
clientPhone =
document.form.clientName.options.text = '';
clientEmail =
document.form.clientName.options.text = '';
clientContact =
document.form.clientName.options.text = '';
clientTitle =
document.form.clientName.options.text = '';
}else{
document.form.clientName.selectedIndex == count
clientAddress1 =
document.form.clientName.options.text = '$address1';
clientAddress2 =
document.form.clientName.options.text = '$address2';
clientCity =
document.form.clientName.options.text = '$city';
clientState =
document.form.clientName.options.text = '$state';
clientZip =
document.form.clientName.options.text = '$zip';
clientPhone =
document.form.clientName.options.text = '$phone';
clientEmail =
document.form.clientName.options.text = '$email';
clientContact =
document.form.clientName.options.text = '$contact';
clientTitle =
document.form.clientName.options.text = '$title';
}
}
document.form.address1.value = \"\" + clientAddress1 + \"\"
document.form.address2.value = \"\" + clientAddress2 + \"\"
document.form.city.value = \"\" + clientCity + \"\"
document.form.state.value = \"\" + clientState + \"\"
document.form.zip.value = \"\" + clientZip + \"\"
document.form.phone.value = \"\" + clientPhone + \"\"
document.form.email.value = \"\" + clientEmail + \"\"
document.form.contact.value = \"\" + clientContact + \"\"
document.form.title.value = \"\" + clientTitle + \"\"
}
//-->
</SCRIPT>
";
?>
</head>
<body>
<?
////////////////// FORM WOULD GO HERE /////////////////////
////////////////////FORM: END /////////////////////////////////////
?>
</body>
</html>
Jason Hoover
Entolo
2299 Territorial Road
St. Paul, MN 55114
Phone: 612.847.8022
Fax: 651.642.1600
jhoover@entolo.com
www.entolo.com