'Why hidden input field values can't be empty
I'm using PHP form to send user registration data and then handle it with PHP form. I need to register a few user types such as "normal users," "admin users," "edit enabled users," etc. Details requests for registration of every kind of user are different. But I hope to use the same formDataManagement.php file. When I click the "Submit" button, an error message shows.
Undefined array Key.
Form data section
<input type="hidden" id="companyname" name="companyname" value="">
<input type="hidden" id="regno" name="regno" value="">
<input type="hidden" id="farmaddress" name="farmaddress" value="">
<input type="hidden" id="role" name="role" value="admin">
<input type="hidden" id="designation" name="designation" value="manager">
formDataManagement.php
//if user signup button
if(isset($_POST['signup'])){
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$conpassword = mysqli_real_escape_string($con, $_POST['conpassword']);
$compName = mysqli_real_escape_string($con, $_POST['companyname']);
$role = mysqli_real_escape_string($con, $_POST['role']);
$regno = mysqli_real_escape_string($con, $_POST['regno']);
$designation = mysqli_real_escape_string($con, $_POST['designation']);
$farm_address = mysqli_real_escape_string($con, $_POST['farmaddress']);
$resi_address = mysqli_real_escape_string($con, $_POST['resaddress']);
}
But after the process, it shows the following error.
Warning: Undefined array key "companyname" in C:\xampp\htdocs\test\admin\register\formDataManagement.php on line 15
Warning: Undefined array key "regno" in C:\xampp\htdocs\test\admin\register\formDataManagement.php on line 17
Warning: Undefined array key "farmaddress" in C:\xampp\htdocs\test\admin\register\formDataManagement.php on line 22
Can't I send empty values as hidden input values? Please advise solving this issue.
Solution 1:[1]
Use
hidden
withouttype="hidden"
like<input hidden id="companyname" name="companyname" value=""/>
. You can set for typetype="text"
.Also try to close elements:
<input hidden type="text" id="companyname" name="companyname" value=""/>
or</input>
.
So here is perfect input element:<input hidden type="text" id="companyname" name="companyname" value=""/>
Solution 2:[2]
I can't determine the exact problem with only your currently given code, but the problem might be caused by any of the followings:
- You didn't use POST method to send the data in the form.
- You put the three input fields outside of the form.
- Your submit button doesn't work properly, especially when you have a pre-submit JS function before submitting the form.
- And more (please include more of your codes)
You definitely can send empty values as hidden input values. Although the content will be empty, it will never give out warnings.
From MDN (description of input="hidden"
):
A control that is not displayed but whose value is submitted to the server.
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 | Valery Dremov |
Solution 2 |