0

hi, i am trying to learn web development using tutorials, i have this contact form my problem is that if i put the php on a separate file n call it it works fine but when i put on same page it gives Notice: Undefined index...
what do i do or wat am i doing wrong. i want the echo to be displayed on same page....

plz help

<!doctype html>
<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Contact Us</title>


        <!--[if lt IE 9]>

            <![endif]-->
<link rel="stylesheet" media="all" href="css/style.css"/>    
<link rel="stylesheet" href="flexslider.css" type="text/css" media="screen" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script src="jquery.flexslider-min.js"></script>  

        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <!-- Adding "maximum-scale=1" fixes the Mobile Safari auto-zoom bug: http://filamentgroup.com/examples/iosScaleBug/ -->
<style type="text/css">
body {
    background:url(images/background.jpg) #19191A no-repeat left top;
    background-size: 100%;
}
</style>
</head>



<?php include('includes/header.php'); ?>

<?php include('includes/nav.php'); ?>

<body>
<div class="wrap_container">
<div class="main">

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Demo'; 
    $to = 'info@domain.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

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

    if ($_POST['submit'] && $human == '4') {                 
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    } 
    } else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
?>

    <form method="post" action="contact.php">

    <label span class="cursive">Name</label>
    <input name="name" placeholder="Type Here">

    <label span class="cursive">Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label span class="cursive">Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>

    <label span class="cursive">*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

    <input id="submit" name="submit" type="submit" value="Submit" span class="cursive">

</form>
    </section>



    <footer class="body">
    </footer>

</body>

</html>

Edited by nitos

3
Contributors
5
Replies
23
Views
3 Years
Discussion Span
Last Post by nitos
1

If the undefined index message is related to the $_POST variables it is because when you normally open a page you execute a GET request, when you submit the form, instead, you perform a POST request and only in this case the $_POST array is populated.

So from:

$name = $_POST['name'];

You will get:

Notice: Undefined index: name ...

In order to stop this you can place an IF statement checking if the $_POST array is true:

if($_POST)
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Demo'; 
    $to = 'info@domain.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    if ($_POST['submit'] && $human == '4') {                 
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    } 
    } else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
}
1

In the event you had more than one form, you can check to see if a particular input elment of type "submit" executed the post. For example..

<?php

if(isset($_POST['submit'])) 
{
    // input with name=submit was clicked.
    // do stuff;
}

?>

<input type="submit" value="Submit" name="submit" />
0

thanx it Works fine but how do i get the echo mesage to appear on the same page instead of in a blank page

0

You will post to the same page as long as the action attribute specifies the same page.

<form method="post" action="contact.php">
1

thanx it worked...thats one step closer to mastering this php thing :-)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.