can javascript merge with php?...if so,how can we implement it...
i had a hard time searching on that problem....
i made experiments but it doesn't work...hehe;)

Recommended Answers

All 5 Replies

You can use PHP to generate JavaScript in a client side script, but if you are talking about getting PHP (a server side language) to talk to JavaScript as a client-side language, you cannot do it.

PHP is executed on the server. JavaScript in a Web page is executed in the user's browser.

Actually, you can code functions in javascript that talk to php. You send data and get a response. AJAX.

This whole forum uses that ability. But, as pointed out, the javascript runs on your browser, and the php on the webserver. But the two talk.

<"You can use PHP to generate JavaScript in a client side script">
how can i do that?..^__^

I sometimes find that I need to use PHP to generate a JavaScript array that is used by an application. In this situation, I am probably using data in a MySQL database to generate the array. This is no different than when you generate any other client-side code.

For example, if you had a MySQL table loaded with names of books that you want to use to generate a JavaScript array, you would do something like this:

  echo "var bNames = new Array(";

  $sql = "SELECT book_name FROM books ORDER by book_name";

  $rs = mysql_query($sql);

  while($row = mysql_fetch_array($rs))
    echo "\"".$row['book_name']."\",";
  echo "\"\"";
  echo ");\n";

This generates the following client-side JavaScript array:

var bNames = new Array("book name 1","book name 2","book name 3","");

Before you jump into using JavaScript heavily for Web applications, keep in mind that JavaScript should never be used for a critical function. According to the W3Schools surveys, up to 10% of users have JavaScript disabled in their browsers. I think this is mostly due to people who set the security level to high in Internet Explorer, which disables all active scripting, including JavaScript.

<"You can use PHP to generate JavaScript in a client side script">
how can i do that?..^__^

You echo out the javascript like any html. The example above while not complete, explains echo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.