-3

I've made an HTML form for to email. I've used

 if(!array_key_exists($_POST)) {
    $data = file_get_contents('php://input');
 }

to determine that the $_POST array is empty and none of the values are being passed to the php input using the code below. What is going on here?

Here's my code:

HTML:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input type="text" class="form-control" required="required" placeholder="Name" id="name" name="name">
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input type="text" class="form-control" required="required" placeholder="Email address" id="email" name="email">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
                                </div>
                                <div class="form-group">
                                    <input type="submit" class="btn btn-danger btn-lg" value="Send Message">
                                </div>
                            </div>
                        </div>
                    </form>

PHP:

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Email sent!'
);

$subject = "Contact from Website";

$name =  $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(!array_key_exists($_POST)) {
    $name= "empty";
    $email = "empty";
    $message = "empty";
    $data = file_get_contents('php://input');
}
$email_to = '[email protected]';

$body = "A message has been submitted from your website: \n" .  "Name: ". $name . "\nEmail: " . $email . "\nMessage: " . $message . "\nData: " . $data;

mail($email_to, $subject, $body, 'From: ' . $email . '\n\r'); 

echo json_encode($status);
die;
?>
3
  • 1
    if(!array_key_exists($_POST)): Did you read the docs on that function? How did you even end up using it? Oct 1, 2014 at 23:49
  • I read some other trouble shooting $_POST threads on here that said isset shouldn't used for arrays but for single values. I guess that's wrong?
    – Curtis
    Oct 2, 2014 at 0:13
  • It depends on what you're checking. See my answer. Oct 2, 2014 at 23:29

3 Answers 3

1

I read some other trouble shooting $_POST threads on here that said isset shouldn't used for arrays but for single values. I guess that's wrong?

if(!isset($_POST)) will check if the variable $_POST has even been set to anything at all.

if(!array_key_exists('x', $_POST)) will check if the key x exists in the array $_POST. Which is basically the same as if(!isset($_POST['x'])) except in the case where the key x does exist but has been explicitly given a value of null. From the example in the docs for array_key_exists :

<?php
$search_array = array('first' => null, 'second' => 4);

// returns false
isset($search_array['first']);

// returns true
array_key_exists('first', $search_array);
?>

Usually when checking post parameters your interest is not only in the existence of the key but also that it is not null. So isset makes more sense, even when looking for a particular key. But certainly when just checking the $_POST object you should use isset.

0

You are grievously misusing array_key_exists. Also assuming you don't want to send an email when the form isn't posted, just check at the top and bail out if it's not set: if (!isset($_POST)) exit;

0

Try removing this line:

header('Content-type: application/json');

Are you outputting a header for Ajax here? If so it sounds like your issue is in the JavaScript.

And for heaven's sake, please do a sanity check on post:

if (!empty($_POST)) { // insert process code here }

Your action should probably be this if it's not ajax:

action="/sendemail.php"

The code you have is relative, sendemail.php needs to be in the same directory as your html form or else it won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.