Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:11:40+00:00 2026-05-26T18:11:40+00:00

I am a PHP newb. I have the following code for a web form.

  • 0

I am a PHP newb. I have the following code for a web form. It works fine as is, but I would like to do the following:

  1. Return the errors as an array (?) so I can display errors as individual lines under each input.

and

  1. Disallow the form from being able to be submitted twice.

Any help would be greatly appreciated.

<form id="form1"  method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<fieldset>
<legend>Contact Me</legend> 
<?php 


    if (isset($_POST['Submit'])) {  

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

           if ($_POST['lastname'] != "") {  
            $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);  
            if ($_POST['lastname'] == "") {  
                $errors .= 'Please enter a valid last name.<br/><br/>';  
            }  
        } else {  
            $errors .= 'Please enter your last 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/><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 (!$errors) {  
            $mail_to = '***@****.com';  
            $subject = 'New Mail from Web Site';  
            $message  = 'From: ' . $_POST['firstname'] . " " .  $_POST['lastname'] . "\n";  
            $message .= 'Email: ' . $_POST['email'] . "\n";                   
            $message .= "Message:\n" . $_POST['message'] . "\n\n";  
            mail($mail_to, $subject, $message);  

            echo "<p>Thank you for your email!<br/><br/></p>";  
        } else {  
            echo '<div style="color: #00CC00">' . $errors . '<br/></div>';  
        }  
    }

?>  

<label>First Name:</label>   
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" size="50" />

<label>Last Name:</label>  
<input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" size="50" />    
<label>Email Address:</label>  
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/>    
<label>Message:</label>  
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>  
<br/>  
<input type="submit" class="moveright" name="Submit" value="Submit" /> 
</fieldset> 
</form> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:11:41+00:00Added an answer on May 26, 2026 at 6:11 pm

    You can use an array for the errors instead of concatenating them into one string. Then you can check for each error at the specified form input.

    Sample error check

    // instead of: $errors .= 'Please enter a message to send.<br/>';
    if ($_POST['message'] == "")
        $errors['message'] = 'Please enter a message to send.<br/>';
    

    Sample error display

    <label>Message:</label>
    <?php if ($errors['message'] != "") echo $errors['message']; ?>
    <textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (stripped down) code: <?PHP class A { function Show(){ echo
I would like to make a php file i have a part of a
PHP Array logic re-factor I'm trying to re-factor my code... This is PHP ...
I'm a little bit of a newb when it comes to PHP, but know
i have the oop php code: class a { // with properties and functions
I'm new to PHP and web scripting in general so this a newb question.
sorry for this newb question, i just started programming with php I'm following Yahoo
Newb here trying to fix my php code. Getting an error at line 89.
PHP's explode function returns an array of strings split on some provided substring. It
I'm keen to get started experimenting with gzip, but like i used to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.