LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2008, 02:55 PM   #1
fatrandy13
Member
 
Registered: Jun 2004
Location: new jersey
Distribution: anything debian based, long live apt-get
Posts: 230

Rep: Reputation: 30
PHP code inside a javascript function


I have a PHP page: page.php

At some point in the file there is a javascript function.

Code:
function popAirType(){
     var db = document.forms[0].db.value;	
     AddToOptionList(document.forms[0].air_type, "","Select Airplane Type");

     <?php $query_string = "SELECT distinct air_type FROM my_table";

     $air_types = mysql_query($query_string);

     while ($air_types_row = mysql_fetch_array($air_types)){
          echo "AddToOptionList(document.forms[0].air_type, '" . $air_types_row['air_type'] ."', '" . $air_types_row['air_type'] . "');";
     }
     ?> 
}
Now, this code works great. i loop through the results of the query and (in php) echo out the appropriate javascript code to create the options in an html select box. No problems here.

What i'd like to do is add a WHERE clause to the query.

WHERE db_version = '(in here i want the javascript variable db)'

As you can see, I declare the variable "var db" at the first line of the function.

Is there anyway I can put the javascript variable inside the query I am running??
 
Old 03-11-2008, 03:11 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

Remember PHP runs on the server and Javascript on the client. So you need to send the javascript variable as a parameter to the PHP script. For example:

Javascript:

Code:
window.location = 'page.php?db=' + db;
PHP:

Code:
$query_string = "SELECT distinct air_type FROM my_table WHERE db_version='".$_GET['db']."'";
Edit:
It's a bit complicated, but Javascript can also read XML data from a web-server. So you can send requests to the server with parameters and get back data as XML. You can get then it into variables in Javascript, and use it for e.g. setting the option lists of a select in a form. If you are interested, I suggest reading about Ajax.

Last edited by Guttorm; 03-11-2008 at 03:21 PM.
 
Old 03-11-2008, 05:18 PM   #3
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Rep: Reputation: 15
AJAX is what you want. It will allow you to call php code from javascript (as long as the php code is in its own file). However, you must decide on an event that you want to trigger the php code with. For instance, when a user hovers their mouse over something or when they type something into a field. When you read up on AJAX all of this will make a bit more sense.
 
Old 03-12-2008, 10:36 AM   #4
fatrandy13
Member
 
Registered: Jun 2004
Location: new jersey
Distribution: anything debian based, long live apt-get
Posts: 230

Original Poster
Rep: Reputation: 30
well, its all happening dynamiclly, so "GET" or "POST" data is out of the question.

there are multiple select boxes. values of these boxes are dynamicly generated based on the value of previous boxes...

so when i click the value of box 1, i want it to query the database with that value, then populate the next box with the results...

i have it working great execpt that i cant add the WHERE clause with javascript data, as described above


as for using AJAX, is there anything more concrete than "AJAX is what you want"...
what you described: "It will allow you to call php code from javascript".

i am already doing that in the code i posted..


thanks for the effort
 
Old 03-12-2008, 05:57 PM   #5
karpi
Member
 
Registered: Oct 2005
Location: Germany
Distribution: Suse
Posts: 134

Rep: Reputation: 15
Hello,

there is a framework for this.

Have a look at http://www.xajaxproject.org

They have an active community and some good examples.

HTH
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
communicate a php variable to a javascript function Kanaflloric Programming 4 08-11-2006 12:58 PM
Use PHP code inside itself Ephracis Programming 4 04-03-2006 08:33 AM
function defn inside a function in C...!!! arunka Programming 1 02-05-2006 01:08 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
PHP has an easter egg inside (image code) micxz Linux - Software 0 09-18-2004 08:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration