My contact form script is giving following errors:
[01-Jan-2024 12:50:59 UTC] PHP Warning: Undefined array key "subject2" in /home4/xxx/public_html/xxx.com/xxx.php on line 54
[01-Jan-2024 12:50:59 UTC] PHP Warning: Undefined array key "message2" in /home4/xxx/public_html/xxx.com/xxx.php on line 55
[01-Jan-2024 12:50:59 UTC] PHP Warning: Undefined array key "subject2" in /home4/xxx/public_html/xxx.com/xxx.php on line 60
[01-Jan-2024 12:50:59 UTC] PHP Warning: Undefined array key "message2" in /home4/xxx/public_html/xxx.com/xxx.php on line 61
The $_POST array will contain indexes for all the inputs in the form. The indexes will be names as the inputâs name attribute.
So from your form there will be: âissueâ, âfull_nameâ, âgenderâ, âemailâ, âIPâ and âen-us-kf-kmâ in the $_POST array. Though IP has no value.
PHP Warning: Undefined array key "email" in /home4/xxx.com/xxx.php on line 13
Line 13 is this:
$from = $_POST['email'];
If(isset is like this:
if(isset($_POST['submit'])){
$to = "auto@5starastrology.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$headers2 = "From:\r\n" . $to;
Any help will be appreciated. Please tell me what to add or what to remove.
Thanks.
What is in $_POST when you var_dump() it for debugging purposes? The error message is telling you that there is no element called email in the array, despite the form having a field. Could it be that the form user left it empty somehow? Is there any code prior to line 13 that might be removing that array element? Is line 13 definitely within the if isset() clause?
<?php
if(isset($_POST['submit'])){
$to = "myemail@mydomain.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$headers2 = "From:\r\n" . $to;
}
//Define email for send messages and who the message is sent to
$DomainEmail = "admin@mydomain.com";
$to = "myemail@mydomain.com";
$from = $_POST['email'];
The error comes from trying to use a POST[âkeyâ] before it is set.
Wrap all processing in a reliable unique POST[âkeyâ] such as your submit button name en-us-kf-km. This way the processing code will only happen when this button is pressed. You can further define each variable by first checking that each POST[âkeyâ] is set before using it.
ON A SIDE NOTE:
You should not be attempting to send emails with a POST email address. Use a domain email for sending then use the reply to header to send the user email to you.
From the HTML form you posted, the âsubmitâ button is named âen-us-kf-kmâ so your if condition will never be true because $_POST['submit'] will never be set, there is no such input named âsubmitâ in the form.
As mentioned by @Drummin, check the REQUEST_METHOD to see if a form has been posted, donât rely on isset() of some arbitry input, or even a non-existant input which wonât ever be set.
And at the same time, catch your ACTUAL required values in issets (or better, validation filtersâŚ) so that the site doesnt asplode when a bot sends an empty POST to the form probing it for weaknesses