'2 Forms, Same Method but different task

How can I make 2 forms which has the same method? One is responsible to open editaddresss.php with hidden addresse ID, and the second form (editaddress.php)should trigger controller and repository to save the data?

The problem is, as soon as I click "Edit Address" button on profile.php, triggers editAddress() method in controller. But I want to only happen when is clicked on editaddress.php

Profile.php

<form action="editaddress.php" method="POST" id="form1">
   <tr>
      <input type="hidden" name="addressid" value="<?php echo $perssonalAddress->getAddressid(); ?>">
               <td><?php echo $perssonalAddress->getStreet(); ?></td>
               <td><?php echo $perssonalAddress->getZip(); ?></td>
               <td><?php echo $perssonalAddress->getCity(); ?></td>
               <td><?php echo $perssonalAddress->getCountry(); ?></td>
               <td><?php echo $perssonalAddress->getType(); ?></td>
               <td><input type="submit" id="removeitem" value="Edit Address" form="form1"></td>
 </form>

Editaddress.php

<form action="editaddress" method="POST" name="editadresse" id="form2">
                    <input type="hidden" name="update">
                    <input type="hidden" value="<?php echo $getPersonalAddress->getAddressid(); ?>" name="addressid">
                    <label for="street">Street</label>
                    <input type="text" value="<?php echo $getPersonalAddress->getStreet() ?>" id="street" name="street">
                    <label for="zip">ZIP</label>
                    <input type="text" value="<?php echo $getPersonalAddress->getZip() ?>" id="zip" name="zip">
                    <label for="city">City</label>
                    <input type="text" value="<?php echo $getPersonalAddress->getCity() ?>" id="city" name="city">
                    <label for="country">Country</label>
                    <input type="text" value="<?php echo $getPersonalAddress->getCountry() ?>" id="country" name="country">
                    <input type="submit" class="login" id="login" value="Update Address" name="editadresse" form="form2">
                </form>

Controller

    /**
     * Get Customers specific Address
     */
    public function editAddress(): void
    {
        //Error and Success Messages
        $error = false;
        $success = false;

        //Error if there is no such an address
        $message = false;


        //Get the specific customer address
        $getPersonalAddress = $this->profileService->getAddressById();

        if ($getPersonalAddress === false) {
            $message = $this->profileService->getMessages();
        }

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && $this->addressForm->isValid($_POST)) {
            $this->profileService->updateCustomerAddress();
            $success = $this->profileService->getMessages();
        }else {
            $error = $this->addressForm->getFormErrorMessage();
        }

        //Render to View
        $this->render('editaddress', ['getPersonalAddress' => $getPersonalAddress, 'message' => $message, 'error' => $error, 'success' => $success]);
    }


Solution 1:[1]

I think you're approaching this incorrectly. You are trying to get one end-point (editaddress.php) to do two different things.

instead, you should have one end-point for generating a form with the details filled in.

and a different end-point for recording data saved from that form.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kae Verens