0

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?

  • Ooo, you can't send a collected array of key -> value pairs through an html POST, just an array of values to one name. You gotta send JSON if you want a more complex data structure. I'm on the bus back from my job, I'll help ya out more in a few hours. – jrothafer Jul 30 '15 at 20:33
  • Also, you need to comment on my answer to get my attention :p and can you post exactly the result your looking for (example data structure or email output)? – jrothafer Jul 30 '15 at 20:33
  • ok cool. Thanks again! So Im only posting this info to an email. And was trying to show only the string values of the array, but I think - 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? – j_lemon Jul 31 '15 at 14:28
  • Lol we're working on it : w3.org/TR/html-json-forms hopefully I'll see this in my lifetime. But yes, for form submission, that's as compex as you can get ( basically $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!). – jrothafer Jul 31 '15 at 14:35
  • Awesome! Yes, its a static form. Thanks so much for all of your help! I will mark your answer and the answer! – j_lemon Jul 31 '15 at 14:42
1

Please post your <form> tags.

First thing, no square brackets belong on this line:

$checkList  = $this->EE->input->post(['check_list']);

It should be

$checkList  = $this->EE->input->post('check_list');

And please do some debugging if you're confused:

$checkList  = $this->EE->input->post('check_list');

ob_start();
var_dump($checkList);
$result = ob_get_clean();
// email the $result variable to yourself, it is a string of that var_dump
// that will tell you what you're getting

// ...

$this->EE->email->message(entities_to_ascii($msg . " - " . $result));

// ... continue

And for frigs' sake, stop using camelCase in PHP. Sorry, I don't mean to be a jerk but it's really awful a million miles away from PSR-1 standard.

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md

  • Thank you for the help! I added the form tags to form, and will do some debugging. Thanks for the link to the php standard, will bookmark this. And thank you for the advice to avoid camelCase. Will implement the changes you suggested. – j_lemon Jul 30 '15 at 18:10
  • No problem! I really don't mean to be a jerk about the styling, but in my experience, a messy code base/practices scares away the best talent! Let me know what happens (edit your question) and I'll assist further if needed. – jrothafer Jul 30 '15 at 18:16
  • not a jerk at all! I appreciate the direction to the right resources. – j_lemon Jul 30 '15 at 18:23
  • You never said what version of EE, but you should be able to use this convention: ee()->input->post('name'); – Jim Wyse Jul 31 '15 at 15:53
0

Here is a quick solution ...try this

    <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>

//PHP CODE

$check_list = implode("|",$_POST['check_list']);

//now just put $check_list in your mail body....ping back if you need any help
  • Thanks for adding this alternate answer! This also works to solve the original problem. – j_lemon Jan 7 '16 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.