Hello,
I'm totally new to Drupal, but I have experience in programming and especially in web programming.
I want to do sth that in general is simple, but I can't find info on how to implement it in Drupal.
So, here it is:
I create a new page in Drupal. In that page I want to add a drop down list that will take values from a table in the database.
The code for this for a NON Drupal site is following:
require ("DatabaseController.php"); // Is a class that I use in all of my sites that has database methods.
echo "<select id='nomes' name='nomes' value='' onchange='ShowTowns()' style='width:150px;'>
<option selected>Επιλέξτε Νομό</option>";
$query = "SELECT * FROM districts;";
$database = new DatabaseController();
$database->Connect();
$database->Query($query);
while ( $row = $database->Fetch() )
{
$id = $row['id'];
$nome = $row['nome'];
echo '<option value="'.$id.'">'.$nome.'</option>';
}
$database->Close();
echo "</select>";
Question No1
The problem with this part of code is that require ("DatabaseController.php") doesn't work. But even if we suppose that is working, it is not convenient for me, cause if someone change the password of database, then it will have to make changes to my DatabaseController.php Class. So, I was wondering if there is a way to put database queries directly in my code using some of Drupal's built in functions like db_query and db_fetch_object. Is this possible?
//End of question No1
When a user selects a value from the dropdown list that created above, then the onchange event is calling a Javascript function called ShowTowns(). This sends with ajax the selected value to a php script. Finally this script responds with a new dropdown list that will show in div with id townsDiv.
Question No2
First of all I find it really difficult to implement a javascript function when the onchange event takes place. I tried drupal_add_js but doesn't seem to work. Can anyone help me about this?
//End of question No2
Question No3
Do I have to create a new module for this or I can implement it by just adding some php in my page throw Content Management in Drupal?
//End of question No3
Thanks in advance anyone who will help on this!
Comments
I found sth...
Ok, I wrote the following code in a module that creates the drop down list.
<?
But now how I'm going to call a javascript function? Should I create a .js file into my module folder? If that's the case, then how I include this .js file to my .module file?
Thx in advance!
Hey there, it's late, and I
Hey there, it's late, and I haven't read through your whole post, but this might put you on the right track.
How to query external DB with drupal:
http://drupal.org/node/18429
Table Wizard is also a module that's new on the scene, and it might work for you:
http://drupal.org/project/tw
I'd recommend googling it and watching a screencast, as that's the best way to see how it works.
To add JS, you use the drupal_add_js function:
http://api.drupal.org/api/function/drupal_add_js
This will either load the js file in the script tag at the beginning or inline, depending on the arguments.
Hope this helps, and sorry that I couldn't give a more thoughtful answer -- I'm really tired!
Pat
Thank you but can you help me with this?
Hello and thank u very much for your reply!
I want now to send a value with javascript to a php script lets say area.php which I have put in my module folder.
Javascript is working correctly (I tested using alert(myValue)), but when I send the value to area.php nothing is happening (I use $_GET['myValue']) and drupal returns: "Page Not Found"...
If you have any idea I would appreciate it a lot!
Thank u in advance!