Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to put php code in javascript?

    can anybody teach me how to include a php code in a javascript?

    Code:
    	function addRow()
    {
    	
    	var tbl = document.getElementById("tableId");
            var row = document.createElement("tr");
       
            var td = document.createElement("td");
            var sel = document.createElement("select");
    	var opt=document.createElement("option");
            var theText=document.createTextNode("<?php echo 'hello';?>");	
    
       
    	opt.appendChild(theText);
            sel.appendChild(opt);
    	td.appendChild(sel);
            row.appendChild(td);
    	tbl.appendChild(row);
    instead of just showing hello i want to show the items that are from my database. how do i do that or is it even possible?

    sorry im just a beginner regarding php and javascript.
    Last edited by evilbugoy; 07-09-2012 at 05:22 AM.

  • #2
    New Coder
    Join Date
    Oct 2009
    Posts
    61
    Thanks
    2
    Thanked 1 Time in 1 Post
    Since PHP is server side, that code should work, and would appear as var theText=document.createTextNode("hello"); on the client side.

    If you need help with how to pull data from your database, that's not a Javascript issue

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Sing View Post
    Since PHP is server side, that code should work, and would appear as var theText=document.createTextNode("hello"); on the client side.

    If you need help with how to pull data from your database, that's not a Javascript issue
    Thanks Sing...

    I know how to pull the data from my database but i dont know how to have the data that I query to be put in the options of the select that i made...

    Code:
    <?php 
    					//select data in the database and view it in the drop down list
    					$product_name=$_GET['product_name'];
    					$fa1="SELECT * FROM product_details WHERE product_name = '$product_name'"; 
    					$ff1=&dbQuery($fa1);
     					echo mysql_error();
     					extract(dbFetchAssoc($ff1)); 
    					$a="SELECT * FROM product_details";
    					$b=mysql_query($a) or die("Could not get data");
    			
    				while($c=mysql_fetch_array($b))
        			{
    					echo $c['product_name'];
           				print "<option value='$c[product_name]'>$c[product_name]</option>";
    				}
    			?>
                
                	<option ><?php echo $product_name;?></option>
    i have this code in my program and it works fine if i manually created the select box.. but on the dynamically("javascript") created select i dont know how to include this code...


    P.S. if I'm posting in the incorrect forum could you point me to the correct part of codingforums.com where i can place my problem?

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    anybody know how to do this? or where i should be looking?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •