Jump to content

Wrong information in Form Fields


mdmartiny

Recommended Posts

I am writing an email form for my site with some simple validation. I have the error messages working just fine. They do what they are supposed to do.

 

However, when the form is brought back up to have the errors fixed. It puts a "1" in the field box. I looked at the code and I do not see where the "1" is coming from.

 

I am also in the process of trying to figure out how to send email through my mail account using smtp. I want to see if it is going to send me the information that I ask for or the "1". Currently all of my work is being done on my localhost server

 

Here is the code that I am using

 

<?php

$email_form = "<form action='' target='' id='contactform' method='post'>\n";
$email_form .="<p class='form_header'>Contact us</p>\n";
$email_form .= "<fieldset>\n";
$email_form .= "<P>\n";
$email_form .= "<label for='name'><em>*</em> Your Name:</label>\n";
$email_form .= "<input type='text' name='name' id='name' value=".(isset($_POST['name']))." />\n";
$email_form .= "</p>\n";
$email_form .= "<p>\n";
$email_form .= "<label for='email'><em>*</em> E-mail:</label>\n";
$email_form .= "<input type='text'  name='email' id='email' value=".(isset($_POST['email']))." />\n";
$email_form .= "</p>\n";
$email_form .= "<p>\n";
$email_form .= "<label for='subject'>Subject:</label>\n";
$email_form .= "<input type='text' name='subject' id='subject' value=".(isset($_POST['subject']))." />\n";
$email_form .= "</p>\n";
$email_form .= "<p>\n";
$email_form .= "<label for='message'><em>*</em> Message:</label>\n";
$email_form .= "<textarea name='message' id='message'>".(isset($_POST['message']))."</textarea>\n";
$email_form .= "</p>\n";
$email_form .= "<input type='submit' id='submit' value='Submit!' name='submitted' /><br />\n";
$email_form .= "<p class='required'>Fields marked with an asterik(*) are required</p>\n";
$email_form .= "</fieldset>\n";

if (!isset($_POST['submitted'])) {
    echo "$email_form";
}
else {
    $name = (isset($_POST['name']));
    $email = (isset($_POST['email']));
    $to = "[email protected]";
    $subject = (isset($_POST['subject']));
    $body = (isset($_POST['message']));

    if ($subject == "") {
        $subject = "Email from website";
    }
    else {
        $subject == $subject;
    }

    if ($_POST['name'] != "") {
        $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
        if ($_POST['name'] == "") {
            $errors .= 'Please enter a valid name.<br/><br/>';
        }
    }
    else {
        $errors .= 'Please enter your name.<br/>';
    }

    if ($_POST['email'] != "") {
        $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors .= "$email is <strong>NOT</strong> a valid email address.<br/>";
        }
    }
    else {
        $errors .= 'Please enter your email address.<br/>';
    }

    if ($_POST['message'] != "") {
        $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
        if ($_POST['message'] == "") {
            $errors .= 'Please enter a message to send.<br/>';
        }
    }
    else {
        $errors .= 'Please enter a message to send.<br/>';
    }

    if (empty($errors)) {

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= "From: " . $email . "\r\n";

        $success = mail($to, $subject, $body, $headers);
    }

    if ($success) {

        echo "<p>The following email has been sent</p>\n";
        echo "<p>Name: $name</p>\n";
        echo "<p>E-mail: $email</p>";
        echo "<p>Subject: $subject</p>\n";
        echo "<p><em>*</em> Message: $body</p>\n";
        echo "<p>While you are waiting for a response from one of our staff members. Feel free to look at some of the following sections</p>\n";
        echo "<a href='../waiting.php'>In The Mail</a>";
        echo "Thank you for visiting Michael48060.</p>\n";
    }

    else
        echo "$email_form";
}

if (!empty($errors)) {
    echo "<div class='error_div'>
	<span class='errors'>" . $errors . "</span>
	</div>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/248265-wrong-information-in-form-fields/
Share on other sites

isset() returns a boolean value. When you echo isset($_POST whatever, if it evaluates to TRUE, it will echo the 1. The value of the value= attributes in the form fields should also be quoted; currently they are not.

How do I make it so that I do not get the undefined index messages for each of the fields. I tried making it an if statement but that did not work.  I do not want there to be anything in the field unless it is set.

 

 

If it were me, I'd use ternary syntax for brevity, and assign either the value of the element fro the $_POST array or an empty string to a variable, then use that variable as the value= attribute. Make sure you've trim()med the values in the $_POST array so that just whitespace isn't considered a valid entry.

 

$_POST = array_map('trim', $_POST) // if the $_POST array is not multidimensional, trim all the elements.

$name = !empty($_POST['name']) ? $_POST['name'] : '';
$email_form .= "<input type='text' name='name' id='name' value='$name' />\n";

 

EDIT: removed redundant trim() function . . .

 

2nd EDIT: Pretty much what Buddski said, just written a little differently.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.