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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:51:47+00:00 2026-05-29T08:51:47+00:00

I would like to cycle through all the elements in a submitted form, assign

  • 0

I would like to cycle through all the elements in a submitted form, assign each element its own variable, and then check if they are required. IF they are required, they will be checked if they are empty. So far, my form looks like this:

<form id="newitem" action="addnew.php" method="POST">
        <label for="title">Title: </label>
        <input type="text" name="title" id="title" required="required" /><br />
        <label for="description">Description: </label>
        <textarea name="description" id="website" cols="40" rows="5" ></textarea><br />
        <input type="hidden" name="formsubmit" value="true" /><!-- Option to know we've sumbited the form -->
        <input type="submit" value="Submit" id="submit" />
</form>

I then have it validated using this:

if (isset($_POST['formsubmit'])) {
    // form has been submitted
    $errors = Array();
    foreach($_POST as $key => $value) {
        $$key = $value;
        if (!$value) {
            array_push($errors, 'Please enter a value for ' . $key);
        }
    }
    if (count($errors) == 0) {
        echo 'Form validated';
    } else {
        showErrors($errors);
    }
}

This currently checks if the elements have a value. However, I would like it to check if that field is required, using the HTML required="required".
Also, although I feel I do understand how this works, I’m not sure I do. I think $$key = $value basically means create a variable with the value of $key and give it the value of $value. So, $key is the name of the element, whilst $value is its value. However, I really don’t get the foreach($_POST as $key => $value) {. I know it cycles though all the $_POST‘s, but don’t really know any more than that.
Thanks for any help!

  • 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-29T08:51:47+00:00Added an answer on May 29, 2026 at 8:51 am

    If think you are looking for something like

    $errors = Array();
    foreach($_POST as $key => $value) {
        if (empty($value)) {
            $errors[] = 'Please enter a value for ' . $key;
        }
    }
    

    Foreach loops over an associative array and gives you the key and value of every ‘element’ in the array. In this case it will give you the name of a field in $key and the value of a field in $value.

    See http://php.net/manual/en/control-structures.foreach.php for more info.

    However, if a field was not submitted at all (i.e. not send from the client to the back-end), the $_FORM would not contain that field at all. Thus, to check if some required fields are filled, you would need to have that list on the serverside as well..! The code would then be something like:

    $errors = Array();
    $requiredfields = Array("somefield" => "...", "secondfield" => "...");
    foreach($_POST as $key => $value) {
        unset($requiredfields[$key]); // The encountered field $key is present
        if (empty($value)) {
            $errors[] = 'Please enter a value for ' . $key;
        }
    }
    // Loop over all the required fields that are not found
    foreach($requiredfields as $key => $val) {
        $errors[] = 'Please enter a value for ' . $key;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to cycle through four li elements that all contain tags, setting
The goal is that I would like to cycle through ALL of the .CSV
I would like to make a cycle over the following elements: [1,2,11,12,21,22,111,112,121,122,....,222222] or for
I would like to breakup certain phases in the maven life cycle into sub
I currently have a C# form, although I would like to find out how
I would like to achieve the following: I want a free Application Lifecycle Management
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at

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.