Note: Undefined index: submit in (Path)

Hi StackOverflow Community, I have a problem with my contact form.

I used this PHP Script:

<?php
    if ($_POST["submit"]) {
        $name = $_POST['name'];
        $email = $_POST['phone'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Kontaktformular'; 
        $to = 'email@mail.com'; 
        $subject = 'Buchungsanfrage - $name';

        $body ="From: $name\n: $phone\n E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Bitte geben Sie den Namen ein.';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Bitte geben Sie eine gültige E-Mail Adresse ein.';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Bitte hinterlassen Sie mir eine Nachricht.';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Ihre Anti-Span Antwort war falsch.';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Danke! Ich werde mich schnellstmöglich bei Ihnen melden.</div>';
    } else {
        $result='<div class="alert alert-danger">Entschuldigung, beim versenden Ihrer Nachricht ist etwas schief gelaufen. Bitte versuchen Sie es nochmal.</div>';
    }
}
    }
?>

      

And this HTML for my form:

<div class="block block-primary-head no-pad">
            <h3><i class="fa fa-pencil"></i> Buchungsanfrage</h3>
            <div class="block-content">
                <form role="form" method="post" action="fotografie.php">
            <div class="form-group">
                <label>Name*</label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Vor- und Nachname" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                <?php echo "<p class='text-danger'>$errName</p>";?>
            </div>
            <div class="form-group">
                <label>Telefonnummer</label>
                <input type="text" class="form-control" id="phone" name="phone" placeholder="Ihre Telefonnummer" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
            </div>
            <div class="form-group">
                <label>E-Mail Adresse</label>
                <input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                <?php echo "<p class='text-danger'>$errEmail</p>";?>
            </div>
            <div class="form-group">
                <label>Nachricht</label>
                <textarea class="form-control" id="message" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                <?php echo "<p class='text-danger'>$errMessage</p>";?>
            </div>
            <div class="form-group">
                <label>2 + 3 = ?</label>
                <input type="text" class="form-control" id="human" name="human" placeholder="Ihre Antwort">
                <?php echo "<p class='text-danger'>$errHuman</p>";?>
            </div>
        </div>
        <div class="form-group">
            <?php echo $result; ?>
        </div>
        <button type="submit" id="submit" name="submit" value="send" class="btn btn-primary">Absenden</button>
    </form>
</div>

      

But when I go to my side I got the following error:

Note: Undefined index: submit in .. \ htdocs \ projects \ bootstrap-themes \ 001 \ contact \ form-fotografie.php on line 2

Line 2 corresponds to the following code:

if ($ _POST ["submit"]) {

What could be the problem?

Thank you for your help: -)

+3


source to share


1 answer


It looks like you are trying to check if the form has been submitted. For this you need to use a function isset

as shown below

Line 2:

if ($_POST["submit"]) {

Should read:

if (isset($_POST["submit"])) {

EDIT:



You should always check if a variable is set before using it, so to fix your html you need to change the following:

<div class="form-group">
            <label>E-Mail Adresse</label>
            <input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
        </div>

      

To:

<div class="form-group">
            <label>E-Mail Adresse</label>
            <input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php if(isset($_POST['email'])) { echo htmlspecialchars($_POST['email']); } ?>">
            <?php if(isset($errEmail) && ($errEmail) != "") { echo "<p class='text-danger'>$errEmail</p>"; } ?>
        </div>

      

You need to make this change for all form groups with corresponding variable names, let me know how you get to

+3


source







All Articles