Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
add ajax result to contact us submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas van Staden committed Oct 5, 2017
1 parent 35c9fbd commit 6efca91
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 91 deletions.
65 changes: 58 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,57 @@ An example is as such:

<input name="firstname" id="firstname" title="<?php echo Mage::helper('contacts')->__('First Name') ?>" value="<?php echo $this->htmlEscape($_firstname) ?>" class="input-text required-entry" type="text" />

###Submitting Contact Us via AJAX

From version 1.3.0, you can pass two additional params via an AJAX submitted form.
The response form the module will then be a JSON string denoting if teh captcha failed.

Example AJAX call to submit a contact us form:

$j.ajax({
url: $j('#contactForm').attr('action'),
type: 'POST',
data: {
help: $j("#help").val(),
firstname: $j("#firstname").val(),
lastname: $j("#lastname").val(),
email: $j("#email-address").val(),
telephone: $j("#telephone").val(),
suburb: $j("#suburb").val(),
postcode: $j("#postcode").val(),
comment: $j("#comment").val(),
about: $j("#about").val(),
consultant: $j("#consultant").val(),
json: 1,
gcr: $j("#g-recaptcha-response").val()
},
success: function (result, xhr) {
try {
var result = jQuery.parseJSON(result);
} catch (err) {
// fail silently as result was not JSON, so could be success
}
if(typeof result =='object') {
if (result.error) {
alert(result.error);
}
} else {
// assume a success as not capctha error
// deal with any other form errors here.
}
},
error: function (xhr, err) {
alert(err);
}
});
event.preventDefault();
return false;

Note the inclusion of two extra variables in the POST:

json: 1,
gcr: $j("#g-recaptcha-response").val()


Product Review Captcha
-------------------------
Expand All @@ -164,7 +215,13 @@ Some possibilities:
* You are using a custom theme package, and the reCaptcha layout directive file is not loaded.
* You are using a custom theme and the fallback to the base theme is not picking up the layout file.

To fix this, simply copy the file `app/design/frontend/base/default/layout/proxiblue_recaptcha.xml` to your package or theme folder, which will be located something like such: `app/design/frontend/<PACKAGE_NAME>/<THEME NAME>/layout/proxiblue_recaptcha.xml`
To fix this, simply copy the file

app/design/frontend/base/default/layout/proxiblue_recaptcha.xml

to your package or theme folder, which will be located something like such:

app/design/frontend/<PACKAGE_NAME>/<THEME NAME>/layout/proxiblue_recaptcha.xml

* You have directives in your custom theme that changes how the review and customer screen layouts are built.

Expand Down Expand Up @@ -201,13 +258,7 @@ copy the entire section from the reCaptcha layout over into that section.
If, you also have the following: ```<reference name="product.review.form">``` then only copy the BLOCK definition part into that reference.





Our Premium extensions:
----------------------
[Magento Free Gift Promotions](http://www.proxiblue.com.au/magento-gift-promotions.html "Magento Free Gift Promotions")
The ultimate magento gift promotions module - clean code, and it just works!

[Magento Dynamic Category Products](http://www.proxiblue.com.au/magento-dynamic-category-products.html "Magento Dynamic Category Products")
Automate Category Product associations - assign any product to a category, using various rules.
164 changes: 83 additions & 81 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Config/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,89 +7,91 @@
* @package ProxiBlue_reCaptcha
* @author Lucas van Staden ([email protected])
*/
class ProxiBlue_ReCaptcha_Model_Config_Lang {
class ProxiBlue_ReCaptcha_Model_Config_Lang
{

private $lang = array(
array('value' => 'en', 'label' => 'English (US)'),
array('value' => 'en-GB', 'label' => 'English (US)'),
array('value' => 'fr', 'label' => 'French'),
array('value' => 'fr-CA', 'label' => 'French (Canadian)'),
array('value' => 'de', 'label' => 'German'),
array('value' => 'de-AT', 'label' => 'German (Austria)'),
array('value' => 'de-CH', 'label' => 'German (Switzerland)'),
array('value' => 'nl', 'label' => 'Dutch'),
array('value' => 'pt', 'label' => 'Portuguese'),
array('value' => 'pt-BR', 'label' => 'Portuguese (Brazil)'),
array('value' => 'pt-PT', 'label' => 'Portuguese (Portugal)'),
array('value' => 'ru', 'label' => 'Russian'),
array('value' => 'es', 'label' => 'Spanish'),
array('value' => 'es-419', 'label' => 'Spanish (Latin America)'),
array('value' => 'tr', 'label' => 'Turkish'),
array('value' => 'ar', 'label' => 'Arabic'),
array('value' => 'af', 'label' => 'Afrikaans'),
array('value' => 'am', 'label' => 'Amharic'),
array('value' => 'hy', 'label' => 'Armenian'),
array('value' => 'az', 'label' => 'Azerbaijani'),
array('value' => 'eu', 'label' => 'Basque'),
array('value' => 'bn', 'label' => 'Bengali'),
array('value' => 'bg', 'label' => 'Bulgarian'),
array('value' => 'ca', 'label' => 'Catalan'),
array('value' => 'zh-HK', 'label' => 'Chinese (Hong Kong)'),
array('value' => 'zh-CN', 'label' => 'Chinese (Simplified)'),
array('value' => 'zh-TW', 'label' => 'Chinese (Traditional)'),
array('value' => 'hr', 'label' => 'Croatian'),
array('value' => 'cs', 'label' => 'Czech'),
array('value' => 'da', 'label' => 'Danish'),
array('value' => 'et', 'label' => 'Estonian'),
array('value' => 'fil', 'label' => 'Filipino'),
array('value' => 'fi', 'label' => 'Finnish'),
array('value' => 'cs', 'label' => 'Czech'),
array('value' => 'da', 'label' => 'Danish'),
array('value' => 'et', 'label' => 'Estonian'),
array('value' => 'fil', 'label' => 'Filipino'),
array('value' => 'fi', 'label' => 'Finnish'),
array('value' => 'it', 'label' => 'Italian'),
array('value' => 'gl', 'label' => 'Galician'),
array('value' => 'ka', 'label' => 'Georgian'),
array('value' => 'el', 'label' => 'Greek'),
array('value' => 'gu', 'label' => 'Gujarati'),
array('value' => 'iw', 'label' => 'Hebrew'),
array('value' => 'hi', 'label' => 'Hindi'),
array('value' => 'hu', 'label' => 'Hungarain'),
array('value' => 'is', 'label' => 'Icelandic'),
array('value' => 'id', 'label' => 'Indonesian'),
array('value' => 'ja', 'label' => 'Japanese'),
array('value' => 'kn', 'label' => 'Kannada'),
array('value' => 'ko', 'label' => 'Korean'),
array('value' => 'lo', 'label' => 'Laothian'),
array('value' => 'lv', 'label' => 'Latvian'),
array('value' => 'lt', 'label' => 'Lithuanian'),
array('value' => 'ms', 'label' => 'Malay'),
array('value' => 'ml', 'label' => 'Malayalam'),
array('value' => 'mr', 'label' => 'Marathi'),
array('value' => 'mn', 'label' => 'Mongolian'),
array('value' => 'no', 'label' => 'Norwegian'),
array('value' => 'fa', 'label' => 'Persian'),
array('value' => 'pl', 'label' => 'Polish'),
array('value' => 'ro', 'label' => 'Romanian'),
array('value' => 'sr', 'label' => 'Serbian'),
array('value' => 'si', 'label' => 'Sinhalese'),
array('value' => 'sk', 'label' => 'Slovak'),
array('value' => 'sl', 'label' => 'Slovenian'),
array('value' => 'sw', 'label' => 'Swahili'),
array('value' => 'sv', 'label' => 'Swedish'),
array('value' => 'ta', 'label' => 'Tamil'),
array('value' => 'te', 'label' => 'Telugu'),
array('value' => 'th', 'label' => 'Thai'),
array('value' => 'uk', 'label' => 'Ukrainian'),
array('value' => 'ur', 'label' => 'Urdu'),
array('value' => 'vi', 'label' => 'Vietnamese'),
array('value' => 'zu', 'label' => 'Zulu'),
);
private $lang = array(
array('value' => 'en', 'label' => 'English (US)'),
array('value' => 'en-GB', 'label' => 'English (US)'),
array('value' => 'fr', 'label' => 'French'),
array('value' => 'fr-CA', 'label' => 'French (Canadian)'),
array('value' => 'de', 'label' => 'German'),
array('value' => 'de-AT', 'label' => 'German (Austria)'),
array('value' => 'de-CH', 'label' => 'German (Switzerland)'),
array('value' => 'nl', 'label' => 'Dutch'),
array('value' => 'pt', 'label' => 'Portuguese'),
array('value' => 'pt-BR', 'label' => 'Portuguese (Brazil)'),
array('value' => 'pt-PT', 'label' => 'Portuguese (Portugal)'),
array('value' => 'ru', 'label' => 'Russian'),
array('value' => 'es', 'label' => 'Spanish'),
array('value' => 'es-419', 'label' => 'Spanish (Latin America)'),
array('value' => 'tr', 'label' => 'Turkish'),
array('value' => 'ar', 'label' => 'Arabic'),
array('value' => 'af', 'label' => 'Afrikaans'),
array('value' => 'am', 'label' => 'Amharic'),
array('value' => 'hy', 'label' => 'Armenian'),
array('value' => 'az', 'label' => 'Azerbaijani'),
array('value' => 'eu', 'label' => 'Basque'),
array('value' => 'bn', 'label' => 'Bengali'),
array('value' => 'bg', 'label' => 'Bulgarian'),
array('value' => 'ca', 'label' => 'Catalan'),
array('value' => 'zh-HK', 'label' => 'Chinese (Hong Kong)'),
array('value' => 'zh-CN', 'label' => 'Chinese (Simplified)'),
array('value' => 'zh-TW', 'label' => 'Chinese (Traditional)'),
array('value' => 'hr', 'label' => 'Croatian'),
array('value' => 'cs', 'label' => 'Czech'),
array('value' => 'da', 'label' => 'Danish'),
array('value' => 'et', 'label' => 'Estonian'),
array('value' => 'fil', 'label' => 'Filipino'),
array('value' => 'fi', 'label' => 'Finnish'),
array('value' => 'cs', 'label' => 'Czech'),
array('value' => 'da', 'label' => 'Danish'),
array('value' => 'et', 'label' => 'Estonian'),
array('value' => 'fil', 'label' => 'Filipino'),
array('value' => 'fi', 'label' => 'Finnish'),
array('value' => 'it', 'label' => 'Italian'),
array('value' => 'gl', 'label' => 'Galician'),
array('value' => 'ka', 'label' => 'Georgian'),
array('value' => 'el', 'label' => 'Greek'),
array('value' => 'gu', 'label' => 'Gujarati'),
array('value' => 'iw', 'label' => 'Hebrew'),
array('value' => 'hi', 'label' => 'Hindi'),
array('value' => 'hu', 'label' => 'Hungarain'),
array('value' => 'is', 'label' => 'Icelandic'),
array('value' => 'id', 'label' => 'Indonesian'),
array('value' => 'ja', 'label' => 'Japanese'),
array('value' => 'kn', 'label' => 'Kannada'),
array('value' => 'ko', 'label' => 'Korean'),
array('value' => 'lo', 'label' => 'Laothian'),
array('value' => 'lv', 'label' => 'Latvian'),
array('value' => 'lt', 'label' => 'Lithuanian'),
array('value' => 'ms', 'label' => 'Malay'),
array('value' => 'ml', 'label' => 'Malayalam'),
array('value' => 'mr', 'label' => 'Marathi'),
array('value' => 'mn', 'label' => 'Mongolian'),
array('value' => 'no', 'label' => 'Norwegian'),
array('value' => 'fa', 'label' => 'Persian'),
array('value' => 'pl', 'label' => 'Polish'),
array('value' => 'ro', 'label' => 'Romanian'),
array('value' => 'sr', 'label' => 'Serbian'),
array('value' => 'si', 'label' => 'Sinhalese'),
array('value' => 'sk', 'label' => 'Slovak'),
array('value' => 'sl', 'label' => 'Slovenian'),
array('value' => 'sw', 'label' => 'Swahili'),
array('value' => 'sv', 'label' => 'Swedish'),
array('value' => 'ta', 'label' => 'Tamil'),
array('value' => 'te', 'label' => 'Telugu'),
array('value' => 'th', 'label' => 'Thai'),
array('value' => 'uk', 'label' => 'Ukrainian'),
array('value' => 'ur', 'label' => 'Urdu'),
array('value' => 'vi', 'label' => 'Vietnamese'),
array('value' => 'zu', 'label' => 'Zulu'),
);


public function toOptionArray() {
return $this->lang;
}
public function toOptionArray()
{
return $this->lang;
}

}
10 changes: 8 additions & 2 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ public function checkContact($observer)
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$request = $controller->getRequest();
$isAjax = $request->getParam('json');
// insert form data to session, allowing to re-populate the contact us form
$data = $controller->getRequest()->getPost();
Mage::getSingleton('customer/session')->setFormData($data);
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setRedirect(Mage::getUrl('*/*/'));
if($isAjax) {
$controller->getResponse()->setBody(Zend_Json::encode(array('error'=>Mage::helper('captcha')->__('Incorrect CAPTCHA.'))));
} else {
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->getResponse()->setRedirect(Mage::getUrl('*/*/'));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function isCorrect($word)
$request = Mage::app()->getRequest();
$this->generate();
// is this the new 'I am not a robot'?
if($request->getParam('gcr')) {
$request->setParam('g-recaptcha-response', $request->getParam('gcr'));
}
if ($response = $request->getParam('g-recaptcha-response')) {
$path = ProxiBlue_ReCaptcha_Helper_Data::RECAPTCHA_SITEVERIFY_PATH;
$params = array('secret' => $this->_private_key,
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/ProxiBlue/ReCaptcha/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<ProxiBlue_ReCaptcha>
<version>1.2.9</version>
<version>1.3.0</version>
<depends>
<Mage_Captcha/>
</depends>
Expand Down

0 comments on commit 6efca91

Please sign in to comment.