How to create a single page PHP form submission
<p>I'm trying to create a contact form so people can leave messages on a website. Essentially I want it to be a single page process with a success or error message appearing on the same page. However, I'm a beginner in PHP so I'm not sure where to go from where I'm currently stuck at. </p>

<p>So far I've gotten the form to submit but always returns with missing fields even though no fields are missing. Also the page reloads and returns to the top of the page. Is there a way, after form submission, to have it not return to the top of the page after form submission? </p>

<p>Here's the HTML form, essentially it's a single page website segmented into different slides. Upon form submission I want the page to stay on slide4 where the form and success/error message will be.</p>

<pre><code>    &lt;div id="slide4"&gt;
       &lt;div class="slidetitle"&gt;Contact Me&lt;/div&gt;
           &lt;div class="content"&gt;

           &lt;form action="index.php" method="POST" id="contactform" target="_self"&gt;
                    &lt;input type="hidden" name="postid" value="&lt;?php $postid?&gt;"&gt;
                    &lt;table&gt;
                    &lt;tr&gt;&lt;td&gt;&lt;b&gt;First Name:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="firstName" size="45" value="&lt;?php $firstName?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Last Name:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="lastName" size="45" value="&lt;?php $lastName?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Email:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="email" name="email" size="45" value="&lt;?$email?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Phone:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="tel" name="phone" size="45" value="&lt;?$phone?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Content:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;textarea name="content" form="contactform" rows="10" cols="100"&gt;&lt;?php echo $content; if (empty($content)){ echo "Enter text here...";} ?&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;&lt;td colspan=2 style="text-align: center"&gt;&lt;input type=submit name="submit1" value="Leave me a message"&gt;&lt;/td&gt;&lt;/tr&gt;
                    &lt;/table&gt;
                &lt;/form&gt;

                &lt;div id="contactoutput"&gt;
                    &lt;?php $output ?&gt;
                &lt;/div&gt;
            &lt;/div&gt;
    &lt;/div&gt;
</code></pre>

<p>And here's the Updated PHP for the form after implementing the changes Frank suggested.</p>

<pre><code>&lt;?php
$firstName = "";
$lastName= "";
$email = "";
$phone = "";
$content = "";
$errMsg = "";
?&gt;
&lt;?php $errMsg ?&gt;
&lt;?php
if (isset($_POST['submit1'])) {

// ==========================
//validate user input

// set up the required array
$required = array("firstName","lastName","email","phone","content"); // note that, in this array, the spelling of each item should match the form field names

// set up the expected array
$expected = array("firstName","lastName", "email","phone","content","postid"); // again, the spelling of each item should match the form field names


$missing = array();

foreach ($expected as $field){
    // isset($_POST[$field]):  Note that if there is no user selection for radio buttons, checkboxes, selection list, then $_POST[$field] will not be set

    // Under what conditions the script needs to record a field as missing -- $field is required and one of the following two (1)  $_POST[$field] is not set or (2) $_POST[$field] is empty

    //echo "$field: in_array(): ".in_array($field, $required)." empty(_POST[$field]): ".empty($_POST[$field])."&lt;br&gt;";

    if (in_array($field, $required) &amp;&amp; (!isset($_POST[$field]) || empty($_POST[$field]))) {
        array_push ($missing, $field);

    } else {
        // Passed the required field test, set up a variable to carry the user input.
        // Note the variable set up here uses a variable name as the $field value.  In this example, the first $field in the foreach loop is "title" and a $title variable will be set up with the value of "" or $_POST["title"]
        if (!isset($_POST[$field])) {
            //$_POST[$field] is not set, set the value as ""
            ${$field} = "";
        } else {

            ${$field} = $_POST[$field];

        }

    }

}

//print_r ($missing); // for debugging purpose

/* add more data validation here */
// ex. $price should be a number

/* proceed only if there is no required fields missing and all other data validation rules are satisfied */
$stmt = $conn-&gt;stmt_init();

$sql = "Insert Into msg_table16 (firstName, lastName, email, content, phone, postid) values (?, ?, ?, ?, ?, ?)";

if($stmt-&gt;prepare($sql)){

    // Note: user input could be an array, the code to deal with array values should be added before the bind_param statement.

    $stmt-&gt;bind_param('ssssii',$firstName, $lastName,$email,$content,$phone,$postid);
    $stmt_prepared = 1; // set up a variable to signal that the query statement is successfully prepared.
}


if ($stmt_prepared == 1){
    if ($stmt-&gt;execute()){
        $output = "&lt;p&gt;The following information has been saved in the database:&lt;br&gt;&lt;br&gt;";
        foreach($_POST as $key=&gt;$value){
            $output .= "&lt;b&gt;$key&lt;/b&gt;: $value &lt;br&gt;"; //$key (form field names) may not be very indicative (ex. lname for the last name field), you can set up the form field names in a way that can be easily processed to produce more understandable message. (ex. use field names like "Last_Name", then before printing the field name, replace the underscore with a space.  See php solution 4.4)
        }
    } else {
        //$stmt-&gt;execute() failed.
        $output = "&lt;p&gt;Database operation failed.  Please contact the webmaster.";
    }
} else {
    // statement is not successfully prepared (issues with the query).
    $output = "&lt;p&gt;Database query failed.  Please contact the webmaster.";
}

} else {
// $missing is not empty
$output = "&lt;p&gt;The following required fields are missing in your form submission.  Please check your form again and fill them out.  &lt;br&gt;Thank you.&lt;br&gt;\n&lt;ul&gt;";
foreach($missing as $m){
    $output .= "&lt;li&gt;$m";
}
$output .= "&lt;/ul&gt;";
}
?&gt;
</code></pre>

<p>So in summary, why is the form returning that fields are missing when I would fill them all in so there shouldn't be any missing. AND How do I have the form submit and not reload and return to the top of the page?
I apologize if there is any required information that I did not post in advance, if you guys need anymore code snippets please let me know. Thank you guys!</p>