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]);
}
My Answer:
To achieve this, you can differentiate between the two forms by adding a hidden input field in each form that specifies the action to be taken.
In your Profile.php form, add a hidden input field with a specific value that indicates it is for opening editaddress.php:
html
In your Editaddress.php form, add a hidden input field with a different value that indicates it is for updating the address:
html
Then, in your controller, you can check the value of the "action" field to determine which action to take:
php
public function editAddress(): void
{
// Check the value of the "action" field
if ($_POST['action'] === 'openEditAddress') {
// Code to open editaddress.php with hidden address ID
// Redirect or render the editaddress.php page
} elseif ($_POST['action'] === 'updateAddress') {
// Code to update the address
// Call the updateCustomerAddress() method
}
}
By adding this additional hidden input field and checking its value in the controller, you can differentiate between the two forms and perform the appropriate action based on the form that was submitted.
Rate this post
5 of 5 based on 5738 votesComments