Form update and disabled fields. How can I make this work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Form update and disabled fields. How can I make this work?

Post by hybris »

Hi again,

so I have this form with like 50 fields to fill out. Upon submit it jumps to write_to_DB.php where i read all variables from post and write them to the database.

This part is working fine. The problem is when I want to update a form then I read from database and fill out the form from the database and disables all textfields. After each field there is a checkbox called update. When you click that checkbox it enable the line for input (or if you uncheck the field is disabled again).

This also works, but as You probably guessed by now the values in the disabled fields are not submitted to the write_to_DB.php so when i try to update it only writes the new values (from the checked (active) fields) and write over all unchecked (disabled fields) in the database with NULL.

I guess I cold do some script that enables all fields when i submit the form but I dont think its a nice solution.

I can also do a check for each variable if its set and call DB to update it but it will be an ugly code to do so for all variables...

I know there must be a way to only update the set parameters but I cannot figure out how.

Below is the code for update

Code: Select all

 if ($stmt = $mysqli->prepare("UPDATE Deviation_Report SET ArticleNumber = ?,
        Wholesale = ?,
        Customer = ?,
        CustomerOption = ?,
        CustomerFirstName = ?,
        CustomerFamilyName = ?,
        CustomerTelephoneNumber = ?,
        CustomerEmail = ?,
        ProductionDate = ?,
        BestBeforeDate = ?,
        BatchNumber = ?,
        ProductionTime = ?,
        PlantId = ?,
        ClaimedAmount = ?,
        ClaimedAmountUnit = ?,
        SeverityGrade = ?,
        DevianceDescription = ?,
        SaidToCustomer = ?,
        CreditOption = ?,
        CreditAmount = ?,
        CreditAmountUnit = ?,
        UserIDWhoCredit = ?,
        CreditInformation = ?,
        RetrievalOption = ?,
        UserIDWhoRetrieve = ?,
        PickUpAdress = ?,
        DeliveryAdress = ?,
        PickupInformation = ?,
        WarehouseCode = ?
        WHERE ID = ?")) { 
                        $stmt->bind_param('sssisssssssssisissiisisiisssss', $ArticleNumber, $Wholesale, $Customer, $CustomerData_Opt, $CustomerContactFirstName, $CustomerContactLastName, $CustomerContactTelephone, $CustomerContactEmail, $ProductionDate, $BestBeforeDate, $BatchNumber, $ProductionTime, $PlantId, $ClaimedAmount, $ClaimedAmountUnit, $SeverityGrade, $DevianceDescription, $SaidToCustomer, $Credit_Opt, $CreditAmount, $CreditAmountUnit, $WhoWillDoTheCredit, $CreditDescription, $GoodsPickup_Opt, $WhoWillDoThePickup, $PickupAdress, $GoodsDeliveryAdress, $GoodsPickupInformation, $GoodsPickupWarehouseStatus, $DevID);
                        $stmt->execute(); 
            }
Also I really want the fields to be disabled in the form.. I do not want to change to read only....(As I see ppl suggest in similar topics)

I guess I have to do some array where only the set variables go but I don't know how to do the DB update code from such an array.

Thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form update and disabled fields. How can I make this wor

Post by Celauran »

You're dealing with a $_POST array at some point. Build your statement from the keys provided.

Code: Select all

array_keys($_POST)
should get you started. Strip out id since you'll be using that elsewhere.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Form update and disabled fields. How can I make this wor

Post by hybris »

Hi, Thank You.

One more question..

Since every line has a checkbox (to see if the line should be updated or not) I need to do a lot of stripping.

Is it fine to unset directly from $_POST or should I copy the $_POST array first (Edit:And work with the copy)? (Does it matter or what is concidered best practice)?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form update and disabled fields. How can I make this wor

Post by Celauran »

Working directly with $_POST is never a good idea.
Post Reply