On my page I have a dropdown menu with a list of countries, this is a page for users to update their address details so I would like to have the "selected" value when they visit the page set to what the entered upon signup.
I have
<option value="254" >Afghanistan</option><option value="255" >Albania</option>
Etc as my HTML markup and in the database the user's country is stored as the code like in the markup.
Normally this would be simple but as it is such a vast list of countries I can't write php code in each one manually.
Thanks
You tagged the question as MySQL.
The answer is there:
Make a table called countries
Table countries
---------------
id integer auto_increment
name varchar(255)
code char(3)
....
And run a query like
SELECT name FROM countries ORDER BY name
Use the output in your dropdown.
If you are using PHP to create the drop-down, simply insert the "select" test into the loop. (If you aren't, then you might want to - create an array of country vals or get from DB and create the options with a PHP loop.)
Related
I have two table program(p_id(pk)AI, program_name and another table graduate_survey(id(pk)AI,total_PO1,total_PO2,session,p_id(fk). In the program table values are already inserted as
p_id Program_name
1 B.tech CSE
2 B.tech IT.
..so on. I select from a drop down list B.tech CSE and then redirect to the survey form. I enter the total_PO1, total_PO2 there and submit it.Now i want to insert in the graduate_survey table where in the fk field the p_id of B.tech CSE should automatically enter so that i can know the survey is done for which program..Is there any query in MySQL to do that? The insert operation should be done in php code.Please suggest any query.
Your approach seems to be incorrect. When you generate your dropdown menu in HTML, then the display value (the value that the user actually sees and selects) should be the program name, but the underlying value should be the id of the same record.
When you send the data to the server, then you should send the underlying id value, not the display value. Using HTML's standard dropdown menu, you would create sg like this as an output:
<select name="programs">
<option value="1">B.tech CSE</option>
<option value="2">B.tech IT</option>
</select>
If this control is submitted via a form, then the browser will send the value of the selected option automatically. If you use ajax, then your code has to retrieve the selected value of the select element. In the php code you just insert the underlying value into the graduate survey table.
I am using phpgrid in my project and i wanted to see a dropdownlist to be populated based on the condition.
When i click on the add buttoon in phpgrid, it will allow me to select the Country and based on the country selected, the dropdownlist to be generated which belonging to the selected country.
In my MySQL database i have a location tables separated with parent and child columns and i can able to see the proper result in mysql views. The same thing i wanted to implement with phpgrid.
Ref: http://phpgrid.com/
Have you read http://phpgrid.com/example/nested-dropdown/ ? Seems what you are looking for.
I would like to populate a drop down list of moods and save it to my database with a time stamp. And at the same time connect the moods with the current user. How should I approach this?
In my database, I have a table called "checkin" which includes: ID, Time, FKMoods (foreign key to list of moods) and FKUsers(foreign key to users).
Right now I have a form called checkin in "page.php":
<form id="checkin" action="checkin/checkin.php" method="POST">
<label for="checkinMood">Select your current mood :</label>
<select id="checkinMood" class="SelectMood"></select><br />
<input type="submit" id="checkin-button" value="Update!" />
Steps to accomplish this
Select the moods from a moods table
Loop through the results and put them into a select input
Upon submitting, update the given mood into the user table
Don't save the html option list to the database. It's too hard to edit when you want to change the options. Store a list of moods like this:
CREATE table moods
mood_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
mood VARCHAR(45);
To make the option list, query the mood table and loop through the results creating options as you go:
<option value='{$mood["mood_id"}'>{$mood["mood"]}</option>
To store the user's mood, update the users table with the mood_id.
Do a joined sql query to pull the foreign keyed values/rows from other tables first - assuming you are using mysql - using very basic functions everyone knows (without going into whether you are using mysqli or any pdo or lib) :
SELECT * FROM checkin c LEFT JOIN listofmoods lm ON c.FKMoods = lm.FKMoods LEFT JOIN listofusers lu ON c.FKUsers = lu.FKUsers ORDER BY c.ID ASC
(or however you want to sort)
Now you got all the fields you need. then run a mysql fetch to create the list, something like :
$query=mysql_query(-thequery listed above-);
while($fetch=mysql_fetch_assoc($query))
{
$moodselect.='<option value="'.$fetch['FKMoods'].'">'.$fetch['whateverfieldlabelthemoodnameyouhaveonmoodstable'].'</option>';
}
this will pop you a $moodselect with the necessary options being there. you can then shove this in to the select you have in your example.
you would probably have to put in the user id in some hidden field in your form. or if your user system recognizes the user automatically from session you can just use it when the form is posted to checkin.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.
I'm trying to make an Ajax search form - This form will simply search the database for names.
The table design is as follows:
id name age
1 some name 10
2 some name2 11
3 some name3 12
Each name is associated with a unique ID.
I want to create a drop down menu of top hits when the user enters their search string.
Also, somehow in the background i would like to retrieve the associated ID with each result as well. That ID will be POST'ed to a form say
myform.php
Now, my question is as follows:
1) How do i create the drop down menu?
I believe i can follow this tutorial and simply customise it to create a drop down menu.
http://www.w3schools.com/ajax/ajax_aspphp.asp
This would be fairly simple.
2) The main question is, how do i keep track of the ID and selected name, so that i can POST that ID to myform.php
Thanks.
<select name="carlist">
<option value="id">name</option>
</select>
Keep id and name like this.
on posting id is posting , you can get name from id .
I hope this is what you are asking.
But i dont find drop down in that link you provide