reCAPTCHA - textarea g-recaptcha-response doesn't work - javascript

I have the following problem with a page that worked reCAPTCHA v2 and no longer works
For some reason, the $ _POST ['g-recaptcha-response'] arrives empty to the file that validates the data after doing submit in the form
In the user panel of www.google.com/recaptcha/ the web is correctly added, and the keys are verified to be correct.
The point is that when you check the I am not a robot check, the text area g-recaptcha-response should be updated with the code that comes from google, but this code remains blank.
<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response"
style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px;
resize: none; display: none;"></textarea>
And this I think is the reason why it arrives empty
the code of my form is
<form action="votaciones.php" method="post" class="form" role="form" id="login">
<div class="form-group">
<label for="user" class="form-label"><?= _("Usuario") ?> : </label>
<input type="text" id="user" name="user" class="form-control" placeholder="<?= _("Usuario") ?> " required autofocus/>
</div>
<div class="form-group">
<label for="user" class="form-label"><?= _("Password") ?> : </label>
<input type="password" id="pass" name="pass" class="form-control" placeholder="<?= _("Password") ?> " required />
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="<?php echo "$reCAPTCHA_site_key"; ?>"></div>
</div>
<div class="form-group">
<div class="text-center">
<button class="btn btn-start-order" type="submit"><?= _("Acceder") ?> </button>
</div>
</div>
</form>
<script src='https://www.google.com/recaptcha/api.js' ></script>
and one of the PHP to which the data arrives is
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
// Google reCAPTCHA API secret key
$secretKey = $reCAPTCHA_secret_key;
// Verify the reCAPTCHA response
$verifyResponse = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
// Decode json data
$responseKeys = json_decode($verifyResponse, true);
// should return JSON with success as true
if ($responseKeys["success"]) {
//Captcha correcto
$validoCaptcha = true;
} else {
// eres un robot, asi que terminamos
$validoCaptcha = false;
Header("Location: $url_vot/index.php?error_login=10");
exit;
}
}else{
echo "no llego" .$_POST['g-recaptcha-response'];
}
}
Obviously, it prints"no llego" and it does not show the post because it is empty
I have looked at other similar questions and tried the solutions but they do not solve the problem for me
For layout, I am using bootstrap, although I don't think it influences
Can anybody help me? please
I'm stuck for days and I don't understand why it doesn't update that textarea

Related

Send the value of a javascript funcion, on a HTML form, into a PHP page

I've got a JavaScript function, that as an array, and a HTML form that sends data to a PHP page to later send it by email. But I would also need to send the result of the JavaScript function on the form to the php page. Is this possible?
I'm currently trying the following way, but with no results:
HTML page and script :
function showcartitems() {
var showcart = paypal.minicart.cart.items();
}
<form action="teste4.php" method="post" class="creditly-card-form agileinfo_form" onsubmit="showcartitems()">
<section class="creditly-wrapper wthree, w3_agileits_wrapper">
<div class="information-wrapper">
<div class="first-row form-group">
<div class="controls">
<label class="control-label">Nome Completo: </label>
<input class="billing-address-name form-control" type="text" name="name" placeholder="Nome Completo">
<input type="hidden" name="cart" value="showcart" />
</div>
<div class="w3_agileits_card_number_grids">
<div class="w3_agileits_card_number_grid_left">
<div class="controls">
<label class="control-label">Numero de telemóvel:</label>
<input class="form-control" type="text" name="mobile" placeholder="Numero de telemóvel">
</div>
</div>
<div class="w3_agileits_card_number_grid_left">
<div class="controls">
<label class="control-label">Email:</label>
<input class="form-control" type="text" name="email" placeholder="Email">
</div>
</div>
<div class="w3_agileits_card_number_grid_right">
<div class="controls">
<label class="control-label">Rua: </label>
<input class="form-control" type="text" name="rua" placeholder="Rua">
</div>
</div>
<div class="clear"> </div>
</div>
<div class="controls">
<label class="control-label">Cidade: </label>
<input class="form-control" type="text" name="cidade" placeholder="Cidade">
</div>
<div class="controls">
<label class="control-label">Método de pagamento: </label>
<select name="pagamento">
<option>MBWay</option>
<option>Transferência Bancária</option>
<option>Multibanco (apenas para entregas ao domicílio)</option>
</select>
</div>
</div>
<button class="submit check_out">Verificar disponibilidade!</button>
</div>
</section>
</form>
The PHP page is still quite simple, just checking if the variables are coming correctly, they all are, except the value of the javascript function:
echo $_POST['name'];
echo $_POST['mobile'];
echo $_POST['email'];
echo $_POST['rua'];
echo $_POST['cidade'];
echo $_POST['pagamento'];
echo $_POST['showcart'];
?>
Am I doing something wrong? Is there a better way to do this, or is it just impossible?
EDIT 1:
Following #epascarello suggestion, I've managed to get the value into the PHP side, which is returning the following:
[{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010302","item_image":"images/1010302-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":2,"href":"http://teste/index.html#5"},"_options":[],"_discount":100,"_amount":99,"_total":98,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010575","item_image":"images/1010575-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010707","item_image":"images/1010707-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA SCM00514","item_image":"images/scm00514-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}}]
I've tried several approaches with json_decode, but I'm not getting the result I seek.
I would like to split that array into, for example:
item_name=x;
item_image=x;
quantity=x;
Can someone pin point me into the right direction? Sorry for asking this, I'm new in this languages and despite my research I did not manage to get to a solution on my own.
Thank you in advance.
So you have <input type="hidden" name="cart" value="showcart" /> so you need to set the value of the field. That value does not magically reference the JavaScript value you created.
Give the input element an id, so it is easier to reference
<input type="hidden" name="cart" id="cart" />
and set the value of the input in your method that is called on submit
function showcartitems() {
document.getElementById('cart').value = JSON.stringify(paypal.minicart.cart.items());
}
After #epascarello solution, to be able to decode de JSON string, I've followed #RaymondMutyaba suggestion which works pretty properly:
$item_name = json_decode($_POST['cart'], true)[0]["_data"]["item_name"];
$array_of_items_in_cart = json_decode($_POST['cart'], true);
$first_item_in_cart = array_of_items_in_cart[0];
$name_of_first_item_in_cart = first_item_in_cart["_data"]["item_name"];
$first_item_discount = first_item_in_cart["_discount"];

How to set field as required if value exist in other field?

I have first, last name, and email input fields. The requirement from the customer is to have email field required only if either first or last name value exist. If there is no value entered in either of those two fields email should not be required. I have developed this code that works assuming that form is always empty. The problem is the case when user already has data entered in there and user is loading the form. In that case my code is not useful. Here is working example of what I have so far.
$(".check").on("keyup blur", function() {
var fldVal = $(this).val();
if (fldVal) {
$("#email").closest("div").addClass("required");
$("#email").prop("required", true);
} else {
$("#email").closest("div").removeClass("required");
$("#email").prop("required", false);
}
});
.row {padding: 3px;}
.required label:after { content: " *"; color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="text" id="test">
<div class="row">
<label>First Name:</label>
<input type="text" name="first" id="first" class="check">
</div>
<div class="row">
<label>Last Name:</label>
<input type="text" name="last" id="last" class="check">
</div>
<div class="row">
<label>Email:</label>
<input type="email" name="email" id="email">
</div>
<button type="button" name="submit" id="submit">Submit</button>
</form>
I would like for my code to check all the requirements on page load. If possible that should all be done with one function. I can't think of a good way to achieve that. Please let me know if you have any ideas.

Php domain availability checker - Need an ajax code or similar to show result without reloading site

I have a onepage site and I found this code for a domain checker, it works great but the problem is that when I submit the search it goes to the top of the page and I need to scroll back down to see the result. Can anyone help me so when I do the search it stays at the same place? I think ajax will do the job but I have not knowledge about it, and don't even know how to search for what I need.
<div class="container">
<div class="row">
<div class="col-md-6">
<h3>Check Domain Name Availability</h3>
<form action="" method="GET">
<div id="custom-search-input">
<div class="input-group col-md-24" >
<input type="text" name="domain" class="form-control input-lg" placeholder="Example.com or Example.in etc." />
<span class="input-group-btn">
<button type="submit" class="glyphicon glyphicon-search"></button>
</span>
</div>
</div>
</form>
<?php
error_reporting(0);
if(isset($_GET['domain'])){
$domain = $_GET['domain'];
$godaddycheck = 'https://in.godaddy.com/domains/searchresults.aspx?checkAvail=1&tmskey=&domainToCheck='.$domain.'';
$namecomcheck = 'https://www.name.com/domain/search/'.$domain.'';
$registercomcheck = 'http://www.register.com/domain/search/wizard.rcmx?searchDomainName='.$domain.'&searchPath=Default&searchTlds=';
if ( gethostbyname($domain) != $domain ) {
echo "<h1>Already Registered!</h1>";
}
else {
echo "<h3>$domain</h3><h2><br>Not Taken, you can register it.
</h2>";
}
}
?>
</div>
</div>
</div>

redirect function doesnt work properly after submitting the form twice

I did a form validation where server checks submitted data. If data passes validation, then redirect to previous page, if not, a modal box will pop up tells user wrong username/password.
The question is, after user resubmit the form, redierct function doesn't work. It only works when user first time successfully input those fields. Can anyone help me with this please?
The html code:
<div id="formInfo">
<form method="post" action="<?PHP echo $_SERVER['PHP_SELF'];?>">
<span class="title">Sign in to urmajesty.net</span><br><br>
<div id="input_container">
<input type="text" name="username" placeholder="username"><img src="Images/icons/user.png" id="input_img"><br><br>
</div>
<div id="psw_container">
<input type="password" name="psw" placeholder="password"><img src="Images/icons/key.png" id="key_img"><br><br>
</div>
<div id="checkbox">
<input type="checkbox" name="RememberUsername" value="Yes"><span class="checkboxtxt">Remember my username</span>
</div><br><br>
<div id="Forget">
<span class="forget1">Forget Password</span><span class="forget2">Forget Username</span>
</div>
<input type="submit" class="button" value="SIGN IN"><br><br>
<div id="hispan"><p class="hip"><span class="hi">New to urmajesty.net?</span></p></div><br>
<input class='disable_me' name="referer" type="hidden" value="<?php echo urlencode($_SERVER['HTTP_REFERER'])?> " />
<button type="button" class="button" id="btn">CREATE ACCOUNT</button>
</form>
</div>
<div id='modal_box'>
<div class='modal_content'>
<span class='close'>×</span><p>username/password is wrong. Please try again!</p>
</div>
</div>
The php code:
if($_SERVER["REQUEST_METHOD"]=="POST"){
$validation=Login();
if($validation==false){
echo"<script>
var modal_box=document.getElementById('modal_box');
modal_box.style.display='block';
modal_box.style.backgroundColor='rgba(0,0,0,0.4)';
var close=document.getElementsByClassName('close')[0];
close.onclick=function(){
modal_box.style.display='none';
}
window.onclick=function(event){
if(event.target==modal_box){
modal_box.style.display='none';
}
}
</script>";
}
else{
if(!empty($_POST['referer']) && !is_array($_POST['referer'])){
header("Location: ".urldecode($_POST['referer']));
exit();
}
}
}
?>
Use javascript to redirect.
if(!empty($_POST['referer']) && !is_array($_POST['referer'])){ ?>
//header("Location: ".urldecode($_POST['referer']));
<script>
window.location.href = "<?=$_POST['referer']?>";
</script>
<?php
exit();
} ?>
Apart from using header() to redirect, you can use meta refresh method, or JS window.location method.
<script>
window.location = "<?=$_POST['referer']?>"
</script>

Bootstrap Email signup form with Parse backend

[edited] I'm trying to include an email signup box on my website with Parse on the backend.
Problem: The email ids entered aren't getting saved into the Parse database.
here's the HTML with corresponding JS. I'm still learning JS so I might have picked up some code from here and there.
<form id="signup-form" class="align-center">
<div class="form-group align-center" style="width: 90%;">
<input type="text" class="form-control" name="email" id="email" placeholder="get informed when it's online!" style="font-size: 1em; width: 100%; float: left;"/>
<button id="signup-button" class="btn btn-default" style="margin-top: 10px;" >count me in!</button>
</div>
</form>
Javascript:
$(document).ready(function() {
$("#signup-button").click(function() {
Parse.$ = jQuery;
Parse.initialize("n7cfi9v9FErlM13bSC4qb6obHr0c9lSNmEgyBGTB", "NHA6CciUx6xXCvrQwHGTG48D7ggItvUlrYE36mTT");
var SignupList = Parse.Object.extend("signup_list");
var signup = new SignupList();
signup.save({email: $('#email').val()}).then(function(object) {
alert($('#email' + " enrolled in list!").val());
});
});
});
Don't know what I'm doing wrong. Help.

Categories