Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Assigning a Foreach loop…

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #40886
    robertallen
    Participant

    Hi all,

    I’m still new to PHP. I took 5 months at a community college and still don’t get everything. Currently I’m trying to make a form mail script with checkboxes. All the other types of inputs are easy, except for this.

    My HTML code is this…

    Have you ever had*:
    Diabetes Hypoglycemia Seizures Fainting Spells Eating Disorders Respiratory Problems Psychiatric Care Phobias

    And my PHP code is this:

    $subject = ‘Awe Star Online Missionary Application Form’;
    $webMaster = ’[email protected]’;

    $body = << E-Mail – $email

    Have you ever had – $haveyoueverhad

    Submitted on:
    $time

    EOD;

    /* Sends E-Mail */
    $headers = “From: $emailrn”;
    $headers . “Content-type: text/htmlrn”;
    $success = mail($webMaster, $subject, $body, $headers);

    /* Results after E-Mail is sent */
    header(‘Location:thankyou.php’);

    Now, learning about checkboxes, I’ve figured out that I need a foreach loop as checkboxes submit results into an array of values.

    foreach($haveyouever as $haveyouever_result) {
    echo $haveyouever_result)
    }

    But, I was wondering, is there a way to output this foreach loop into a variable so I can add that variable into the EOD body for e-mail purposes?

    #114850
    robertallen
    Participant

    I can’t seem to add the code feature to the HTML but the name of the checkboxes are $haveyouever[] with a value of whatever it is (not a numerical value)

    #114871
    SgtLegend
    Member

    You can use the `join()` function which accepts a separator then an array.

    $haveyouever = join(‘, ‘, $haveyouever);

    #114885
    robertallen
    Participant

    I tried putting that inside the foreach loop but it received errors. Is this something I do beforehand?

    #114927
    __
    Participant

    `$haveyouever` is the array of results from the form submission, correct?

    no, you don’t use a loop.

    ****************
    p.s. `join()` is an alias for `implode()`
    (I’d never heard of `join()` before!)

    *p.p.s. – nice to see a college course that teaches using HEREDOC notation! : )*

    #114936
    robertallen
    Participant

    That worked beautifully! Thank you guys so much.

    I was googling last night for answers and there were so many if statements and whatnot, I figured it was a lot simpler than 20 lines of code for one checkbox.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.