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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:47:05+00:00 2026-06-06T02:47:05+00:00

I have simple php validation form that is halfway working. If you leave the

  • 0

I have simple php validation form that is halfway working. If you leave the field empty and click submit, it does the display the correct error message. The issue is with the regular expressions. For example if the currency field is not valid it doesnt display the correct error message. As a matter when clicking submit it reloads the page and erases all the values in the textboxes. How come it will not display the correct error message?

HTML Form

<form action="" method="post" id="form">

                <label for="tile">Title: <em>*</em></label>
                <input type="text" name="title" id="title" value="<?php echo $form['title']; ?>"> <?php echo $error['title'] ?>


                <label for="currency">Currency: <em>*</em></label>
                <input type="text" name="currency" id="currency" value="<?php echo $form['currency']; ?>"> <?php echo $error['currency'] ?>

                <input type="submit" name="submit" id="submit">

        </form>

PHP Validator

  //variables
    $error_open = "<label class='error'>";
    $error_close = "</label>";
    $valid_form = TRUE;
    $redirect = "success.php";

    $form_elements = array('title', 'currency');
    $required = array('title', 'currency');

    foreach ($required as $require)
    {
        $error[$require] = '';
    }

    if (isset($_POST['submit']))
    {
        //process form

        //get form data
        foreach ($form_elements as $element)
        {
            $form[$element] = htmlspecialchars($_POST[$element]);
        }

        //check form elements
            //check required fields
            if ($form['title'] == '')
            {
                $error['title'] =  $error_open . "* This field is required" . $error_close;
                $valid_form = FALSE;
            }
            if ($form['currency'] == '')
            {
                $error['currency'] = $error_open . "* This field is required" . $error_close;
                $valid_form = FALSE;
            }


            //check formatting
            if ($error['title'] == '' && !preg_match('/^([A-Za-z0-9_-]+)/', $form['title']))
            {

                $error['title'] = $error_open . "* Enter a valid descriptive title" . $error_close;
                $valid_form = FALSE;

            }

            if ($error['currency'] == '' && !preg_match('/^\s*[+-]?(\d*\.\d\d)\s*$/', $form['currency']))
            {

                $error['currency'] = $error_open . " Enter a valid decimal number <br> * Do not include Dollar($) sign <br> * Example: (1.00)" . $error_close;
                $valid_form = FALSE;

            }



        //check for bad data
            if (contains_bad_str($form['title']) ||
            contains_bad_str($form['currency']))
            {
                $valid_form = FALSE;

            }

            if (contains_bad_str($form['title']) ||
            contains_bad_str($form['currency']))
            {
                $valid_form = FALSE;

            }

        //check if form is valid
        if ($valid_form)
        {

            //redirect
            header("Location: " . $redirect);

        }
        else
        {
            include('form.php');

        }

    }
  • 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-06T02:47:06+00:00Added an answer on June 6, 2026 at 2:47 am

    this worked for me.

    function contains_bad_str($s) {
        $r = preg_match ( "/cheese|rice|beans/i" , $s);
        return $r;
    }
    
    // variables
    $error_open = "<label class='error'>";
    $error_close = "</label>";
    $valid_form = TRUE;
    $redirect = "success.php";
    
    $form_elements = array('title', 'currency');
    $metadata = array('title'=> array('/^([A-Za-z0-9_-]+)/',
                                      "* Enter a valid descriptive title"),
                      'currency' => array('/^\s*[+-]?(\d*\.\d\d)\s*$/',
                                          "* Enter a valid decimal number <br/>" .
                                          "* Do not include Dollar($) sign <br/>" .
                                          "* Example: (1.00)" ));
    
    foreach ($form_elements as $element) {
        $error[$element] = '';
    }
    
    if (isset($_POST['submit'])) {
        // get form data
        foreach ($form_elements as $element) {
            $form[$element] = htmlspecialchars($_POST[$element]);
            // check presence
            if ($form[$element] == '') {
                $error[$element] =  $error_open . "* required" . $error_close;
                $valid_form = FALSE;
            }
            // check formatting
            elseif (!preg_match($metadata[$element][0], $form[$element])) {
                $error[$element] = $error_open . $metadata[$element][1] . $error_close;
                $valid_form = FALSE;
            }
            // sanitize
            elseif (contains_bad_str($form[$element])) {
                $error[$element] =  $error_open . "* bad string" . $error_close;
                $valid_form = FALSE;
            }
        }
    
        if ($valid_form) {
            header("Location: " . $redirect);
        }
        else {
            include('form.php');
        }
    }
    else {
        // blank form
        $form = array('title'=>'', 'currency'=>'');
        $error = array('title'=>'', 'currency'=>'');
        include('form.php');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP form that needs some very simple validation on submit. I'd
I have a simple php file that gets info from mysql database display inside
I have a simple html form that I've added validation to using the JQuery
I have a very simple form with a name field and two submit buttons:
I have a relatively simple question. Will using PHP guarantee that a form is
I have a simple PHP function that will grab information from a database based
I have a simple php script on my domain that sends me an email:
I have a simple PHP app that prints 'hello world'. When I run it
I have a simple PHP script that will either serve up a streaming ASF
I have a simple php page that displays data from a mysql database. I

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.