Forum Moderators: coopster

Message Too Old, No Replies

Posting a blank field in a form when editing a profile

         

tec4

6:30 pm on Nov 1, 2011 (gmt 0)

10+ Year Member



Hey Everyone,

Had a quick question.

To start, I'm trying to allow a user to be able to edit his/her account information via a form. But I am having a problem when it comes to allowing the user to have the ability to completely leave a input value blank and have that post to the database.

Currently I have the form automatically populate the user's information inside of the input box upon the page loading like so:

<tr>
<td>First Name:</td>
<td><input type="text" name="first_name" maxlength="30" value="<? echo $user_info['first_name']; ?>" /></td>
</tr>

The above snippet populates the users first name into the input box but if the user were to delete his/her first name and push submit, it would not update the account to have no name, but instead disregard the blank field and leave the users name in the database - if that makes sense.

The above is merely an example as they should obviously have a nave in the DB.

Summary:How would I be able to populate the input with the users current information from the database (which it does currently) but allow them to delete the value entirely from the form and delete the current value from the DB?

Thanks! and let me know if that made sense

londrum

6:39 pm on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try changing it to

if(isset($_POST['first_name'])) { echo $_POST['first_name']; } else { echo $user_info['first_name']; } 


or maybe $_GET instead of $_POST, if your form is a GET

tec4

9:41 pm on Nov 1, 2011 (gmt 0)

10+ Year Member



Awesome, thanks londrum :)

rocknbil

4:24 pm on Nov 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes but that will still put what's in the form into the database, right?

but allow them to delete the value entirely from the form and delete the current value from the DB?


You'd just enter whatever is submitted, if it's blank, update it.

$formvars = array('first_name','last_name','email');

$id=$_POST['user_id'];

if (! is_numeric($id) or ($id ! > 0)) {
die("No user id to update");
}

$query = "update table set ";
foreach ($formvars as $k) {
$query .= "$k='" . mysql_real_escape_string($_POST[$k]) . "',";
}

$query = rtrim($query,',');

$query .= " where record_id=$id";