I have a form on my website that sends the captured values of the form to an email address. On my form I have input types of text, and checkbox. But every time I submit the form with checkbox values checked the email shows up blank, with not captured values. I am using a foreach loop to check the checkbox array of its values, but maybe the loop is only seeing the Array and not the individual values of the array? Also the form is no longer redirecting to the redirect page. That must mean that the http_code is > 200 and the info is posting with an error code, but not sure what error?
Any and all help to understand the posting process would be greatly appreciated.
EDIT I added the form tags to the html. The form posts to submit.html (where the php lives)
my html
<form id="form" method="post" action="/submit" enctype="multipart/form-data">
<div class="field">
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</div>
<div class="field">
<label for="categoryType">Category type:</label>
<input type="checkbox" name="check_list[]" value="value1">
<label>value1</label>
<input type="checkbox" name="check_list[]" value="value2">
<label>value2</label>
<input type="checkbox" name="check_list[]" value="value3">
<label>value3</label>
</div>
</form>
My php: submit.html
<?php
$name = $this->EE->input->post('name');
$checkList = $this->EE->input->post(['check_list']);
foreach ($checkList as $check=>$value) {
$msg = '<p><strong>Category</strong> '.$value.'</p>';
}
$msg = '<p><strong>Name:</strong> '.$name.'</p>';
$msg .= '<p><strong>Category</strong> '.$value.'</p>';
$this->EE->email->wordwrap = true;
$this->EE->email->mailtype = 'html';
$this->EE->email->to('email@emailcom');
$this->EE->email->subject('form');
$this->EE->email->message(entities_to_ascii($msg));
$this->EE->email->Send();
if($http_code > 200) {
print "<p><strong>Error</strong> </p>";
} else {
header( 'Location: redirecturl' ) ;
}
?>
EDIT 2
With the suggestions from @jrothafer I have updated the submit.html file to look like this:
<?php
$name = $this->EE->input->post('name');
$check_list = $this->EE->input->post(['check_list']);
$msg = '<p><strong>Name:</strong> '.$name.'</p>';
$msg .= '<p><strong>Category</strong> '.$value.'</p>';
ob_start();
var_dump($check_list);
$result = ob_get_clean();
$this->EE->email->wordwrap = true;
$this->EE->email->mailtype = 'html';
$this->EE->email->to('email@emailcom');
$this->EE->email->subject('form');
$this->EE->email->message(entities_to_ascii($msg . " - " . $result));
$this->EE->email->Send();
if($http_code > 200) {
print "<p><strong>Error</strong> </p>";
} else {
header( 'Location: redirecturl' ) ;
}
?>
By correctly formatting the brackets I think I have eliminated the error code and the page successfully redirects to the redirect url on submit. The email is sending and the ob_get_clean(); returns
array(2) { [0]=> string(5) "value1" [1]=> string(5) "value2" }
Also just noting here that the foreach loop returns only the last value of the array.
foreach ($check_list as $check=>$value) {
$msg = '<p><strong>Category</strong> '.$value.'</p>';
}
So now I tried using
ob_start();
var_export($check_list);
$result = ob_get_clean();
and the return captured value is:
array ( 0 => 'value_1', 1 => 'value_2', )
Does this mean I need to capture each value individually? Or can you point me in the direction to find out how to return specific key value pairs?
- array ( 0 => 'sms', 1 => 'social', 2 => 'survey', )
is the closest I can get to making sure all array items show up. The foreach loop only prints the last value of the array. Is it true that this is the only format to post an array?$array_var = array ( 0 => 'value_1', 1 => 'value_2', etc...)
. So you have to adapt your form to that, unless you wanna go with an ajax call. It basically makes dynamic plan POST forms incredibly annoying to create (I hope yours is static!).