ajeesh.tr Posted May 3, 2011 Share Posted May 3, 2011 Hi All, Am new to this forum and also to scripting,I have some arrays in php, onclick function of the button in html should pass the arrays to javascript function which is in a ".js" file.The php and html code is in a ".php" file. Please help me on this... Quote Link to comment https://forums.phpfreaks.com/topic/235413-passing-array-from-php-to-javascript-using-html-button/ Share on other sites More sharing options...
spiderwell Posted May 3, 2011 Share Posted May 3, 2011 php doesn't talk to javascript and vice versa. what exactly are you trying to achieve Quote Link to comment https://forums.phpfreaks.com/topic/235413-passing-array-from-php-to-javascript-using-html-button/#findComment-1209815 Share on other sites More sharing options...
ajeesh.tr Posted May 3, 2011 Author Share Posted May 3, 2011 Am reading data from mysql database and saving it in an array,these data i need to display in a form in textboxes, the form management is taken care by the java script.In the form there is button, on clicking the button, i need to display the data in the textboxes. In the javascript i have taken care of displaying data,but am not able to pass array to the java script. Quote Link to comment https://forums.phpfreaks.com/topic/235413-passing-array-from-php-to-javascript-using-html-button/#findComment-1209817 Share on other sites More sharing options...
spiderwell Posted May 5, 2011 Share Posted May 5, 2011 you would have to create php code to echo out javascript code to do it, this function below is from a site i made (for fun!) and it takes a list form a database, and spits it out in different formats, www.copyandpastelists.com if you look i am using the php to write a small javascript array with values from a php dataset from mysql you should be able to acheive what you want in this manner, but of course it will need a rewrite to work exactly how you want it too function genItem1ListJS($id) { $i = 0; $rtnstr = ""; $rtnstr .= "<script type=\"text/javascript\">\r\tvar list=new Array(); \r"; if (is_numeric($id)) //check $id is numeric { $result = DbConnector::query("SELECT * FROM listitems WHERE `listid` = $id ORDER BY `order` ASC;"); while ($row = DbConnector::fetchArray($result) ) //list list data { $rtnstr .= "\tlist[" . $i . "]=\"" . $row['item1'] . "\";\r"; $i++; } $rtnstr .= "</script>\r"; return $rtnstr; } else //$id is not numeric { $this->errors[] = "Value of \$id was not numeric in ListControl->genItem1List()"; //Value not numeric! Add error description to list of errors return false; } } it turns out something like this : <script type="text/javascript"> var list=new Array(); list[0]="Monday"; list[1]="Tuesday"; list[2]="Wednesday"; list[3]="Thursday"; list[4]="Friday"; list[5]="Saturday"; list[6]="Sunday"; </script> Quote Link to comment https://forums.phpfreaks.com/topic/235413-passing-array-from-php-to-javascript-using-html-button/#findComment-1210694 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.