Display data from dropdown in another div - php

I have a dropdown with a list of values that are pulled from a MySQL database. Each of these values has other corresponding attributes in the database. This is the structure of that table:
id | name | password
The dropdown values are basically just all of the values in the "name" column.
What I'd like to do is display the id/name/password for a selected dropdown value in a separate div. That is, if I select "MIT" from the dropdown, I'd like the div to show me the id and the password associated with MIT. If I select "Harvard", I'd like the div to show the id and password associate with Harvard.
I am just looking for high-level suggestions on how I should approach a setup like this.
I was thinking of using AJAX and passing the selected value into a separate PHP file, which would then pull and display the associated ID and password. The div would then contain code to make a call to that PHP file and display the values on that page.
I think it'd work in theory, but it seems a bit cumbersome...any ideas for simplifying the process?

You could use data-attributes on your options. This would expose the passwords through the markup, but since they are accesible anyways through choosing the option in the dropdown, I guess this isn't an issue.
If you gave each option an attribute data-password="thepassword", then you could $("select").find(":selected").data("password") to retrieve the password. Do the same with a data-id attribute, and you're golden.
Another solution would be to have a JSon structure with the option values as keys. Let's say {"MIT":"1234","Harvard":"verysecret"} and then use $("select").change() and $(this).val() to retrieve the corresponding passwords (and other data) from you JSon structure.

Related

How to get the data in a text field based on the value in a dropdown box which are populated from database?

Please refer the attached image.
We have the document_id and document_name in document_master table.
Now, when we select the document name from the dropdown, we need to display the corresponding document_id (for the selected document name) from the document_master table.
Can you please help us how to fix this?
you can always do an AJAX call to retrieve the related data from database. But, isn't it weird to retrieve the doc_ID based on doc_Name? usually its the other way around

MySQL fields in URL

Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.

fill textbox with values based on selected table cell value (ajax call & mysql)

I have a table which is populated by values obtained from mysql database. I want to use an ajax call function so that when I click a cell - based on the cell value the textboxes get filled. Now the data in the textboxes will be from another table in the mysql database.
So an example of how it would work is:
I have one table for credentials, which shows user name and their user id.
On the website this is shown as a table. Now when i click the a cell which has a user name. The textbox would be populated by the users say total exams taken which is obtained from exams table in the mysql.
I am totally stumped on how to go about doing this. Please can someone help!
You can do this by using ajax .
use this
$("#cell").click(function(){
$.get("file.php?id="+$(this).val(),function(data){
$("#textboxid").val(data);
})
})
the file.php is the file which will perform the back-hand calling to database and make a output.

How to handle $_POST results of autosuggest multi-input on form

I have the following JsFiddle to help with my question:
http://jsfiddle.net/eY2yH/3/
I am ultimately trying to create a simple input field which allows a user to enter multiple 'names' which creates a message to those users on our system. In many ways, this should function EXACTLY as Composing an email in Gmail (enter text for a user name/email, the autosuggests appears, then allow for additional input or submit).
When I run the code given, the results of the $_POST are quite strange.
Depending on which members are selected, the value of the key changes:
array([as-results-0XXX]=>1,2,3,4)
where XXX varies and 1,2,3,4 would represent the value of the members selected. These values are stored in a single element and separated by comma. In the end, I want to run through a loop for each user selected and send the necessary alert:
If 3 members are selected, e.g.
foreach($rows as $row) {
do table insert with particular value
}
My site is built with php and clearly using jquery. I have done a bit of searching and can find nothing regarding multiple inputs on the same autosuggest field.
You can change the way the name of the hidden input is determined by adding the asHtmlID attribute to your .autoSuggest():
$("input.autoSuggest").autoSuggest(data.items, {
selectedItemProp: "name",
searchObjProps: "name",
asHtmlID: "custom_id"
});
The variable posted to your php script will be available through $_POST['as_values_custom_id']

Store dynamic form fields in database

I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.
I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.
Thank you!
So it's very dynamic here and I don't
know which would be the best approach
for storing every form field in
database.
I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:
If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.
Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".
When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.
If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.
If you can post in a specific example of something you don't know how to do, that would be useful.
You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.
If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.
Note: this method only working when you don't need to search for specific items in your database.
Edit: maybe i'm misunderstood your problem.
You can create a 'metadata' table like this:
form_id | option_name | option_value
---------------------------------------
1 | hotel | true
1 | restaurant | false

Categories