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

  • Home
  • SEARCH
  • 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 7958889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:29:21+00:00 2026-06-04T04:29:21+00:00

I have this code where i validate each field to be not empty and

  • 0

I have this code where i validate each field to be not empty and the problem is that when every field is filled I have to press submit twice in order for the form to be submitted.

<table>
    <form method ="post" <?php 
    if(!empty($_POST['Firstname']) &&
       !empty($_POST['Lastname']) &&
       !empty($_POST['Email']) &&
       !empty($_POST['Comments'])){
       echo 'action="messagesent.php"';}
     else if(isset($_POST['Firstname']) ||
       isset($_POST['Lastname']) ||
       isset($_POST['Email']) ||
       isset($_POST['Comments'])) {
       echo 'action="contact.php"';}?> >
        <tr>
        <td><label>Firstname:</td><td><input name ="Firstname" type ="text" size ="30" <?php if(!empty($_POST['Firstname'])){$Firstname=$_POST['Firstname'];echo "value=$Firstname";} ?> /></td><td><?php if(empty($_POST['Firstname'])){echo "<font color='blue'>*</font>";}?></td>
        </tr>

        <tr>
        <td><label>Lastname:</td><td><input name="Lastname" type ="text" size ="30" <?php if(!empty($_POST['Lastname'])){$Lastname = $_POST['Lastname'];echo "value=$Lastname";}?> /></td><td><?php if(empty($_POST['Lastname'])){echo "<font color='blue'>*</font>";}?></td>
        </tr>

        <tr>
        <td><label>E-mail:</td><td><input name="Email" type ="text" size ="30" <?php if(!empty($_POST['Email'])){$Email = $_POST['Email'];echo "value=$Email";}?> /></td><td><?php if(empty($_POST['Email'])){echo "<font color='blue'>*</font>";}?> </td>
        </tr>

        <tr>
        <th colspan="2"><label>Your Message</th>
        </tr>
        <tr>
        <td colspan="2"><textarea name= "Comments" rows="10" cols="34"><?php $Comments=$_POST['Comments'];if(!empty($_POST['Comments'])){echo "$Comments";} ?> </textarea></td><td><?php if(empty($_POST['Comments'])){echo "<font color='blue'>*</font>";}?> </td>
        </tr>
        <tr>
        <td><input type="submit" value = "SUBMIT"/> </td>
        </tr>

    </form>
</table>
  • 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-06-04T04:29:22+00:00Added an answer on June 4, 2026 at 4:29 am

    It looks like you’re expecting PHP to check whether the fields on the page are filled in. That’s not how PHP works. It evaluates once, when the page is first rendered, and then never does anything again until the page is loaded again. PHP’s $_POST array doesn’t refer to the input fields on the page; it refers to the values that the page received from the form when it was loaded. So what’s happening is;

    1. The page loads for the first time, and $_POST is empty because no form was submitted.
    2. PHP sees this and sets ACTION to send you back to the same page.
    3. You fill in the form and hit submit.
    4. The page reloads, but this time $_POST has values in it because you just submitted the form.
    5. PHP sees the values in $_POST and sets ACTION to send you to the next page.
    6. You fill in the form again, hit submit, and get sent to the next page.

    If you want to check the form to see if the fields are filled in before the form is submitted, you can’t use PHP. You’ll need to use some javascript. Here’s a quick tutorial, where the first example addresses what you’re looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview that I'm trying to validate from code behind. In this
I have a problem with content_tag into an helper, this piece of code def
I have some terribly written ASP.NET code that is just not working right (go
OK, i have a couple of inputs. I have this code to validate them.
I currently have an ASP.Net Web Form that looks like this: Each section of
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
have this code: template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class
i have this code local strs = my dog print (string.gsub( strs , ,%20))
I have this code : h1, h2, h3, h4{ color: red; } .message_rouge h1,
I have this code: std::string name = kingfisher; char node_name[name.size()+1]; strcpy(node_name,name.c_str()); node_name[name.size()] = '\0';

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.