0

first of all I've been googling about this and found that I need to insert array box inside the checkbox name. I did that but it returned an error. May I knew what I did wrong and how to fix it?

Here's the code that I manage to echo only 1 value although both checkbox are been checked. (Before adding "[]")

<form method="post">
    <div class="form-group">
    <p>If yes, which type of disposable contact lens have you worn before?</p>
    <input type="checkbox" name="disposable_lens"  value="Monthly"/> Monthly<br>
    <input type="checkbox" name="disposable_lens"  value="Daily Disposables"/> Daily Disposables<br>
</div>
</form>

if(empty($_POST["disposable_lens"]))
{
    $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
}
else
{
    $disposable_lens = clean_text($_POST["disposable_lens"]);
    echo $disposable_lens."</br>";
}

This is the code after I inserted the "[]" and used foreach statement

<div class="form-group">
    <p>If yes, which type of disposable contact lens have you worn before?</p>
    <input type="checkbox" name="disposable_lens[]"  value="Monthly"/> Monthly<br>
    <input type="checkbox" name="disposable_lens[]"  value="Daily Disposables"/> Daily Disposables<br>
</div>

    if(empty($_POST["disposable_lens"]))
{
    $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
}
else
{
    foreach($_POST['disposable_lens'] as $disposable_lens){
        echo $disposable_lens."</br>";
}

I got this error when I go to index.php

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\send_mail\index.php on line 216

And I checked on line 216, there are no any word, just got one ~ (in red) and I cant delete it. This is the image screenshot I took.

Barmar
  • 741,623
  • 53
  • 500
  • 612
iceiceicy
  • 705
  • 1
  • 9
  • 24

2 Answers2

1

As I said in my comment you are missing a } at the end of your code for the else statement. Also, I do not see where you are setting your php tags when you transition from html to php. This is how your code should look:

<div class="form-group">
    <p>If yes, which type of disposable contact lens have you worn before?</p>
    <input type="checkbox" name="disposable_lens[]"  value="Monthly"/> Monthly<br>
    <input type="checkbox" name="disposable_lens[]"  value="Daily Disposables"/> Daily Disposables<br>
</div>

<?php

    if(empty($_POST["disposable_lens"])){

    $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';

    }else{

      foreach($_POST['disposable_lens'] as $disposable_lens){

        echo $disposable_lens."</br>";

      }

    }

?>
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
0

You forgot a } after the foreach.

Change :

 if(empty($_POST["disposable_lens"]))
{
    $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
}
else
{
    foreach($_POST['disposable_lens'] as $disposable_lens){
        echo $disposable_lens."</br>";
}

To :

    if(empty($_POST["disposable_lens"]))
{
    $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
}
else
{
    foreach($_POST['disposable_lens'] as $disposable_lens){
        echo $disposable_lens."</br>";
    }
}
Sébastien S.
  • 1,444
  • 8
  • 14