-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
problem with response and ajax #163
Comments
I've just make more tests and it appears that with JSON workflow using |
Hi! Any news about this? |
Sorry i've been busy with getting Grav 1.3 and Admin 1.5 released. I'll try to take a look at this during the week. |
Hi @rhukster! |
I'm not sure returning 500 error is the correct response. Calling a form with |
Hi guys, sorry I've been in a hurricane the last ten days. @flaviocopes this will not return the appropriate error message for each case. Also it will not resolve the bug. (the returned message will still be the full HTML code of the page) @rhukster yes that's the expected behaviour. |
I think i've fixed the issue with the whole HTML page being returned on error. I was able to replicate this and fix it for the HTML version of the Ajax form. However, I'm not sure how to accurately replicate your JS test. Can you provide me with the full page including the the form, and anything custom you've done so i can test it? Also can you test latest fix??? |
Hi @rhukster ! Thanks for the fix, I just tried the last update and it seems to work great for the html message 👍 There is only one thing to improve, actually the json response from the form always return a Here is the form: ---
title: Contact
form:
name: contact
template: form-messages
fields:
-
name: firstname
label: false
placeholder: 'First Name'
autofocus: true
autocomplete: true
type: text
validate:
required: true
-
name: lastname
label: false
placeholder: 'Last Name'
autocomplete: true
type: text
validate:
required: true
-
name: email
label: false
placeholder: Email
autocomplete: true
type: email
validate:
required: true
-
name: phone
label: false
placeholder: Phone
autocomplete: true
type: tel
-
name: message
label: false
placeholder: Message
type: textarea
validate:
required: true
-
name: honeypot
type: honeypot
buttons:
-
type: submit
value: Submit
process:
-
email:
subject: 'Message from {{ form.value.firstname|e }} {{ form.value.lastname|e }}'
body: [{ content_type: text/html, body: '{% include ''emails/message_admin_receipt.html.twig'' %}' }, { content_type: text/plain, body: '{% include ''forms/data.txt.twig'' %}', charset: iso-8859-1 }]
-
save:
dateformat: Ymd-His-u
extension: txt
body: '{% include ''forms/data.txt.twig'' %}'
-
message: 'Thank you {{ form.value.firstname|e }}. I will contact you shortly.'
--- here is the twig template: {% extends 'partials/base.html.twig' %}
{% block content %}
{{ page.content }}
{% include "forms/form.html.twig" %}
<div class="contact-response"></div>
{% endblock %} and there is the js to test success, error: $(document).ready(function(){
let contactForm = $('#contact')
let responseOutput = $('.contact-response')
// FORM SUBMISSION
contactForm.on('submit', function (event) {
// PREVENT DEFAULT FORM SUBMISSION
event.preventDefault()
// SUBMIT THE FORM VIA AJAX
$.ajax({
url: contactForm.attr('action'),
type: contactForm.attr('method'),
data: contactForm.serialize(),
dataType: 'html',
// ON AJAX SUCCES
success: (message, status) => {
responseOutput.html(message)
console.log(status)
// IF JSON RETURN 'success' as statut
if (status === 'success') {
console.log('this is a SUCCESS !!!')
}
// IF JSON RETURN 'error' as statut
if (status === 'error') {
console.log('this is an ERROR !!!')
}
}
})
})
}) Let me know if you need more informations |
Ok, here's the problem.. I've implemented a quick solution that basically allows a status code to be set for a form, then in the I could set it to something a bit ambiguous like |
Please check out the feature branch above.. It should work with standard Grav as long as you have |
Working well with ajax now! |
@rhukster don't know if you read my comment about having on option to change red class? should I create a feature request? |
Yah new issue would be good |
@jimblue @rhukster First of all, thank you guys for deep explanation and examples. I have the same problem. I use noty to handle my form submit messages and latest Form plugin 2.10.0. My form code: form:
name: send-form
template: form-messages
#stop sending twice
refresh_prevention: true
fields:
name:
label: Name
placeholder: Your name
outerclasses: 'six columns'
type: text
validate:
required: true
mail:
label: Email
type: email
placeholder: my@mail.com
outerclasses: 'six columns'
validate:
rule: email
required: true
message:
label: Message
placeholder: Small message
outerclasses: 'twelve columns'
type: textarea
validate:
required: true
honeypot:
type: honeypot
buttons:
submit:
type: submit
value: Send
process:
message: 'Thank you!' Here is my js code: var form = $('#send-form');
form.submit(function(e) {
// prevent form submission
e.preventDefault();
// submit the form via Ajax
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
dataType: 'html',
data: form.serialize(),
success: function(result, status) {
console.log(result);
console.log(status);
new Noty({
theme: 'metroui',
layout: 'topCenter',
type: 'success',
text: result,
timeout: 4000
}).show();
}
});
}); And I see Just changed Is this problem still in development? |
Hi @Sogl ! The I think you just miss understand what It's just to verify the connection to the target server is a success. That doesn't mean Grav return a success. That's two different things. In other world if Ajax connection is Inside this response (coming from Grav) you can found:
What @rhukster did in Here is a testing script that should help: var form = $('#contact');
var responseWrapper = $('.contact-response');
// FORM SUBMISSION
form.on('submit', function (event) {
// PREVENT DEFAULT FORM SUBMISSION
event.preventDefault();
// SUBMIT THE FORM VIA AJAX
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
dataType: 'html',
success: function success(data, textStatus, jqXHR) {
console.log('AJAX SUCCESS (connected to target server):');
console.log(' - status: ' + textStatus);
console.log(' - code: ' + jqXHR.status);
console.log(' - data: ' + $.trim(data));
if (jqXHR.status === 200) {
console.log('\n=> GRAV FORM SUCCESS (form is submited)');
}
if (jqXHR.status === 207) {
console.log('\n=> GRAV FORM ERROR (form is not submited)');
}
responseWrapper.html(data);
},
error: function error(jqXHR, textStatus, errorThrown) {
console.log('AJAX ERROR (not connected to target server):');
console.log(' - status: ' + textStatus);
console.log(' - code: ' + jqXHR.status);
console.log(' - data: ' + $.trim(errorThrown));
}
});
}); That said you're write the new branch doesn't return the good status if there is an error. Thank you 😄 |
@jimblue code block didn't come through... |
@rhukster just updated... resfresh! |
@rhukster, after thinking a lot about it... I'm sorry but returning a code of Knowing that it's definitively better to guess if Grav form return an error or a success from the To make it work in the Ajax verification will be more accurate with this markup. Here is a script to make testing easier (open your browser console): var form = $('#contact');
// FORM SUBMISSION
form.on('submit', function (event) {
// PREVENT DEFAULT FORM SUBMISSION
event.preventDefault();
// SUBMIT THE FORM VIA AJAX
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
dataType: 'html',
success: function success(data, textStatus, jqXHR) {
console.log('AJAX SUCCESS (connected to target server):');
console.log(' - status: ' + textStatus);
console.log(' - code: ' + jqXHR.status);
console.log(' - data: ' + $.trim(data));
// CHECK IF THE RESQUEST IS A SUCCESS
if (jqXHR.status >= 200 && jqXHR.status < 400) {
console.log('\n=> REQUEST SUCCESS (reached target server and form is submited)');
// CHECK GRAV FORM RESPONSE STATUS
if (textStatus === 'success') {
console.log('\n=> GRAV FORM SUCCESS (form has no error)');
}
// CHECK GRAV FORM RESPONSE STATUS
if (textStatus === 'error') {
console.log('\n=> GRAV FORM ERROR (form has an error)');
}
} else {
console.log('\n=> RESQUEST ERROR (reached target server but returned an error)');
}
},
error: function error(jqXHR, textStatus, errorThrown) {
console.log('AJAX ERROR (not connected to target server):');
console.log(' - status: ' + textStatus);
console.log(' - code: ' + jqXHR.status);
console.log(' - data: ' + $.trim(errorThrown));
}
});
}); Sorry for all this confusion. It's probably because the markup in PHP / AJAX / HTTP is the same (success, status, error...) but doesn't point to the same things... |
@jimblue Ok, thanks for the explanation... but now we still can't get |
@Sogl |
BTW, i've now merged this branch into develop, so it will be released as part of Form plugin 2.10.0 |
But what do you think about last conclusion? |
sorry had already merged before you had commented (or at least before i read your comment). I will take a look at this before I release. |
Ok I'm preparing something for you to make it even more easier if you can wait 30min. |
yah, i'm knee deep in some contract work anyway right now.. |
@jimblue So... for me is better to just return status from Grav, I could paste it straight as Noty parameter. For now I did a quick workaround checking output div classes: var form = $('#send-form');
form.submit(function(e) {
// prevent form submission
e.preventDefault();
// submit the form via Ajax
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
dataType: 'html',
data: form.serialize(),
success: function(result, status, jqXHR) {
new Noty({
theme: 'metroui',
layout: 'topCenter',
text: result,
timeout: 5000
}).show();
if ($(result).hasClass("green")) {
//close modal window
$.modal.close();
}
}
});
}); And some sass magic: .noty_body {
padding: 0 !important;
color: #fff;
.notices {
padding: 1.25em;
&.green {
background: #66d181;
}
&.red {
background: #f64b39;
}
}
} p.s. And I found why form plugin didn't return codes. I used old |
Ok I've been testing this all around and I've found a definitive fix. @rhukster please follow this method in Grav to make AJAX work for everybody once for all:
––––––– Exemple in php for response $data: $data = json_encode(array('status' => 'success', 'message' => 'thanks for contacting us')); OR $data = json_encode(array('status' => 'error', 'message' => 'something happened, please try again')); –––––––– To help you with this, I've just written a script full of comment and console.log. var form = $('#contact');
// FORM SUBMISSION
form.on('submit', function (event) {
// PREVENT DEFAULT FORM SUBMISSION
event.preventDefault();
// SUBMIT THE FORM VIA AJAX
$.ajax({
type: form.attr('method'),
url: form.attr('action') + '.json',
data: form.serialize(),
dataType: 'json',
// AJAX REQUEST => TRANSFER COMPLETE
success: function success(gravResponse, ajaxStatus, xhr) {
var httpCode = xhr.status;
console.log('%c AJAX SUCCESS (The transfer is finished):', 'color: #bada55');
// RESPONSE HTTP CODE => SUCCESS
if (httpCode >= 200 && httpCode < 400) {
// THE STATUS RETURNED IN GRAV RESPONSE (success or error)
var gravResponseStatus = gravResponse.status;
// THE MESSAGE RETURNED IN GRAV RESPONSE (idealy just the message without html)
var gravResponseMessage = gravResponse.message;
console.log('%c \t=> HTTP CODE SUCCESS (' + httpCode + ')', 'color: #bada55');
// IF GRAV FORM RESPONSE STATUS IS SUCCESS
if (gravResponseStatus === 'success') {
console.log('%c \t\t=> GRAV FORM SUCCESS (form has no error)', 'color: #bada55');
console.log('\t\t\t=> GRAV FORM STATUS (' + gravResponseCode + ')');
console.log('\t\t\t=> GRAV FORM MESSAGE (' + gravResponseMessage + ')');
}
// IF GRAV FORM RESPONSE STATUS IS ERROR
if (gravResponseStatus === 'error') {
console.log('%c \t\t=> GRAV FORM ERROR (form has an error)', 'color: #da5555');
console.log('\t\t\t=> GRAV FORM STATUS (' + gravResponseCode + ')');
console.log('\t\t\t=> GRAV FORM MESSAGE (' + gravResponseMessage + ')');
}
// RESPONSE HTTP CODE => ERROR
} else {
console.log('%c \t=> HTTP CODE ERROR (' + httpCode + ')', 'color: #da5555');
}
},
// AJAX REQUEST => TRANSFER FAILED
error: function error(xhr, ajaxStatus, errorThrown) {
console.log('%c AJAX ERROR (An error happened while file was transfering)', 'color: #da5555');
}
});
}); |
Hi @rhukster Sorry forgot this yesterday... ---
title: Home
form:
name: contact
template: form-messages
fields:
-
name: name
label: false
placeholder: 'Name'
default: 'Bob'
autofocus: true
autocomplete: true
type: text
validate:
required: true
-
name: honeypot
type: honeypot
buttons:
-
type: submit
value: Submit
process:
-
message: 'Thank you {{ form.value.name|e }}. I will contact you shortly.'
--- |
Hi,
I'm working on an Ajax form that use your plugin and I'm having some issues.
I've follow the Grav tutorial for the base and it works great. The form is processed and the response message is properly append to
<div class="contact-response"></div>
.As the basic worked I tried to make some test with
js
to understand how your plugin works.The objective was to catch the
status
and themessage
from the server for every case.Here is the script I use:
To see all cases scenario I've
force
the form to return an error by changing the value of theform-nonce
input or by adding a value tohoneypot
input.In the end that script reveals two problems:
first: the
status
from response is alway equal tosuccess
. Thestatus
never returnerror
as it should. (however I see you are handling that inclasses/form.php
)Having the good
status
return tojs
would be very useful to create betterUser XP
second: the
message
from response looks good onsuccess
:error
it returns the complete html page with the errormessage
append inside{{ page.content }}
:Hope everything is clear, let me know if you need more informations.
Thank you
The text was updated successfully, but these errors were encountered: