Unable to Programmatically invoke the invisible recaptcha challenge by ID - invisible-recaptcha

We have a set up with multiple forms on a single page. We are rendering each recaptcha successfully, however I'm struggling to invoke the recaptcha challenge programatically targeted to an ID.
Looking at the docs (https://developers.google.com/recaptcha/docs/invisible#programmatic_execute) my understanding is that I can pass an ID with the execute command so the response is filled into g-response within the correct form, otherwise the response defaults to the first g-response it finds on the page (which is no good for anything other than the first form on the page).
I've tried it with a slightly modified version of Googles own example, however we get the error message 'Invalid site key or not loaded in api.js: recaptcha123' even though the key is correct.
Does anyone have any idea how we might get this working?
<html>
<head>
<script>
function onSubmit(token) {
alert('thanks ' + document.getElementById('field').value);
}
function validate(event) {
event.preventDefault();
if (!document.getElementById('field').value) {
alert("You must add text to the required field");
} else {
grecaptcha.execute('recaptcha123');
}
}
function onload() {
var element = document.getElementById('submit');
element.onclick = validate;
}
</script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form>
Name: (required) <input id="field" name="field">
<div id="recaptcha123" class="g-recaptcha"
data-sitekey="XXXXXXXXXXXXXXXX"
data-callback="onSubmit"
data-size="invisible"></div>
<button id="submit">submit</button>
</form>
<script>onload();</script>
</body>
</html>

The following code works:
<html>
<head>
<title>reCAPTCHA demo: Explicit render after an onload callback</title>
<script>
var onSubmit = function(token) {
console.log('success!');
};
var onloadCallback = function() {
widgetId1 = grecaptcha.render('recaptcha', {
'sitekey' : 'XXXXXXXXXXXXXXXXXX',
'callback' : onSubmit
});
};
</script>
</head>
<body>
<script src="/wp-content/themes/kc_water_care_services/js/pristine.min.js"></script>
<form action="?" method="POST" id="contactForm">
<div class="form-group">
<label for="name">Name (required)</label>
<input type="text" required data-pristine-required-message="Please enter your name"
id="name" name="name" />
</div><!--/.form-group-->
<div id="recaptcha" data-size="invisible"></div>
<input id="submit" type="submit" value="Submit">
</form>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer>
</script>
</body>
</html>
<script>
var form = document.getElementById("contactForm");
// create the pristine instance
var pristine = new Pristine(form);
form.addEventListener('submit', function (event) {
event.preventDefault();
// check if the form is valid
var valid = pristine.validate(); // returns true or false
if(valid == true){
grecaptcha.execute(widgetId1);
}
});
</script>
Turns out the id doesn't refer to the css ID, it refers to an ID created when you use the render function.

Related

one-time payment with Laravel Cashier without subscription

I'm Planning to use Laravel Cashier for one-time payments with Stripe. It says in the documentation:
If you're only performing "one-off" charges and do not offer subscriptions, you should not use Cashier.
However, I was able to charge the user without subscription using the following code:
$amount = $request['amount];
Stripe::setApiKey(Config::get('stripe.secret_key'));
$token = $input['stripeToken'];
$user = Auth::user();
return $user->charge($amount * 100, ['source' => $token]);
And it worked! I'm wondering is there a problem with this approach? Why they suggested to not use Cashier? Is it gonna cause problems along the way? Please let me know what do you think?
As currently implemented, it's safe to do this. You can see the code for the charge method here: https://github.com/laravel/cashier/blob/822b6535e755fd36dec8ecceb52cc8909c8a953e/src/Billable.php#L37
That said, given the explicit warning that Cashier isn't intended for this sort of use, it's your own fault if the charge function gets modified in a way that breaks your app somewhere down the line.
There's not much reason to use Cashier in this situation, either. Using the SDK's Stripe\Charge class directly will be cleaner code, and you don't take on the risk of misusing a different library.
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<script src="https://js.stripe.com/v3/"></script>
<br><br>
<h1 align="center">One Time Charge</h1>
<form method = "post" action = "{{route('charge')}}" id="form">
#csrf
<div class="form form-cb" align="center">
Name on Card: <input type="text" id="card-holder-name" required>
<br> <br>
<div id="card-element" style="width: 300px;">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors. -->
<div id="card-errors" role="alert"></div><br>
<button disabled style="background-color:skyblue;color:black">$1.00 USD</button><br><br>
<button id="card-button" style="background-color: green;color:white;font-size:20px;" />Pay
</button>
</div>
</form>
<script>
var stripe = Stripe('Your Stripe-key');
var elements = stripe.elements();
var cardElement = elements.create('card');
cardElement.mount('#card-element');
// Add an instance of the card UI component into the `card-element` <div>
var cardHolderName = document.getElementById('card-holder-name');
var cardButton = document.getElementById('card-button');
cardButton.addEventListener('click', async (e) => {
var { paymentMethod, error } = await stripe.createPaymentMethod(
'card', cardElement, {
billing_details: { name: cardHolderName.value }
}
);
if (error) {
console.log('error');
} else {
var payment_id = paymentMethod.id;
createPayment(payment_id);
}
});
var form = document.getElementById('form');
form.addEventListener('submit', function(event) {
event.preventDefault();
});
// Submit the form with the token ID.
function createPayment(payment_id) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'payment_id');
hiddenInput.setAttribute('value',payment_id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
</script>
</body>
</html>

Send method in new Vue is not called when form is submitted

<!DOCTYPE html>
<head>
<meta charset=" UTF-8">
<title> Document</title>
</head>
<body id="chat">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.5.0/socket.io.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.min.js"></script>
<form v-on="submit: send">
<input v-model="message">
<button>Send</button>
</form>
<script>
var socket = io();
new Vue({
el: '#chat',
date: {
message: ''
},
methods: {
send: function(e)
{
e.preventDefault();
alert("a");
}
}
})
</script>
</body>
I want to call the send method defined in new Vue when the form is submitted ,
But when i submit the form, page is reloading.
I have created a Vue object and linked it to the chat element.
I guess e.preventDefault() is not working.
Interesting, I just helped somebody with a similar issue, the syntax for Vue.2.0 is v-on:submit="send" not v-on="submit: send". Vue already has a way stop the form submitting which is: v-on:submit.prevent so you don't need the e.preventDefault, you would get:
<form v-on:submit="send" v-on:submit.prevent>
or a shorter version:
<form v-on:submit.prevent="send">
There are a few more issues here, so I will go through them for you:
Firstly, you are never submitting the form. To submit a form you need a submit input, not a button:
<input type="submit" value="Send" />
However, from what I can see it's likely you don't even need a form, and can simply use a button with v-on:click:
<div>
<input v-model="message">
<button v-on:click="send">Send</button>
</div>
And then get what was submitted from the view model:
send: function()
{
alert(this.message);
}
You should also use the console (under developer tools in your browser) and log any output rather than alert (console.log(this.message)), because it will also sniff out any general errors with your code - for example I can see that you also have a typo (the same one I always make) it's data not date:
data: {
message: ''
},
Okay, what about this
<form #submit.prevent="send">
<input v-model="message">
<button>Send</button>
</form>
And then you can remove preventing default browser action from your send() method

ajax form does not submit

I can't get this form to submit via Ajax to a PHP script and cannot figure out why. Please help. I am planning to use this in phone gap after development and was under the impression this ajax format would work better than xmlhttprequest method - is this really the case?
<html>
<head>
<title></title>
</head>
<body>
<h1>Welcome</h1>
<hr>
<form id="idForm" name="idForm">
<input type="text" size="8" id="date" name="date"><br>
<input type="hidden" size="8" id="reversedate" name="reversedate"><br>
<input type="hidden" size="1" id="dow" name="dow"><br>
<input type="submit" id="submitbutton" name="submitbutton">
</form>
<br><br><br>
<br>
<br>
<p id="p1">x</p>
<br>
log out
<script>
$('#submitbutton').click(function(){
$("#idForm").submit(function() {
var url = "fetchsimple.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
return false; // avoid to execute the actual submit of the form.
});
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
</body>
</html>

Dart: AJAX form submit

Please note: I am not interested in using Polymer for this; I want to use "pure" Dart!
I am trying to build a simple sign-in screen for my Dart app, and am having difficulty getting the two form variables (email and password) to POST to the server-side:
Here's my main HTML file (myapp.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sign in</title>
<link rel="stylesheet" href="assets/myapp/myapp/myapp.css">
<link rel="stylesheet" href="assets/myapp/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form id="signinForm" method="POST" class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="input-block-level" name="email" placeholder="Email address">
<input type="password" class="input-block-level" name="password" placeholder="Password">
<button class="btn btn-large btn-primary" id="signinBtn" type="submit">Sign in</button>
</form>
</div>
<script type="application/dart" src="myapp.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
Here's my main Dart file (myapp.dart):
import 'dart:html';
void main() {
querySelector("#signinForm")
..onClick.listen(handle);
}
void handle(MouseEvent mouseEvent) {
mouseEvent.preventDefault();
FormElement formElement = mouseEvent.target as FormElement;
var url = "http://localhost:8080/myapp/signinService";
var request = HttpRequest.request(
url,
method: formElement.method,
sendData: new FormData(formElement)
).then(onDataLoaded);
}
void onDataLoaded(HttpRequest req) {
String response = req.responseText;
if(response == 1)
window.alert("You are signed in!");
else
window.alert("Sign in failed. Check credentials.");
}
When I run this in a browser I see the sign in screen appear, but when I click the signin button nothing happens, and Firebug throws a bunch of errors on the cross-compiled, obfuscated, minified JavaScript:
: CastError: Casting value of type qE to incompatible type Yu
I want this to be an AJAX request so that the user does not have to experience a page reload (the intent here is to be a single page app).
Any ideas as to what's going on here? I'm almost 100% confident the issue isn't on the server-side, so for now I'm omitting to post server code. But I'll update my question with server code if need be.
here is an example how to do this
Forms, HTTP servers, and Polymer with Dart
When you change
void main() {
querySelector("#signinBtn")
..onClick.listen(handle);
}
to
void main() {
querySelector("form")
.onSubmit.listen(handle);
}
you have access to target
void handle(Event event) {
event.preventDefault();
FormElement form = event.target as FormElement;
...
}

jquery ajax is not working

i have two files trying to use ajax
here is code of test.php file
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script type="text/javascript">
var email = $('#email').val();
$(document).ready(function(){
$("#submit").click(function() {
$.ajax({
url:"test1.php",
success:function(){
alert('');
}
});
});
});
</script>
</head>
<body>
<form method="post">
Email:<input type="text" name="email" id="email" />
<input type="submit" name="submit" value="submit" id="submit" />
</form>
</body>
</html>
and here is code of test1.php file
<?php
echo "hi";
exit;
?>
it is not working i am not able to get alert in sucess function
Change the submit button type to just a button. Otherwise, you are submitting the form before the ajax is firing.
Try giving a return false in the end of the click function.
var email = $('#email').val();
$(document).ready(function(){
$("#submit").click(function() {
$.ajax({
url:"",
success:function(){
alert('');
}
});
return false;
});
});​
This might work!
In this case I'd use the submit event on the form because if you bind the click event on the submit button, the submit event still gets triggered.
see the example here
http://api.jquery.com/submit/

Resources