Page 1 of 1
How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 8:42 am
by tsalaki
Hello to everyone,
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:
Code: Select all
<input name="username" type="text" disabled="disabled" class="textfield" id="username" value="<?php echo $row["username"]; ?>
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??
Thanks a lot
Re: How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 10:05 am
by social_experiment
Code: Select all
<input name="username" type="text" disabled="disabled" class="textfield" id="username" value="<?php echo $row["username"]; ?>"
If the value is in the textbox, you can use $_POST['username'] to access it on pofile_updated.php or where it is required
Re: How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 10:18 am
by tsalaki
And what about the drop down list?How can I store the value which is selected by user?
Thanks for your reply
Re: How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 1:29 pm
by social_experiment
Code: Select all
<?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.
Re: How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 1:37 pm
by tsalaki
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?
Thanks a lot for your responses
Re: How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 2:27 pm
by oscardog
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.
Code: Select all
<select name="pseudolist">
<option value="1" <?php if($_POST['pseudolist'] == '1') { echo 'selected="selected"'; } ?>>One</option>
<option value="2" <?php if($_POST['pseudolist'] == '2') { echo 'selected="selected"'; } ?>>Two</option>
</select>
Re: How can I save changes in a form to my db??
Posted: Sat Jul 09, 2011 3:15 pm
by tsalaki
I will attach the code of my drop down list.
Code: Select all
[syntax=php]
<select name="hospital" id="hospital" class="textfield" disabled="disabled">
<?php
$myquery = "SELECT * FROM hospital";
$res = mysql_query($myquery);
while($thisRow = mysql_fetch_array($res)){
?>
<option value="<?php echo $thisRow["hospital_id"]; ?>" <?php if($thisRow["hospital_id"] == $row["hospital_id"]) echo('selected="selected"'); ?>><?php echo $thisRow["hospital_name"]; ?></option>
<?php } ?>
</select>
[/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.
Re: How can I save changes in a form to my db??
Posted: Sun Jul 10, 2011 4:19 am
by social_experiment
tsalaki wrote:How can I store the last selected value of my drop down list?
Javascript might be an option to look into
Re: How can I save changes in a form to my db??
Posted: Sun Jul 10, 2011 2:32 pm
by tsalaki
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.