I have a form with a verification through Google reCaptcha. But now I have to click the submit button twice. I can't find the reason.
My form is build like this (Smarty):
<form method="post" id="form">
<input type="hidden" name="action" value="misdaden">
<table>
{foreach from=$misdaden item=item}
<tr>
<td class="coll">{$item['id']}</td>
<td class="coll">{$item['name']}</td>
<td class="coll">{$item['minmoney']}</td>
<td class="coll">{$item['maxmoney']}</td>
<td class="coll">{$item['difficulty']}</td>
<td class="coll">{$item['percentage']}%</td>
<td class="coll"><input type="radio" style="width:20px;" name="value" value={$item['id']} /></td>
</tr>
{/foreach}
</table>
<input type="submit" value="Misdaad plegen!" name="submit" class="submit" />
<!--input class="button good large" name="submit" type="submit" value="Pleeg misdaad!"-->
</form>
My script is (JQuery):
$('#form').submit(function(e) {
e.preventDefault();
grecaptcha.ready(function() {
grecaptcha.execute('6LesiMAUAAAAAAJ8G94kKbXxJj62_U2ajn-dzJzF', {action:'misdaden'})
.then(function(token) {
$('#form').prepend('<input type="hidden" name="token" value="'+ token +'">');
$('#form').unbind('submit').submit();
});
});
});
I'm probably missing something obvious, but what?
In the jquery script, you are unbinding the submit event handler for form on execution of captcha which is not required.
Just submit the form, see below
$(document).ready(function() {
grecaptcha.ready(function() {
grecaptcha.execute('6LesiMAUAAAAAAJ8G94kKbXxJj62_U2ajn-dzJzF', {action:'misdaden'})
.then(function(token) {
$('#form').prepend('<input type="hidden" name="token" value="'+ token +'">');
$('#form').submit();
});
});
});
Related
Greetings fellow programmers, I've been trying to solve this all day but I dont know much with ajax. I can handle regular forms that require refresh.
Basically I have a form where the client enters their desired quantity and a button that says add to cart with an ajax function that handles the post request.
<form id="qnt'.$ID'">
<input type="hidden" value="'.$price.'" name="price">
<input type="text" placeholder="Enter Quantity in Kilo" name="qty" required>
<button class="btn btn-danger" type="button" onclick= add('.$ID.') class="filled-button" class="add2cart">Add To Cart</button></h6>
</form>
This is the ajax request:
<script type="text/javascript">
function add(id){
qnt = $('#qnt'+id).serialize();
$('#qnt'+id).trigger("reset");
$.ajax({
url:'add2cart.php',
method:'POST',
data:{
'id': id,
'qnt': qnt
},
success:function(data){
console.log(data);
}
});
}
</script>
Now this works completely fine(Tbh a friend helped me do it) , Now I reached a situation where I want to add another variable to the form:
<form id="qnt'.$ID'">
<input type="hidden" value="'.$newPrice.'" name="newPrice">
<input type="text" placeholder="Enter Quantity in Kilo" name="qty" required>
<button class="btn btn-danger" type="button" onclick= add('.$ID.') class="filled-button" class="add2cart">Add To Cart</button></h6>
</form>
How can I pass that new hidden input with the ajax script? Thanks.
You can place the id in a hidden input in the form, then the serialize will have the id in it
<form id="qnt'.$ID'">
<input type="hidden" value="'.$ID.'" name="id">
<input type="hidden" value="'.$newPrice.'" name="newPrice">
<input type="text" placeholder="Enter Quantity in Kilo" name="qty" required>
<button class="btn btn-danger" type="button" onclick= add('.$ID.') class="filled-button" class="add2cart">Add To Cart</button></h6>
</form>
function add(id){
qnt = $('#qnt'+id).serialize();
$('#qnt'+id).trigger("reset");
$.ajax({
url:'add2cart.php',
method:'POST',
data: qnt,
success:function(data){
console.log(data);
}
});
}
I want to execute a script if checkbox is checked when I click on the submit button.
I got the following code snippet in my overview.php:
<table style="width: 100%; font-size: 11;">
<tr>
<td colspan="6">
<form method="post" action="update.php" target="_blank">
<script language="JavaScript">
function toggle(source) {
checkboxes = document.querySelectorAll("input[name^='box[']");
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<input type="submit" name="update" value="Update - LoL / BoL">
<input type="checkbox" onClick="toggle(this)" name="All" value="All"> Select all
</form>
</td>
</tr>
<tr>
<th>ve067</th>
<th>ve378</th>
<th>ve724</th>
<th>ve725</th>
<th>ve726</th>
<th>ve727</th>
</tr>
<tr>
<td>
<form method="post" action="update.php">
<input type="checkbox" name="box[1]" value="ve067_LB1"> LeagueBot 1<br>
<input type="checkbox" name="box[2]" value="ve067_LB2"> LeagueBot 2<br>
<input type="checkbox" name="box[3]" value="ve067_LB3"> LeagueBot 3<br>
<input type="checkbox" name="box[4]" value="ve067_LB4"> LeagueBot 4<br>
</form>
</td>
</tr>
</table>
And this is a code snippet from my update.php:
<?php
if(isset($_POST['update'] && $_POST['box[4]']) {
// code here
}
?>
But it does not work for me.
You can do it as I have mentioned in a post on pastebin
Hope it helps you dude.
Solution: Consider the following JSFiddle
Explanation: This will validate to see that at least checkbox 'box[4]' has been ticked.
The "key" code here being the return function in the following line.
<input type="submit" name="update" value="Update - LoL / BoL" onclick="return validate();">
Update: Made to accommodate extra information given in comment.
Remember: The validation here is done on the client side so that it doesn't need to query the server (PHP) first
I am submitting one of my forms in the following way where I needed to do some processing before it get submitted. So I can't use
Submit type button. When I changed it to Button type and submitted the form using jQuery.
My form, button set and jQuery looks like:
<form id="materialreceiveform" action="<?php echo base_url();?>index.php/Materialreceive_control/handleForm" method="post">
<td>
<input id="savebutton" type="button" value="Save" class="btn" name="btnsave" />
</td>
<td>
<input type="submit" value="Update" class="btn" name="btnupdate" />
</td>
<td><a id="reprint" target="_blank" class="btn" href="">Re-print</a>
</td>
<td>
<input type="submit" value="Clear All" class="btn" name="btnclear" />
</td>
<script>
$( "#savebutton" ).click(function() {
var myDate = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
$('[id$=hiddentime]').val(myDate);
$( "#materialreceiveform" ).submit();
});
</script>
In my PHP controller I caught this submission by using an isset before where the button type was in Submit. But now after making it Button and submitting through jQuery I cant capture the submission by isset value as follows.
public function handleForm(){
if(isset($_POST['btnsave'])){
//code goes here
}
}
Please help me to find a solution for this.
I am using $_SESSION to dynamically create forms for my web store. These forms hold the custom info for the product that the customer wants. This is the layout:
Page1
Customer fills out form that looks something like this:
<form action="page2" method="post">
<input type="text" name="size">
<input type="text" name="color">
<input type="submit" name="submit" value="Review Order">
</form>
Page2
Customer reviews order details and has the option of adding more products. Customer goes back to page1 to order another one. All of the customer's orders will show on page2 in their respective form.
Looks like this:
Size: 1
Color: blue
Click Here To Checkout
Size: 2
Color:green
Click Here To Checkout
Size:3
color:red
Click Here To Checkout
What I want is one button that will add ALL orders to the PayPal cart. Sure they can add every order individually by clicking on Click Here To Checkout, but then they will have to go through a big loop to add multiple products.
I want the customer to be able to add as many products as possible and then click one button that adds all of the orders to the shopping cart.
This is what I tried but it obviously didn't work:
<script>
$(document).ready(function(){
$('#clickAll').on('click', function() {
$('input[type="submit"]').trigger('click');
});
});
</script>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<button id="clickAll">Submit All</button>
Here is the php script that generates the dynamic forms using $_SESSION:
<?php
if(isset($_POST['submit'])) :
$test = array(
'size' => $_POST['size'],
'color' => $_POST['color'],
'submit' => $_POST['submit']
);
$_SESSION['testing'][] = $test;
endif;
if(isset($_SESSION['testing'])) :
foreach($_SESSION['testing'] as $sav) {
?>
<form action="paypal.com/..." method="post">
<input type="text" name="size" value="<?php echo $sav['size']; ?>">
<input type="text" name="color" value="<?php echo $sav['color']; ?>">
<input type="submit" name="submit" value="Click Here to Checkout">
</form>
<?php } endif; ?>
So the question is, how can I submit all of the forms with ONE button?
Have you tried to do it with $.ajax? You can add an foreach, or call another form on the Onsucces function. Another approach is changing all to one form with an array that points to the right "abstract" form:
<form action="" method="post">
<input type="text" name="name[]">
<input type="text" name="example[]">
<input type="text" name="name[]">
<input type="text" name="example[]">
<input type="text" name="name[]">
<input type="text" name="example[]">
<button id="clickAll">Submit All</button>
</form>
And in php:
foreach ($_POST['name'] as $key => $value) {
$_POST['name'][$key]; // make something with it
$_POST['example'][$key]; // it will get the same index $key
}
Here is a working jsFiddle: http://jsfiddle.net/SqF6Z/3/
Basically, add a class to each form and trigger() a submit on that class. Like so:
HTML (example only):
<form action="http://www.google.com" method="get" class="myForms" id="1stform">
<input type="text" value="1st Form" name="q1" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="2ndform">
<input type="text" value="2nd Form" name="q2" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="3rdform">
<input type="text" value="3rd Form" name="q3" />
</form>
<input type="button" id="clickMe" value="Submit ALL" />
jQuery:
$('.myForms').submit(function () {
console.log("");
return true;
})
$("#clickMe").click(function () {
$(".myForms").trigger('submit'); // should show 3 alerts (one for each form submission)
});
FWIW, I do this by creating an iframe, making that the target for the second form then submit both like this
//create the iframe
$('<iframe id="phantom" name="phantom">').appendTo('#yourContainer');
and create the dual submit like this:
function dualSubmit() {
document.secondForm.target = 'phantom';
document.secondForm.submit();
document.firstForm.submit();
}
works!
first create loop get all forms id and send them to ajax.
<script name="ajax fonksiyonları" type="text/javascript">
function validate(form){
//get form id
var formID = form.id;
var formDetails = $('#'+formID);
$.ajax({
type: "POST",
url: 'ajax.php',
data: formDetails.serialize(),
success: function (data) {
// log result
console.log(data);
//for closing popup
location.reload();
window.close()
},
error: function(jqXHR, text, error){
// Displaying if there are any errors
console.log(error);
}
});
return false;
}
//this function will create loop for all forms in page
function submitAll(){
for(var i=0, n=document.forms.length; i<n; i++){
validate(document.forms[i]);
}
}
create button for submit in order
<a class="btn" id="btn" onclick="submitAll();" href="">Save & Close</a>
then stop ajax call after success.also dont forget to log to console.
this code works in popup and closing popup after all ajax completed.
i have input type and button, i need when insert into
<input type="text" name="bills_ID" id="bills_ID" value="2">
i will get id for this item and put it into here
BillsPrint.php?bills_ID=id
this is full code
<form id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue" onclick="window.open('BillsPrint.php?bills_ID='this.id, '_blank')" />
</form>
how can i put the id on this link BillsPrint.php?bills_ID=2 without refresh page
You could do this:
<form id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue" />
</form>
<script>
$(function(){
$('#print').click(function(){
window.open('BillsPrint.php?bills_ID='+$('#bills_ID').val(), '_blank')
});
});
</script>
<form id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue"
onclick="window.open('BillsPrint.php?bills_ID=' + $("#bills_ID").val(), '_blank')" />
</form>
Remove the onclick etc. And use get method. Use this simple code and the variables will come in URL as query string.
<form action="BillsPrint.php" method="get" id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue">
</form>
You forget to specify action of form. Use this code it will work. :)
Hope it will help.
Try this:
$(document).ready(function(){
$('form').submit(function(){
var id = $('input[name="bills_ID"]').val();
window.open('BillsPrint.php?bills_ID='+id, '_blank');
return false;
});
});
$("#print").click(function(){
var id = $("#bills_ID").val();
$.ajax({
url: 'BillsPrint.php?bills_ID=' + id,
success: function(data) {
$('.result').html(data);
alert(data); //alerts output from the BillsPrint.php page
}
});
});