Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2006, 03:38 PM
Registered User
 
Join Date: Nov 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default php and javascript

I have an array in my PHP script, that I need to use in my javascript code, is there a way I can do this?

Thanks
-Josh

 
Old November 1st, 2006, 11:42 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

Hi friend,
Try this
<?php
$myarray=array("xx","yy","zz");
?>
<script language="javascript">
<!--//
alert('<?php echo $myarray[1]?>');
//-->
</script>

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old November 4th, 2006, 08:11 PM
Registered User
 
Join Date: Nov 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried:
<script language="javascript">
<!--//
alert('<?php echo $myarray[1]?>');
//-->

and that did show an alert window with the element in my array at space one. but how would I go about putting that element into a javascript array? I tried doing something like:
a[0] = <?php echo $array[0]?>; but that doesn't work.

thanks.

 
Old November 9th, 2006, 12:52 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

<?php
$array = array('one', 'two', 'three');
?>

<script language="JavaScript" type="text/JavaScript">

var xx=new Array("<?php echo implode("\",\"", $array); ?>")
//alert(xx[0]);
</script>

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old November 23rd, 2006, 02:31 AM
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hmm i read from the other forum some one post it like this, i think it should work fine though

<?
 echo "
 <script language="JavaScript">
  /*
  all the code you want with the php variables
  */
 </script>
 ";
?>





Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and Javascript davidhayter Beginning PHP 5 May 6th, 2005 05:33 AM
javascript with php kuehhc Beginning PHP 8 February 1st, 2005 10:59 PM
How to use javascript with php? raichand_ray PHP How-To 2 November 12th, 2004 06:41 AM
javascript and php? Ashleek007 Beginning PHP 21 September 23rd, 2004 01:40 PM
JavaScript and php Bandy PHP How-To 2 February 10th, 2004 04:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.