Forum Moderators: coopster

Message Too Old, No Replies

php file as javascript source

         

dmmh

3:27 pm on Sep 15, 2005 (gmt 0)

10+ Year Member




im using a few javascript functions which are generated by mysql queries via php, this works fine

in the php file used as source for the javascript, ive defined javascript functions which add textfield's dynamically to the form if a user clicks a button. This works fine as said.
However, the menu's wont show.
The php generated javascript functions for the menu's are inside a include, which is included in every file on the site and returns the output (it doesnt echo)
I echo it by calling the it like 'echo $dyn_form_menu', which works fine if I use it as part of html output, but it doesnt output anything in the javascript include

any ideas? must be something simple....

coopster

12:07 am on Sep 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP has to be printed out by the server, javascript will not process any server-side functions as it is already client-side by that time. You can set variables and then use an include to pull HTML code into that portion of your script, but it has to happen server-side via the PHP parser before it gets back to the browser. If I am misunderstanding you here, please elaborate and we'll go from there.

dmmh

5:19 am on Sep 27, 2005 (gmt 0)

10+ Year Member



well the problem was that I used PHP to generate javascript menu's and such

I use a PHP file as a javascript source, in it are 2 things: static javascript functions and dynamically generated ones

I use it like:

<script language="JavaScript" type="text/javascript" src="http://www.site.com/js/form_fields.php"></script>

example static in that file:
<!--
var newProducer = "";
function producerAdd()
{
newProducer+='<input name=\"producer[]\" type=\"text\" class=\"txtfield\"><br/>';
document.getElementById('producer_container').innerHTML = newProducer;
}
-->

a new form field is insert when the user clicks the button, calling the function producerAdd()

the dynamic javascript is a bit more complicated and I think I will need to let you see some code to understand it. It is used on the same pages as the static javascript function, but this is dynamically generated.
I display it in the HTML <head> part of a page simply by using <? echo $movie_country_java;?> now.
What I wanted to do is use it in inside the same javascript source I used like I mentioned before, but this is what I cant get to work.
It also inserts something into the form, this time a html select which is formed by PHP via MySQL queries, when a user clicks the button calling the javascript function countryAdd()

//MOVIE COUNTRIES SELECTOR
$movie_country_java.= "<script language=\"JavaScript\" type=\"text/javascript\">\n";
$movie_country_java.= "<!--\n";
$movie_country_java.= "var newCountry = \"\";\n";
$movie_country_java.= "function countryAdd()\n";
$movie_country_java.= "{\n";
$movie_country_java.= "newCountry+='<select name=\"movie_country[]\" size=\"1\" class=\"menu\">';\n";
$movie_country_java.= "newCountry+='<option value=\"\" selected>select</option>';\n";
{
@mysql_select_db('DB');
$query = 'SELECT movie_country FROM s1 WHERE movie_country IS NOT NULL ORDER BY movie_country';
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$movies_countries = array(); // initialize the countries array
while ($myresult = mysql_fetch_array($result)) // loop through and add each country to the array
{
$movie_countries[]= $myresult;
}
foreach ($movie_countries as $movies_country)
{
$movie_country = ucwords($movies_country[0]);
// build the options...
$movie_country_java.= "newCountry+='<option value=\"$movie_country\"";
$movie_country_java.= ">$movie_country</option>';\n";
}
// close the select tags
$movie_country_java.= "newCountry+='</select><br/>';\n";
$movie_country_java.= "document.getElementById('country_container').innerHTML = newCountry;\n";
$movie_country_java.= "}\n";
$movie_country_java.= "-->\n";
$movie_country_java.= "</script>\n"; }

how can I let this be echoed so that it appears as static? So it should just show in this file:

<script language="JavaScript" type="text/javascript" src="http://www.site.com/js/form_fields.php"></script>

mincklerstraat

7:35 am on Sep 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure if this will help but I'll give it a shot. You might need to send out the appropriate headers as well with your js. header('Content-type: text/javascript');
You'll find that John Cox, the guy who started PostNuke, has posted about this on his blog: [wyome.com...]