Click to See Complete Forum and Search --> : trouble copying a php array to a javascript array


Natcon67
05-12-2005, 06:13 PM
hello,
I have read through this (http://www.webdeveloper.com/forum/showthread.php?t=48140) post utilizing NogDog's suggestion but I am getting an error.

The error says "';' missing before statement" and it is pointing to the 4th line of the PHP code below.

here is the code:

print "<script type='text/javascript'>\n";
print "var customer_info = new Array();\n";
foreach ($custArray as $key=>$row) {
print "var customer_info[$key] = new Array();\n";
foreach ($row as $id=>$row2) {
print "customer_info[$key][$id] = $row2;\n";
}
}
?>
function populateCustomer(control) {
var form = document.loadDetail;
var test = form.elements[control+'Company'].value;
form.elements[control+'Address1'].value = 'something else';
}
<?php
print "</script>\n";


when i view the page source, this is what it outputs...

<script type='text/javascript'>
var customer_info = new Array();
var customer_info[2] = new Array();
customer_info[2][address1] = 1234 some address;
customer_info[2][address2] = ;
customer_info[2][city] = AURORA;
customer_info[2][state] = CO;
customer_info[2][zip] = 80012;
customer_info[2][email] = Natcon67@hotmail.com;
customer_info[2][phone] = 1231231233;
var customer_info[1] = new Array();
customer_info[1][address1] = 1234 some address;
customer_info[1][address2] = ;
customer_info[1][city] = FREDERIKSTED;
customer_info[1][state] = VI;
customer_info[1][zip] = 00840;
customer_info[1][email] = nathanc@nhba.com;
customer_info[1][phone] = 1231231234;
function populateCustomer(control) {
var form = document.loadDetail;
var test = form.elements[control+'Company'].value;
form.elements[control+'Address1'].value = 'something else';
}
</script>


Thanks for any help you can give.

Nate

ShrineDesigns
05-12-2005, 09:04 PM
try<?php
echo "<script type=\"text/javascript\">\n";
echo "var customer_info = new Array();\n";

foreach ($custArray as $key => $row)
{
echo "var customer_info[$key] = new Array();\n";

foreach ($row as $id => $row2)
{
echo "customer_info[$key]['$id'] = '$row2';\n";
}
}
?>
function populateCustomer(control) {
var form = document.loadDetail;
var test = form.elements[control+'Company'].value;
form.elements[control+'Address1'].value = 'something else';
}
<?php echo "</script>\n"; ?>

Natcon67
05-13-2005, 12:45 AM
ShrineDesigns,
Thanks for the suggestion but I am still getting the same error.
Any other ideas?
On another note, is it just a personal preference whether to use echo or print? I noticed you changed it to echo and I have never had a good answer which one to use.

NogDog
05-14-2005, 11:26 AM
Are you receiving a PHP parse error or a Javascript error? My guess would be a JS error since (1) You are actually generating the JS code to your page source and (2) I suspect the lines which have nothing on the right side of the equal sign would be JS errors. However, I would have thought using SD's suggestion of quoting the assigned values would have taken care of that.

As far as the difference between echo and print: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 (probably 99.9% of the time it does not really matter which you use.)

NogDog
05-14-2005, 11:28 AM
PS: you might want to check with the JavaScript gurus if that is the right way to declare and assign multi-dimensional arrays.

Natcon67
05-14-2005, 01:26 PM
NogDog,
It is not a php parse error, it is definately a JS error.
Thanks for your help... I will post this on the JavaScript forum also to try to get some help.

Thanks again