PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Sorry for my stupid question but I am stuck with this. I have a form with textfields and drop down list. I also have 2 buttons. The first one makes fields and drop down list enable for the user and the other one must save the changes. The fields and the drop down list take the values from my database and show them to the user. The page is about the user profile and what I want to do is to give all the information to the user and let him change it (by pressing the edit button) and save it to the database. In every textfield I have a code like the following:
Which takes the username from the database and show it to the textfield.
I must store in a session variable the new values that the user will post in the form and then in my pofile_updated.php file make the connection to the database and the sql update statement?
Can you help me a little with the procedure in order to make this happen??
<?php
// assume this is your drop down
<select name="pseudolist">
<option value="1">One</option>
<option value="2">Two</option>
</select>
//
$option = $_POST['pseudolist'];
?>
The name of the select element holds the selected value.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
I have already done that but for some reason it doesn't store the last value which is selected by the user after edit button pressed. But it stores the default value which takes from the database. How can I store the last selected value of my drop down list?
Is there a specific place where I have to put my php code in order to store the last selected value or it doesn't matter?
Typically you would use a class for this, a standard one you use for all forms, so it keeps the code tidy as this is messy... but for what you wish to do, this will suffice.
[/syntax]
With the above php code I create a drop down list with all the hospitals I have in my database and also I choose the hospital in which the specific user (the user which is registered and see his profile details) works to be the default value of my list. What I want after is to let user change this value (if someone change work place).
For that purpose I have 2 buttons. The Edit button which make all the form elements enable in order to let the user choose or change values of his personal info and another one which is the save button which must save these changes to the database.
I don't know if I am completely clear to you that's why I send you this extended message.
I have been searching through a javascript solution to my problem as you recommend. What I finally manage to do is to create a javascript function which holds the value selected by user.
var selected;
function outputText(o) {
alert( o.options[o.selectedIndex].value );
selected=o.options[o.selectedIndex].value;
}
This function is called inside my select code onchange event. Every time the user changes the value of the drop down list, the value is shown in an alert window. Now what I have to do is to connect javascript(client side) with php(server side) in order to pass the value of my javascript variable to a session variable. One way to do so is by using ajax but I have no knowledge to it. I have read that another option is by creating a hidden form element and storing the value there..I don't know if I have understand correctly. If anybody knows how to do that I will appreciate if you give me some details.