Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating A Form 2

Status
Not open for further replies.

overyde

Programmer
Joined
May 27, 2003
Messages
226
Location
ZA
Hi,
I have a form for users to update their information.
My problem is that if a user does not want to fill out a particular field in the form it still updates the form with a blank value. How do I organise it that if a field in the form is left blank then the info in that particular field in the database will be left untouched?
 
don't really get what you mean

You mean if the user doesn't fill out Name for example, it's not entered into the database?

Maybe something like

If($name ==""){
$name = NULL;

}
???
or

if($name ==""){
$sql = Insert into table values('id','NULL');
}
else {
$sql = Insert into table values('id',$name);
}

assuming the second field being the name field

 
or you can use javascript.
the advantage is that it is client side, therefore it reduced server traffic.

do u know how to use JS?

Known is handfull, Unknown is worldfull
 
overyde,

The best solution is to present the form with the current values filled in. It is most user friendly and intuitive.
What should I think about an empty field, how would I know it will not change what's in the database, or how would I know what is in there?

All your problems go away when you populate the fields with the current settings.
If you need help on that, let us know.
 
You got it DRJ478!!!
That would be absolutely perfect.
Now how do I get those fields to be filled in?

I'm thinking it would probably be through utilising a session variable????
 
select all the values from the database
fetch the row

then print out the result

example in your edit form :

<input type = text name = f_name value=&quot;<?php echo $resultrow[&quot;fieldname&quot;]?>&quot;>

 
Namida has given you the right advice:

Just prefill the value attribute in the input tags with the values retrieved from the database.

What to keep in mind:
Process your form and validate all input.
In case there's invalid input remember to fill in the POSTed value instead of the database values.
Users hate to retype what they changed and it's good practice.

My suggestion:
Code:
# I assume the database row is in $row
# on first pass use dbRow
if (is_array($_POST)){
   $myValues = $dbRow;
} else {
   $myValue = $_POST;
}
Then you can just refer to
Code:
$myValue['fieldname']
when populating the form.
 
Be careful that you populate the fields with information that does not pose a security risk.

For example if someone enters only an email address, you should not display a wealth of information linked to that address. This is fodder for social-engineering.

I received messages from amazon.com because one of their customers was incorrectly entering an email address that belonged to my own domain. I used the email address to access the customer's account on amazon and put a bunch of perverse literature on their wish list for the next time they logged in.

If populating fields from stored records, ask for two pieces of identification (often a username and password) before querying the database.

- - picklefish - -
 
You guys are right on the money!!!
But I used a predefined session variable instead of just displaying the field name. I think that this may make it just a bit more secure.

Thanks ya all!!!
 
DR J478:
I would like to ask regarding your code
Code:
# I assume the database row is in $row
# on first pass use dbRow
if (is_array($_POST)){
   $myValues = $dbRow;
} else {
   $myValue = $_POST;
}

1 .Does it mean that the first pass the $_POST is an array? the first pass means when the user just access the update form, right? So wouldn't $_POST be empty? *I'm sorry I'm slow here*

2. for example I have a confirmation page and if the user chooses to edit it/there's an error goes to another page which has the original Post value.

Is there any way I can transfer the Post values from the confirmation page to the edit page without using

input type again for every value?

Or is it better that I record it in a new session variable?

3. Is it best that I have a page specifically for the edit or send the user back to the sign up and put an 'if' to check whether it's edit or first pass. I am afraid the file will be large since the form is quite complicated



Regards,

Namida
 
I'm now putting the value reposted into a new session variable called $_SESSION['postvar']
and when the edit page shows up it transfers it into $postvar for example.

The thing is I have arrays in the post as well like check boxes, how do I refer to them?

$postvar[&quot;choice[$i]&quot;] --> is this possible?

Regards,

Namida
 
ooh never mind about the array thing (last post)

I just assigned the elements that are array to its own variable.

But if anyone knows a shortcut it would be great as well!

:)

thanks!

Regards,

Namida
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top