web form is not self-refering and validation errors are not showing.

0 Daniel Stockham · August 30, 2014
I'm at my wits end with this php script. Ever since I put in the if statement with the isset() function, the form refuses to save the input data

Post a Reply


Oldest  Newest  Rating
0 Daniel Stockham · August 30, 2014
Here is the code:

    if isset($_POST['submit'])) {
    $from = 'elmer@makemeelvis.com';
    $subject = $_POST['subject'];
    $text = $_POST['elvismail'];
    $output_form = false;
    if (empty($subject) && empty($text)) {
        //both subject and text empty
        echo 'You forgot both the subject and email text.<br />';
        $output_form = true;
    if (!empty($subject) && empty($text)) {
        echo 'You forgot the email text you dummy!<br/>';
        $output_form = true;
    }    //The text is forgotten
    if (empty($subject) && (!empty($text))) {
        echo 'You forgot the subject!<br/>';
        $output_form = true;
    }    //The subject is forgotten
    if (!empty($subject) && (!empty($text))) {
        //Everything checks out and send email
    $dbc = mysqli_connect('localhost', 'root', 'logman', 'elvis_store')
        or die('Error connecting to MySQL server.');
    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query)
        or die('Error querying database.');
    while($row = mysqli_fetch_array($result)) {
        $first_name = $row['first_name'];
        $last_name = $row['last_name'];
        $msg = "Dear $first_name $last_name, \n $text";
        $to = $row['email'];
        mail($to, $subject, $msg, 'From:'. $from);
        echo 'Email sent to:'.$to. '<br />';
    else {
        $output_form = true;
        if ($output_form) {
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Make Me Elvis - Send Email</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
  <img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis" />
  <p><strong>Private:</strong> For Elmer's use ONLY<br />
  Write and send an email to mailing list members.</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="subject">Subject of email:</label><br />
    <input id="subject" name="subject" type="text" size="30" /><br />
    <label for="elvismail">Body of email:</label><br />
    <textarea id="elvismail" name="elvismail" rows="8" cols="40">
    </textarea><br />
    <input type="submit" name="Submit" value="submit" />

+3 Franz Schmidt · August 30, 2014
first you forgot a bracket of the if statement and you used the wrong name of the submitbutton

Change the first line to:
if (isset($_POST['Submit'])){
0 Daniel Stockham · August 31, 2014
Scratch that, it worked. However, for some reason the text box likes to input data in the form. For instances, when I put the data in the email body, and hit submit the data gets indented. I don't want it to do that.

Would that be more html related or in the php code?
+1 Franz Schmidt · August 31, 2014
I can't follow you, what do you mean with the text box likes to input data in the form ?
0 Daniel Stockham · September 2, 2014
It places a indent in the email text field, so there is a value the moment the script starts up which shouldn't be the case.

Also, what else is strange is when I first open the page for the first time, I get an error saying that the subject variable is undefined, which doesn't make any sense. It should be null.
+1 Franz Schmidt · September 2, 2014
Ok the first problem I solved:
I also didn't know: http://stackoverflow.com/questions/2202999/why-is-textarea-filled-with-mysterious-white-spaces
It's because of the break inside the script:
just change this:
<textarea id="elvismail" name="elvismail" rows="8" cols="40">
</textarea><br />

to this:
<textarea id="elvismail" name="elvismail" rows="8" cols="40"></textarea>
<br />

and it won't show this mysterious whitespace

And I don't get any errors when I open the site :( maybe it has something to do with error report settings in your php.ini 
0 Shamal Sandeep · September 5, 2014
if isset($_POST['submit'])) {
if (isset($_POST['submit'])) {
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.