# Preventing form fields from posting



## cristobal03 (Aug 5, 2005)

I'll admit I might be thinking about this all wrong.

Here's the scenario:

I'm making our company's media pages database-driven, so I'm building PHP scripts and HTML forms to facilitate adding and modifying records. Each record has an attribute for title, author, source, body, etc. If a user wants to update a record, I grab that record and toss its contents into the form fields. But if the user doesn't change anything within a given field, I don't want to waste the transaction time to update record attributes that haven't changed.

Tentatively I've added an onchange property to every form field element which runs a simple javascript function to concatenate the element's name to a variable value *changedFields*, then I thought I'd dump that variable value onto the end of the form's action URI as GET data. But it seems it would be more elegant if I could just prevent the unchanged fields from posting.

Has anybody ever done something like that? Or does anyone have any ideas? Alternatives would be useful as well. Thanks.

chris.


----------



## harmor (Mar 15, 2007)

The first thing that I thought of was create hidden form elements to contain the value of visible forms.

For example here are two form elements, one is hidden and the other is visible.

```

```
The hidden element will always be the same so if someone changes their username on the visible form you can check them against each other.


```
$username = null;

if($_POST['hidden_username'] != $_POST['username'])
{
  $username = "username = '".$_POST['username']."'";
}

mysql_query("Update my_table SET $username ");
```


----------



## WendyM (Jun 28, 2003)

harmor said:


> The hidden element will always be the same


But once it's been changed, then you'd need to change the hidden element to reflect the new value in case it gets updated again, right?


----------



## harmor (Mar 15, 2007)

WendyM said:


> But once it's been changed, then you'd need to change the hidden element to reflect the new value in case it gets updated again, right?


Of course.


----------



## cristobal03 (Aug 5, 2005)

Thanks for the reply.

I thought of that as well, and (I guess I should have mentioned this in my first post) the only reason I want to prevent unchanged form fields from posting altogether is that I may extend the module to accommodate blobs like video/audio files. I haven't decided if I'll go that route, but I'd rather not send large chunks of data if they're identical to what resides on the server.

Really, though, for the current application I have no good argument against posting the entire form and updating every attribute in the source record unilaterally; the data set isn't so large as to be prohibitive. So I guess at this point it's almost an intellectual exercise.

Here's an idea: I could preprocess the form using javascript and onsubmit so that any element that hasn't been flagged as changed gets its value set to NULL. On the server side, if anything isNull (or whatever the PHP equivalent is), it gets disregarded. That might work fairly well.

Again, I'm just tossing out ideas at this point. Thanks for contributing.

chris.


----------



## MMJ (Oct 15, 2006)

```
<?php
$sql = mysql_query("SELECT * FROM people WHERE name='john'") or die('Error: ' . mysql_error());
$array= mysql_fetch_array($sql);
$name = $array['name'];

if (!$_POST['name'] == $name)
	mysql_query("Update people SET name = '$_POST['name']'"); 
?>
```
Take a look at that. I wrote it fast so it could have errors.

Basically it checks if what he posted is what is in the db than if it isn't it updates the data.


----------



## Rockn (Jul 29, 2001)

You are still processing the form regardless of what you are doing and unless there are thousands of concurrent users it won't make much difference anyway.


----------



## cristobal03 (Aug 5, 2005)

Okay, let me clarify something because I think there is some ambiguity regarding my use of the term "posting." By "posting" I mean sending POST data from client to server not posting an update to a record. So far I've found Rockn's comment to be the most pertinent to the exercise.

I appreciate all the suggestions, and after rereading my first post they've all been on-topic because I asked my question poorly. I have no problem making sure that a given record attribute isn't updated if it hasn't been changed. I could do that using the methods suggested in this thread or employing one of my own. I'm trying to prevent sending unnecessary POST data. That's what I meant when I asked if anybody had ever done something like that.

In that regard, Rockn, I tend to disagree with your point. Preprocessing the form client-side requires very few cycles and zero bandwidth, and potentially saves the transaction time of uploading a 20 MB video file, say. I'll be the first to admit my idea of changing unmodified form field values to NULL before submitting the form may not be the best method, but I think it would reduce the size of the data set that gets transmitted to the server. The best solution would be to only send the form fields that were changed, but I'm not sure that's possible.

The more I think about it, the more this seems AJAX-ish. However, I would prefer to use POST rather than XMLHttpRequest and GET, for example, because POST offers me encryption.

Anyway, sorry for the confusion. Any more thoughts/ideas?

chris.


----------



## cristobal03 (Aug 5, 2005)

[bump]

Turns out I don't know enough about XMLHttpRequest. Apparently it is possible to open a POST request per this site's tutorial. So I guess that is the solution I'm looking for. Rather than substituting NULL for the values of unchanged fields and posting the entire form, I will be collecting the values of changed fields and posting those with an XMLHttp mechanism. Something like this:


```
<?php
  $mos = array(
           "1" => "Jan",
           "2" => "Feb",
           "3" => "Mar",
           "4" => "Apr",
           "5" => "May",
           "6" => "June",
           "7" => "July",
           "8" => "Aug",
           "9" => "Sept",
          "10" => "Oct",
          "11" => "Nov",
          "12" => "Dec");

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    Edit Media Record

[B][SIZE=15]Edit Media Record[/SIZE][/B]

          TOC information

            Press date:
            Month:

              <?php

              foreach ($mos as $i => $mo)
              {
                echo "$mo\n ";
              }
              ?>

            Day:

              <?php

              for ($day = 1; $day <= 31; $day++)
              {
                echo "$day\n ";
              }
              ?>

            Year:

              <?php

              $curYear = date("Y");
              $yearStart = $curYear - 10;
              $yearEnd = $curYear + 10;

              for ($year = $yearStart; $year <= $yearEnd; $year++)
              {
                echo "$year\n ";
              }
              ?>

            Press splash:

          Article information

            Title:

            Author:

            Source:

            Body:

            Miscellaneous:

            Copyright:

[URL=http://www.caregivertech.com]home[/URL]
```
That's kind of airware; I haven't written javascript in a while. Mostly it's copied from the tutorial anyway. The handler on the server would have to be intelligent enough to take a variable number of POST parameters and dump them into an UPDATE SQL framework. Off the top of my head I'd probably explode the POST superglobal and make a control block to iterate through the $key => $value pairs.

I'm keen to see more ideas though 

chris.


----------



## MMJ (Oct 15, 2006)

It seems that you have two choices. 
1. Either compare the data with the present data. //More reliable but not much performance gained.
2. Check if the user has changed the data. //less reliable put gains more performance.


----------



## Rockn (Jul 29, 2001)

I am having a hard time following what you are trying to accomplish with your example. If a record hasn't changed it will not effect performance anyway because it will not upload that 20MB file again if it already exists in the current record.


----------



## cristobal03 (Aug 5, 2005)

Here's the general process:


User enters new record data in to client-side form and clicks *Submit*
Data gets sent to server-side handler as POST data
Server-side handler inserts data into new record in the database
User requests database record for editing
Request is transmitted from client to server
Server-side handler extracts data from requested record and pushes data to the client
Client form populates with served data
Client makes changes to any number of fields and clicks *Submit*
Under normal circumstances, the following occurs:

*All* data gets sent to server-side handler as POST data
Server-side handler updates record in database with _all_ new data
What I want to achieve is this:

*Only the changed form field data* gets sent to server-side handler as POST data
Server-side handler updates record in database with _some_ new data
I'm not worried about performance on the server-side. I'm concerned about the client-server transaction. Frankly, that's where the biggest hit occurs anyway because the database resides on the server.

I must be doing a poor job of explaining this. Or maybe I'm making a big deal out of something that isn't typically considered a big deal. I'm just trying to keep from sending data from the client to the server if it wasn't changed on the client.

chris.


----------



## MMJ (Oct 15, 2006)

If you are worried about client side performance than I don't see how doing what you want to achieve will help.


----------



## cristobal03 (Aug 5, 2005)

I'm really failing to see the disconnect here.

If you, as a user, navigate to a site that contains a form with (among other things) an input element of type "file," and you set its value to a 5 MB file, then submit the form, you have to wait until the 5 MB upload to the server before receiving confirmation that it has uploaded. If, however, you've already completed the form once before, and you are only modifying certain fields, rather than send all the data again--including the 5 MB file that hasn't changed--wouldn't you prefer to only send the values that have changed? The upload time is a client-server performance issue that reflects on the client side.

Let's forget the data and performance aspects of the problem. I don't care how the uploaded data gets inserted into the back end. I don't care how the client verifies whether or not a form field value has changed. The fundamental question is, how do I submit some form fields and not others?

chris.


----------



## harmor (Mar 15, 2007)

For the text fields you can do whatever you want but I have a suggestion just for the form elements for uploading images.
You can add a javascript that will show the element when someone ticks the checkbox. Something along the lines of http://www.dynamicdrive.com/dynamicindex16/formdependency.htm


```
Check this box if you're uploading a new image
```


```
if($_POST['new_image'] == 1)
{
  //code here
}
```


----------

