...

View Full Version : How to put php code in javascript?



evilbugoy
07-09-2012, 03:49 AM
can anybody teach me how to include a php code in a javascript?



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?:confused:

sorry im just a beginner regarding php and javascript. :(

Sing
07-09-2012, 05:10 AM
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

evilbugoy
07-10-2012, 03:29 AM
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...



<?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...:confused:


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:thumbsup:?

evilbugoy
07-12-2012, 02:49 AM
anybody know how to do this? or where i should be looking?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum