| | |
merge javascript with php...
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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.
PHP is executed on the server. JavaScript in a Web page is executed in the user's browser.
Last edited by TopDogger; Jun 29th, 2006 at 11:04 am.
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:
This generates the following client-side JavaScript array:
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.
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:
PHP Syntax (Toggle Plain Text)
<?php 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"; ?>
PHP Syntax (Toggle Plain Text)
var bNames = new Array("book name 1","book name 2","book name 3","");
Last edited by TopDogger; Jun 30th, 2006 at 11:26 am.
![]() |
Similar Threads
- How to call a PHP function from Javascript and return the results back into Javascrip (PHP)
- Pass Javascript variable to PHP code (PHP)
Other Threads in the PHP Forum
- Previous Thread: Mambo Administrator Problems - Page Not Found
- Next Thread: php + Javascript
| Thread Tools | Search this Thread |
ajax apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time display dynamic echo email error file files folder form forms function functions gc_maxlifetime global google host href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail memmory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php problem query radio random recourse recursion regex registrationform remote script search seo server sessions sms soap source space sql static syntax system table tutorial update upload url validator variable video web webdesign wordpress xml youtube





