2

I'm building a laravel-app where I'm connecting mailchimp to the contact-form. The user has to accept conditions and has the possibility to receive the newsletter by checking a checkbox. Right now, when the user accepts both the conditions and the newsletter, the form is submitted successfully, but when the user does not check the newsletter checkbox, the form is not submitted and I don't really know why.

here the input

<input name="conditions" value="1" type="checkbox" class="custom-control-input" id="applyConditions" required>
<input type="checkbox" name="newsletter" class="custom-control-input" id="contactNewsletterSubscribe">

and my controller:

class ContactController extends Controller
{
public function store()
{
    $validator = Validator::make(request()->all(), [
        'email' => ['required', 'email'],
        'name' => ['required', 'string'],
        'phone' => ['string'],
        'subject' => ['required', 'string'],
        'message' => ['required', 'string'],
        'conditions' => ['accepted', 'boolean'],
    ]);

    if ($validator->fails()) {
        return response()
            ->json($validator->messages(), 400);
    }

    if (request()->has('newsletter')) {
        Newsletter::subscribePending(request()->email);

        if (!Newsletter::lastActionSucceeded()) {
            return response()
                ->json($validator->messages(), 400);
        }
    }

    Mail::to('[email protected]')
        ->send(new ContactUsMessage([
            'email' => request()->email,
            'name' => request()->name,
            'phone' => request()->phone,
            'subject' => request()->subject,
            'message' => request()->message,
        ]));

    return response()->json('OK', 200);
}

}

When I check both conditions and the newsletter checkboxes, the form data returns:

conditions: 1
newsletter: on

When conditions are accepted but the newsletter checkbox is unchecked, the form data returns:

conditions: 1
newsletter: undefined

Can someone tell me what I'm doing wrong?

EDIT

I'm sending the form data with jquery/axios:

if ($('#contact input[name="newsletter"]:checked')) {
    formData.append(
       "newsletter",
       $('#contact input[name="newsletter"]:checked').val()
   );
} else {
    formData.append(
       "newsletter",
       $('#contact input[name="newsletter"]').val()
    );
}
6
  • How do you send your form? jQuery? Commented May 24, 2019 at 12:09
  • @Tarasovych yes I am... please check my updated question... Commented May 24, 2019 at 12:12
  • What if you remove if/else, and just append val() every time? Commented May 24, 2019 at 12:17
  • you can add value in checkbox Commented May 24, 2019 at 12:18
  • By the way, "the form is not submitted" - any errors in console? Commented May 24, 2019 at 12:23

2 Answers 2

1

Can someone tell me what I'm doing wrong?

First of all, your jQuery code is not much of use:

if ($('#contact input[name="newsletter"]:checked')) {
    formData.append(
       "newsletter",
       $('#contact input[name="newsletter"]:checked').val()
   );
} else {
    formData.append(
       "newsletter",
       $('#contact input[name="newsletter"]').val()
    );
}

In both cases you are trying to get value attribute which is not exists. and you get on only because it's a browser default behavior to assign On value to checked checkbox when <input> tag is not have value attribute.

Moreover, if you set the attribute you will get its value all the time, regardless of checked status of checkbox ;). Try it to see for yourself.

Next, I would go with a simple code, like this:

formData.append(
    "newsletter",
    $('#contact input[name="newsletter"]:checked').length
);

w/o any conditions. in the controller you will get either 1, either 0.

then, in the controller, you could do like this:

if (!empty(request()->newsletter)) {
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Exactly what I was looking for and it makes total sense! Thanks alot :-)
1

Try this:

if ($('#contact input[name="newsletter"]:checked')) {
    formData.append(
       "newsletter",
       $('#contact input[name="newsletter"]:checked').val()
   );
} else {
    formData.append(
       "newsletter",
       "off"
    );
}

So it should be newsletter: off on the server.

P. S. I'd suggest to use boolean values for checkboxes.

Comments

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.