Jump to content

php javascript mysql connection


michaelfurey

Recommended Posts

This is my PHP code:

 

                echo "<table>";form($current_php);
                $cit=do_order("select * from country_itemize_types");
                echo "<tr><td>Description: </td><td><select name='descr_ger' id='descr_ger'>";
                
                $descr_sql=do_order("select distinct description from country_itemize_types");
                while ($descr_arr=mysql_fetch_array($descr_sql)){
                        echo "<option>" . $descr_arr['description'] . "</option>";
                }
                echo "</select></td></tr></table>";

And this is my mysql table named country_itemize_types (the table has thousand rows but I pasted here only few lines...):

 

+-----+------+--------------+---------+------------+----------+----------+------------------+
| uid | type | description  | country |   field    | priority | datatype |      option      |
+-----+------+--------------+---------+------------+----------+----------+------------------+
|   1 |   12 | travelcost   | ger     | vehicle    |        1 | char     | car;bus;airplane |
|   2 |   12 | travelcost   | ger     | distance   |        2 | decimal  |                  |
|   3 |   12 | travelcost   | ger     | times      |        3 | integer  |                  |
|   4 |   12 | travelcost   | ger     | cost       |        4 | decimal  |                  |
|   5 |   12 | randomcost   | ger     | earthquake |        1 | char     | weak;strong      |
|   6 |   12 | randomcost   | ger     | seaquake   |        2 | char     | weak;strong      |
|   7 |   12 | randomcost   | ger     | cost       |        3 | decimal  |                  |
|   8 |   12 | marriagecost | ger     | times      |        1 | integer  |                  |
+-----+------+--------------+---------+------------+----------+----------+------------------+

I would like to create a select tag containing all the descriptions of the table (see the php code above) and when the user selects an option on this select tag should appear some input tag.

For example if the user selects from the dropdown menu "travelcost" should appear 4 new input tags (vehicle, distance, times, cost). If the user selects "marriagecost" should appear only one new input tag (times).

 

How can I do that? I suppose that I have to run a sql query with javascript or something similar but I am a newbie in javascript...

Link to comment
Share on other sites

What about doing it in stages?  Show the user the first dropdown only and let them select an option and click on a submit button. Then you receive their selection and output a 2nd page with that choice along with all the items that match it.  Sure - it will refresh the screen, but you can avoid the JS/Ajax step this way.

Link to comment
Share on other sites

I don't see any point to the first query. You're not doing anything with it unless there's additional code you're not showing.

 

Without JS/ajax, you'll have to create your first <select> similar to what you're doing.

 

Then, after the form is submitted, in your processing code, validate $_GET['descr_ger'] using the filter_* functions and add a WHERE clause to your query referencing the value. You'll want to check out PDO or the mysqli functions to create that query securely. Stop using the mysql() deprecated functions. From the results of that query with the WHERE clause limiting results to a matching descriptive value, you can create all of the other input boxes you want.

 

-John

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.