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 4058360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:04:11+00:00 2026-05-20T15:04:11+00:00

I am generating form and handling the submit event in the same file. If

  • 0

I am generating form and handling the submit event in the same file.

If user has not entered the title, I want to display the form again and include an error message (e.g. “You forgot the title.”).

That means that I have to duplicate code twice – once to diplay empty form and second to display form with body and ask user to enter title:

<?php if(strlen(strip_tags($_POST['posttitle'])) == 0):
    // Display the form with question body that user has entered so far and ask user to enter title.
?>
    <label for="title"><b>Title:</label><br/>
    <input type="text" name="posttitle" id="posttitle" />           
<?php endif;?>

<?php elseif ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action']) && $_POST['action'] == 'post') : ?>
<!-- Everything ok - insert post to DB -->
<?php else : 

   // just display form here (again ouch!)
    <label for="title"><b>Title:</label><br/>
    <input type="text" name="posttitle" id="posttitle" />
?>
  • 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-20T15:04:12+00:00Added an answer on May 20, 2026 at 3:04 pm

    I would do it like this:

    If REQUEST_METHOD is POST I will validate the input and collect messages in an array ($errors in my code).

    Then I would just print the form and if there was an error the code will print it.

    <?php
    
    $errors = array();
    
    function print_value_for($attr) {
        if (isset($_POST[$attr]))
            echo $_POST[$attr];
    }
    
    function print_error_for($attr) {
        global $errors;
    
        if (isset($errors[$attr]))
            echo $errors[$attr];
    }
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // do validation here and add messages to $errors
        // like $errors['posttitle'] = "The title you entered is bad bad bad";
    
        if (empty($errors)) {
            // update database and redirect user
        }
    }
    ?>
    
    <!-- display the form and print errors if needed -->
    <form>
        <?php print_error_for('posttitle'); ?>
        <input name="posttitle" type="text" value="<?php print_value_for('posttitle') ?>">
    
        <?php print_error_for('postauthor'); ?> 
        <input name="postauthor" type="text" value="<?php print_value_for('posttitle') ?>">
    
        <?php print_error_for('postbody'); ?>   
        <textarea name="postbody">
            <?php print_value_for('posttitle') ?>
        </textarea>
    
        <input type="submit">
    </form>
    

    PS. Consider using MVC to separate code and templates.

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

Sidebar

Related Questions

Using PHP can you create a pseudo form submit without ever generating a form?
I'm dynamically generating a Windows Forms form by reading an XML file. (Actually I
I'm generating several check boxes that all have the same name, when the form
I'm generating some xml files that needs to conform to an xsd file that
I'm auto-generating a form in my ASP.NET page. This is already tested and working.
I am new to JSP and generating a form with a text area. Is
On Django 1.2.1 I'm using ModelForm and generating a form with radiobuttons: class myModelForm(ModelForm):
A very general question. I am dynamically generating a form that is split into
To avoid reinventing the wheel, I am generating a form using CakePHP's form helper,
In controller I am generating a special form by ID, passed from AJAX. Form

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.