Undefined array key

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

Lines 54 and 55 are these:

		$text_content = $_POST['subject2'] . "\n";
		$text_content = $_POST['message2'] . "\n";

Lines 60 and 61 are last 2 font lines:

		$html_content = '<html>
				<font size="4" color="blue"><subject>'.$subject2.'</subject><br>*****</font><p></p>
				<font size="2"><span>' . $_POST['subject2'] . '</span></font><p></p>
				<font size="2"><span>' . $_POST['message2'] . '</span></font><p></p>

All the help with examples would be appreciated. Please understand that I do not know PHP at all>
Thanks and regards,

Can you show us the HTML form that submits to this?

Unrelated problems:-

Here the variable $text_content is overwritten by the second line, making the first line redundant.

When outputting user input to HTML is is safer to escape that input with something like htmlspecialchars(), Eg:-

'<font size="4" color="blue"><subject>'.htmlspecialchars($subject2).'</subject>'
1 Like

This is the html form:

<form id="kontaktformular" class="kontaktformular" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" accept-charset="utf-8" enctype="multipart/form-data" >
				<div class="row">
					<div class="col-sm-8">
						<label class="control-label"><b><i>Subject</i> *</b></label>
						<input type="text" name="issue" class="field" style="height:100%; width:100%;" placeholder="Subject, please *"  required >
					    <label class="control-label"><b><i>Full Given Birth Name</i> *</b></label>
						<input type="text" name="full_name" class="field" style="height:100%; width:100%;" placeholder="Your Full Given Birth Name *"  required >
						<label class="control-label"><b><i>Your Gender/Sex</i> *</b></label><br>
						<input type="radio" name="gender" value="Female">Female<br>
                        <input type="radio" name="gender" value="Male">Male<br>
                        <input type="radio" name="gender" value="Other">Other</select><br>
						<label class="control-label"><b><i>Your Valid Active Email</i> *</b></label>
						<input type="text" name="email" class="field" style="height:100%; width:100%;" placeholder="Please enter Valid Active Email *"  required >
						<label class="control-label"></label>
						<input type="text" name="IP" class="field" style="height:100%; width:100%;" hidden>
	<div class="row" id="send">
		<div class="col-sm-8">
			<button type="submit" class="senden" name="en-us-kf-km" style="border: 0; background-color: transparent;" value="Send" /><img src="img/submit.gif" alt="SomeAlternateText"></button>

Well, the form doesn’t contain any inputs named subject2 or message2, so I would not expect those array keys to be defined.

Even if we have to define how will we do?

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.


I am still not able to remove this error:

 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:

    $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.

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?

These are the first 13 lines:


    $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'];

So… why are we repeating line 5 on line 13? Why did we close the if?

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.

if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['en-us-kf-km'])):

	//All processing goes within this outer POST condition
	$issue = (isset($_POST['issue']) ? $_POST['issue'] : '');
	$full_name = (isset($_POST['full_name']) ? $_POST['full_name'] : '');
	$gender = (isset($_POST['gender']) ? $_POST['gender'] : '');
	$email = (isset($_POST['email']) ? $_POST['email'] : '');


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.

	$headers .= "Reply-To: $email\r\n";

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.

Yes, and never name your submit button ‘submit’ as that is unreliable. Use a unique name.

Thanks a lot.
I appreciate your help.
Best regards,

OK. Thank you so much.
Best regards,

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 :wink:


Just by adding @ in 13th line issue is resolved.
Now line 13 is:
@$from = $_POST[‘email’];

Thanks everyone for your kind help.

For clarity, all that is doing is suppressing the error message. It’s not fixing anything.


I debugged the script throgh ms studio using php and it gave no error in script. I added that @ symbol after it.