I have a routine which gets member info from a database (works fine!) and puts the data into text boxes on a form (also works fine). The form has 2 buttons, one to update the details in the text boxes, the other to delete that member. The member’s unique ID is one of the DB fields returned (though it does not physically appear in a text box). A typical text box would contain the echoed value of a database field (also works), let’s say a variable $st which is the member’s street address.
So, can I use the variable $st as a session variable, assuming I have session_start() at the top of the file, then use that in the next page to update that member’s bit of data by retrieving it as a session variable?
Finally, what is stored in that session variable? Is it the string “$st” or its value (say ‘High Street’)?
PS - sorry, I can’t see the icon that lets you indicate a code snippet, hence the ‘over-wordy’ explanation.
From your previous thread -
You can assign any value to a session variable. This can be any expression that produces a value, e.g. a literal value, another variable, the result of an operation or comparison, or the result of a function/method call. The actual value is stored in the session variable.
What does using a session variable have to do with this operation? You should have two forms. The update form should have a hidden field with the id, a hidden ‘action’ field with a value that indicates that the ‘update’ form processing code is to be executed, and the form fields containing the data values. The delete form should have a hidden field with the id and a hidden ‘action’ field with a value that indicates that the ‘delete’ form processing code is to be executed.
The form processing code and the form(s) should be on the same page. This results in the simplest code and the best User eXperience (UX), since you can display any user/validation errors and repopulate the fields with the submitted/modified values when you redisplay the form.
The code for any page should be laid out in this general order -
- initialization
- post method form processing
- get method business logic - get/produce data needed to display the page, such as the initial data being edited/updated.
- html document
Yes, I have two forms. One features a dropdown box. The (visible) values in the dropdown box are members’ names, got from the DB that populates the box (this works fine).
Currently, on hitting a submit button, the name is passed to the sql query which then fills the text boxes on the 2nd form (again, this works fine).
However, what I want to get is the selected member’s unique ID to use as the session variable, because it is possible there may be more than one member with the same name, so not unique!
This can then be used on the 2nd form to update/delete that member. This is what I tried in the dropdown box:
echo "<select name='membox'><option value=''>Pick a Person...";
while ($row = mysqli_fetch_assoc($result))
{
$fullname = ucwords($row['firstname']." ".$row['lastname']." ");
$hisid = $row['idno'];
$_SESSION['id'] = $hisid;
echo "<option value='$fullname'>$fullname";
}
echo "</select></br></br>";
When I try this and echo the result for the $hisid variable, it always says 13 no matter which member I choose! I have no idea where this 13 comes from. Is there any way of getting the member’s ID into the option value… line?
I appreciate what you say about having the form and processing script on the same page, but at the moment I am trying to keep things as modular as possible to track down problems and avoid a page littered with error messages!
To select the user to edit and submit the member’s id to the next step, you would use $row[‘idno’] for the value attribute in the option tag. Using a session variable here is meaningless. The logic is storing the last (highest) $row[‘idno’] value in the session variable.
If you have multiple members with the same name, you would also display some unique value, such as a birthday, join date, or address as part of the option text so that you can uniquely select a member.
The member select menu needs be in a get method form, be ‘sticky’, and pre-select any existing choice, so that it will remember which user has been selected.
Cheers again! But isn’t value what the user sees in the DD box? I want the user to see his name (as currently), not a number which wont mean anything to him - so for every name there will be its matching unique ID, How will PHP know which to use from name or ID?
…or when the form is submitted, will there be a little array of name and ID? I can see that if one uses ‘get’ that will (in this context) be doing the same job as the session variable.
Aha!! Just thinking and my poor old brain has just realised something! If in the echo line above I write
echo "<option value='$hisid'>$fullname";
would that work? The user would see the name in the DD box as now, but the ID would be what was stored in the ‘get’ string! Go on, tell me I’ve got it