1

I want to build a simple form which collect input value and send email. some input name are array, and I just couldn't get the value from these arrays. Below is the code.

$keysToKeep = array('CustomFields[6]', 'email');
$values = array_intersect_key($_POST, array_flip($keysToKeep));
print_r($keysToKeep);
foreach ($values as $name => $value)
{
    $message .= $name . ': ' . $value . PHP_EOL;
}

HTML Code:

Your name:
<input type="text" name="CustomFields[6]" id="CustomFields_6_337" value="" fid="4" fname="Name" size='64' maxlength='64'>
Your Email Address:
<input type="text" class="email" name="email" fid="e" fname="Email" ftype="Email" reqd="1"/>

2 Answers 2

1

Accessing array values in PHP forms goes like this.

$customField = $_POST['CustomFields'][6];
Sign up to request clarification or add additional context in comments.

Comments

0

The following code will allow to step through the $_POST array and collect the data it contains. If any of $_POST array's elements is an array itself another loop is used.

//loop collects form data     
foreach($_POST as $key => $field) {
     //checks if data being collected is an array
     //if it is an array another loop is used to collect data            
     if(is_array($field)){
            foreach($field as $subkey => $subfield){
                echo $subfield;
            }
     }
     else{
            echo $field;
     }
}

3 Comments

Thanks MK.Actually the form is from 3rd party, I can't change the name of the input field.
Are name and email the only form fields that you have to extract data from?
No, the form is big, that's why i want to use the loop to catach all the datas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.