Jump to content

Passing array from php to javascript using html button


ajeesh.tr

Recommended Posts

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...

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.