Page 1 of 1

Merging PHP with JavaScript..probs in passing variables.

Posted: Wed Nov 08, 2006 11:57 pm
by joboy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am merging php and javascript codes.. the former is working, i mean codes within <?php and ?> delimiters successfully 

retrieved data from a database and trasferred it to javacript variables. but looking at the codes below, the javascript 

variables: title, cmpny_name, intro, date, speaker, bio, venue_en, type, and agenda no longer have values when scripts 

in the second <script> and </script> tags  (under <html> </html> delimiters) are executed. my concern is how to pass values or variable 

form one <script> codes... </script> to  a succeeding <script> codes..</script> within a single php or html file? here 

is my scripts:

Code: Select all

<?php
   $id_meet = $_GET['id_meet'];
   require_once('connect_db.php');
   require_once('class_db.php');
   require_once('class_dcfa.php');
   $result = meeting::get_one($id_meet);  

   //this is the first <script> and </script> delimiters.

   echo "<script type='text/javascript'>";
   echo "var title, cmpny_name, intro, date, speaker, bio, venue_en, type, agenda;";
   if ($result != false)  {
      foreach ($result as $element => $value) {
             echo ("title=\"" . $value->title_en ."\";");
	     echo ("cmpny_name=\"" . $value->cmpny_name ."\";");
             echo ("intro=\"" . $value->intro ."\";");
	     echo ("date=\"" . $value->date ."\";");
	     echo ("speaker=\"" . $value->speaker ."\";");
	     echo ("bio=\"" . $value->bio ."\";");
	     echo ("venue_en=\"" . $value->venue_en ."\";");
	     echo ("type=\"" . $value->type ."\";");
	     echo ("agenda=\"" . $value->agenda ."\";");
      } # end of foreach loop.
   } # end ($result != false)
   echo "</script>";
?>
<html>
   <head>
      <title>Detail of Meeting</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
      
      // this is the second <script> and </script> delimiters.
      
      <script type="text/javascript">
	  <!--
	     var Ptitle, Pcmpny_name, Pintro, Pdate, Pspeaker, Pbio, Pvenue_en, Ptype, Pagenda;
		 
		 function meeting()  {
		    Ptitle = document.createElement("p");
		    Ptitle.innerHTML = title;
		    document.body.appendChild(Ptitle);
		 
		    Pcmpny_name = document.createElement("p");
		    Pcmpny_name.innerHTML = cmpny_name;
		    document.body.appendChild(Pcmpny_name);
			
			Pintro = document.createElement("p");
		    Pintro.innerHTML = cmpny_name;
		    document.body.appendChild(Pintro);
			
			Pdate = document.createElement("p");
		    Pdate.innerHTML = cmpny_name;
		    document.body.appendChild(Pdate);
			
			Pspeaker = document.createElement("p");
		    Pspeaker.innerHTML = cmpny_name;
		    document.body.appendChild(Pspeaker);
			
			Pbio = document.createElement("p");
		    Pbio.innerHTML = cmpny_name;
		    document.body.appendChild(Pbio);
			
			Pvenue_en = document.createElement("p");
		    Pvenue_en.innerHTML = cmpny_name;
		    document.body.appendChild(Pvenue_en);
			
			Ptype = document.createElement("p");
		    Ptype.innerHTML = cmpny_name;
		    document.body.appendChild(Ptype);
			
			Pagenda = document.createElement("p");
		    Pagenda.innerHTML = cmpny_name;
		    document.body.appendChild(Pagenda);
			
		 }	  
	  // -->
	  </script>
   </head>
   <body onLoad="meeting()">
   </body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Nov 09, 2006 12:28 am
by joboy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Oh, maybe i got dizzy of facing my PC's monitor while posting this concern of mine.. i think i got the solution to my 

probs.. the bug was that the variable cmpny_name was repeatedly use in the statement: variable.innerHTML = variable. 

anyway for those who view this thread..thanks for dropping by.

[syntax="javascript"]
...

Ptitle = document.createElement("p");
Ptitle.innerHTML = title;
document.body.appendChild(Ptitle);
		 
Pcmpny_name = document.createElement("p");
Pcmpny_name.innerHTML = cmpny_name; //here..
document.body.appendChild(Pcmpny_name);
			
Pintro = document.createElement("p");
Pintro.innerHTML = cmpny_name;  //here again..
document.body.appendChild(Pintro);
			
Pdate = document.createElement("p");
Pdate.innerHTML = cmpny_name;  //and again...
document.body.appendChild(Pdate);
			
Pspeaker = document.createElement("p");
Pspeaker.innerHTML = cmpny_name; //and again...
document.body.appendChild(Pspeaker);

...

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]