<p>Found my answer <a href="https://github.com/craftcms/cms/blob/master/src/web/Request.php" rel="nofollow noreferrer">here</a></p>

<pre><code>// get $_POST['foo']['bar'], if it exists
$bar = Craft::$app-&gt;request-&gt;getBodyParam('foo.bar');
</code></pre>

<p>or inserting it into my code</p>

<pre><code>Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) 
{
    $request = Craft::$app-&gt;getRequest();

    if($request-&gt;getBodyParam('fields.sendEmail') == 1)
    {
        $html = $request-&gt;getBodyParam('fields.sendEmail');
        $subject = "testing";
        $mail = "example@gmail.com";
        $this-&gt;sendMail($html, $subject, $mail);
    }
});
</code></pre>

CC BY-SA 4.0
2018-12-19T18:31:08.743Z
If someone wants to expand on this I'd be happy to select someone else's contribution as top answer, such as a possible better way of doing this.
Darryl Hardin
CC BY-SA 4.0
2018-12-19T19:53:13.857Z
Just occurred to me my answer above isn't complete. While it did accomplish what I asked, `EVENT_AFTER_SAVE_ELEMENT` is for any element being saved, which means I have to find a way to determine what element is being saved.
Darryl Hardin
CC BY-SA 4.0
2018-12-19T18:29:44.553Z
2018-12-19T19:54:17.793Z
2018-12-19T19:54:17.793Z
Darryl Hardin
<p>I am trying to get the value of a custom field in entries, but I can't seem to get it to grab that value. I'm assuming I'm pulling it too early or too late, otherwise I have no idea.</p>

<p>I can pull the value the of the lightswitch <code>enabled</code></p>

<pre><code>Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) 
    {
        $request = Craft::$app-&gt;getRequest();

        // will successfully send email if enabled lightswitch is on
        if($request-&gt;getBodyParam('enabled') == 1)
        {
            $html = $request-&gt;getBodyParam('enabled');
            $subject = "testing";
            $mail = "example@gmail.com";
            return $this-&gt;sendMail($html, $subject, $mail);
        }
    });
</code></pre>

<p>The below doesn't return anything at all for the custom field <code>fields[sendEmail]</code></p>

<pre><code>Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) 
    {
        $request = Craft::$app-&gt;getRequest();

        // will not send email no matter what position the lightswitch is in
        if($request-&gt;getBodyParam('fields[sendEmail]') == 1)
        {
            $html = $request-&gt;getBodyParam('fields[sendEmail]');
            $subject = "testing";
            $mail = "example@gmail.com";
            return $this-&gt;sendMail($html, $subject, $mail);
        }
    });
</code></pre>

<p>I tried changing the event handler to <code>EVENT_BEFORE_SAVE_ELEMENT</code> but it made no difference.</p>

<plugin-development><craft3>
Craft 3 Plugin not returning custom field value
# Craft 3 Plugin not returning custom field value

(Asked by: username_0 on 2018-12-19)

I am trying to get the value of a custom field in entries, but I can't seem to get it to grab that value. I'm assuming I'm pulling it too early or too late, otherwise I have no idea.
I can pull the value the of the lightswitch enabled
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) 
    {
        $request = Craft::$app->getRequest();

        // will successfully send email if enabled lightswitch is on
        if($request->getBodyParam('enabled') == 1)
        {
            $html = $request->getBodyParam('enabled');
            $subject = "testing";
            $mail = "example@gmail.com";
            return $this->sendMail($html, $subject, $mail);
        }
    });

The below doesn't return anything at all for the custom field fields[sendEmail]
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) 
    {
        $request = Craft::$app->getRequest();

        // will not send email no matter what position the lightswitch is in
        if($request->getBodyParam('fields[sendEmail]') == 1)
        {
            $html = $request->getBodyParam('fields[sendEmail]');
            $subject = "testing";
            $mail = "example@gmail.com";
            return $this->sendMail($html, $subject, $mail);
        }
    });

I tried changing the event handler to EVENT_BEFORE_SAVE_ELEMENT but it made no difference.

**Tags:** plugin-development, craft3

---

## 1 answers

### Answer by username_0 on 2018-12-19. Score: 2 | Accepted answer

Found my answer here
// get $_POST['foo']['bar'], if it exists
$bar = Craft::$app->request->getBodyParam('foo.bar');

or inserting it into my code
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) 
{
    $request = Craft::$app->getRequest();

    if($request->getBodyParam('fields.sendEmail') == 1)
    {
        $html = $request->getBodyParam('fields.sendEmail');
        $subject = "testing";
        $mail = "example@gmail.com";
        $this->sendMail($html, $subject, $mail);
    }
});

> **Comments:**
> **username_0**: If someone wants to expand on this I'd be happy to select someone else's contribution as top answer, such as a possible better way of doing this.  
> **username_0**: Just occurred to me my answer above isn't complete. While it did accomplish what I asked, `EVENT_AFTER_SAVE_ELEMENT` is for any element being saved, which means I have to find a way to determine what element is being saved.  

---

