I have another question regarding Form validation and submission.
I am not able to figure out if there is a better way to do this
I have the following pages to perform an Edit - usersView.php and usersEdit.php
usersView.php - This page lists all the current registered users. It has edit/delete buttons next to each row. Eg
No. | Name |------ Email---|------|---------|
1----|User1 |
abc@abc.com| Edit | Delete |
Each row has a hidden input field that holds the id for each record
When I click Edit I am taken to usersEdit.php
This form populates the fields based on the id passed from usersView.php
Code: Select all
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$id = $_POST['id'];
$firstname = value pulled from database based on id;
$firstnameedited = $_POST['firstnameEditBox'];
// When I submit the form below I want to validate the field
if($firstnameedited != empty){
$firstnameedited is valid;
updateFirstName($firstnameedited);
header('Location: usersView.php');
exit();
}
else{
Show errors on the form;
}
<form method="POST" action="usersEdit.php">
<input type="text" name="firstnameEditBox" value="<?php echo $firstname; ?>"/>
<button type="Submit" name="userEditBttn">Submit</button>
</form>
My problem is the POST method from usersView.php and POST method from usersEdit.php
When I go to usersEdit.php I get an error saying undefined index firstnameEditBox. I know why the error is but I don't know how to solve it without creating another page called usersUpdate.php and changing the form action on usersEdit.php to usersUpdate.php
Is there a way without using isset($_POST['userEditBttn'])?
I read at many places that even though isset($_POST) is widely used, it is not best practice because users may use enter key to submit
Please advise.
Thanks.