This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

SlartyBartfast
SlartyBartfast

I am trying to implement a simple Ajax mail form. However instead of the successful response updating my div with the success message it reloads the whole partial where the form resides? The code:

The form (part of the footer partial):

<div class="span4">
                <h3>Newsletter</h3>
                <p>Subscribe to our monthly newsletter and be the first to know about our news and special deals!</p>
                <form data-request="onEmail" data-request-update="footer: '#result'">
                    <input name="email" type="text" placeholder="Enter your E-mail" />
                    <input type="submit" class="btn" value="Ok" />
                </form>
                <div id="result">
                    {% if result %}
                <span class="lead">
                <span class="label label-success">{{ result }}</span>
                </span>
                {% else %}
                    <span class="text-muted">
                        <!-- nothing -->
                    </span>
                {% endif %}
                </div>

The php (written in the code section of the layout):

function onEmail()
{
$data = Input::all();
$rules = ['email' => 'required|email'];
$msgs = ['required' => 'Please fill in your email','email' => 'Please enter a valid email address'];
$v = Validator::make($data, $rules, $msgs);

if ($v->fails())
{
    $this['result'] = $v->messages()->first();
}else{
    Mail::queue('email.newsletter', $data, function($message)
    {
        $message->from(post('email'));

        $message->to('simon@skwebproject.com')->subject('New newsletter registration');

    });

    $this['result'] = 'Success!';
}

}

Does it matter that the php code is written in the layout section? Am I missing something obvious?

Than you for any help.

Simon

Last updated

SlartyBartfast
SlartyBartfast

Fixed it:

The data-request-update="footer: '#result'" was updating the div with the footer partial and the ajax result.

I created a result.htm partial (useful for later on) and called data-request-update="result: '#result'".

The result partial consists of:

{% if result %}
<span class="lead">
    <span class="label label-success">{{ result }}</span>
</span>
{% else %}
<span class="text-muted">
    <!-- nothing -->
</span>
{% endif %}

And the form now has:

<div id="result">{% partial "result" %} </div>

Below it.

See https://octobercms.com/forum/post/how-to-send-mails which helped me.

Last updated

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.