Question

I was trying to ask about required field print out to make sure its working. But the question was rated so badly to prevent me from asking more questions. this was the solution for it, try to rate it up so i can remove block.

 <?php   if (empty($_POST) === false) {  
    $required_fields = array(
        'username', 'password', 'password_again', 
        'first_name', 'last_name', 
        'email', 'bday', 
        'county', 'zip', 'gender'
    );
    // create our output array
    $output = array(); 
    $altOutput = array();

    // this adds the post value to our output array
    // using a foreach loop
    // it simply looks for the POST by the field in your array
    // if it's found and not empty, it will add to our new output array
    foreach ($required_fields as $field) 
        if (!empty($_POST[$field])) {
            // this adds the field to your new output array
            $output[] = $_POST[$field];
            // this removes it from the POST array
            unset($_POST[$field]);
        }

    // now output the new array having only the fields you want
    echo '<pre>', print_r($output, true), '</pre>';

    // if you want to print out what's left over
    echo '<pre>', print_r($_POST, true), '</pre>';}
Was it helpful?

Solution

So, I just noticed, despite having fully answered the question. Despite even 
being selected as correct answer. The 3 upvotes I had gained on this lost the 
final mark today, thus putting this answer at 0. I also noticed this question 
is now marked closed. Could someone please explain to me why an answered 
question is closed after the fact and why the selected correct answer is 
steadily being downvoted?


It's doing exactly as you said because that's what you have set up for it to do.

Quick break down:

<?php // init php code
    // here you check if post has anything in it's array
    // that's right, $_POST will be an ARRAY
    if (empty($_POST) === false) { // same as `if (!empty($_POST)) {` except longer
        // here you simply set an array variable
        $required_fields = array(username, password, password_again, first_name, last_name, email, bday, county, zip, gender);
        // here you're one lining it, but suffice it to say, you're sending a <pre> tag to the browser
        // which helps to make what `print_r` produces more readable as you're printing an array
        echo '<pre>', print_r($_POST, true), '</pre>';

As you may have noticed, you're not even using your array, $required_fields.

Now

If you would like to print each one of those fields and only those fields AS AN ARRAY, then you need to make it so

<?php   
if (empty($_POST) === false) {  
        $required_fields = array(
            'username', 'password', 'password_again', 
            'first_name', 'last_name', 
            'email', 'bday', 
            'county', 'zip', 'gender'
        );

        // create our output array
        $output = array(); 
        // $altOutput = array(); // not used in this example

        // this adds the post value to our output array
        // using a foreach loop
        // it simply looks for the POST by the field in your array
        // if it's found and not empty, it will add to our new output array
        foreach ($required_fields as $field) 
            if (!empty($_POST[$field])) {
                // this adds the field to your new output array
                $output[] = $_POST[$field];
                // this removes it from the POST array
                unset($_POST[$field]);
            }

        // now output the new array having only the fields you want
        echo '<pre>', print_r($output, true), '</pre>';

        // if you want to print out what's left over
        echo '<pre>', print_r($_POST, true), '</pre>';
}

OTHER TIPS

It seems you are having a little trouble understanding the code. Your $required_fields array isn't even being used again. I took spend some time making this work:

$required_fields = array(username, password, password_again, first_name, last_name, email, bday, county, zip, gender);
foreach($required_fields as $field) {
    print $_POST[$field];
}

Notice that we are looping through the $required_fields array and pulling the individual data from $_POST.

Here is a complete example, that cancels further execution if one or more fields are empty and prints out the names of those fields to the user:

if( !empty( $_POST ) )
{
    $requiredFields = array(
        'username', 
        'password', 
        'password_again', 
        'first_name', 
        'last_name', 
        'email', 
        'bday', 
        'county', 
        'zip', 
        'gender',
    );
    $missingFields = array();

    foreach( $requiredFields as $fieldName )
    {
        if( empty( $_POST[$fieldName] ) )
        {
            $missingFields[] = $fieldName;
        }
    }

    if( !empty( $missingFields ) )
    {
        echo sprintf( 'You have not filled out the following fields: %s.', implode( ', ', $missingFields ) );
        return;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top