2017-02-21 7 views
2

Я в основном хочу выполнить проверку на стороне сервера, а затем отправить данные формы в URL-адрес. Мой Google Foo не хватает, поскольку я не нашел реальных примеров этого.Форма recaptcha ColdFusion и проверка сервера

Вот что у меня есть до сих пор, но, возможно, я ошибаюсь.

<!doctype html> 
<html lang="en"> 
<head> 
    <title>Untitled</title> 
<script src="https://www.google.com/recaptcha/api.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    var noRobot = function(response){ 
      document.getElementById("sub").disabled = false; 
    } 
</script> 
</head> 
<body> 
    <!-- Form Post --> 
    <CFIF CGI.Request_method IS "post"> 
     <!-- Sends the recaptcha token to Google to verify --> 
     <cfinvoke component="/components/recaptcha" method="isRecaptchaGood" returnvariable="isGood"> 
     <!-- Only post data if valid recapthca --> 
     <CFIF isGood> 
      <p>Good Recaptcha</p> 
      <!--- 
       How do I foward on all the form data to the API? 
       // <cfhttpparam type="CGI" value="cgivar " name="mycgi"> 
       // <cfhttp method="Post" url="http://myapi.com/api/users"> 
      ---> 
      <CFEXIT> 
     <CFELSE> 
      <p>Bad Recaptcha</p> 
     </CFIF> 
    </CFIF> 
    <CFOUTPUT> 
     <!--- 
      I used to just post the data to the API, but now I first need to validate the recaptcha response on the server 
      <form action="http://myapi.com/api/users" method="post"> 
     ---> 
     <form action="#CGI.Script_Name#" method="post"> 
      Name: <input type="text" name = "user" /> 
     <CFIF Application.recaptcha.enabled> 
      <!--- A simple wrapper around generating the recaptcha div with the appropriate key ---> 
      <cfinvoke component="/components/recaptcha" method="makeRecaptcha"> 
     </CFIF> 
     <button type="submit" id="sub" name="con" disabled="disabled">Continue</button> 
     </form> 
    </CFOUTPUT> 
</body> 
</html> 

Как отказ от ответственности, у меня есть около 6 часов опыта ColdFusion, поэтому я предполагаю, что мне не хватает чего-то простого.

Форма редактировать значения:

<body> 
    <cfparam name="form.user" type="string" default=""> 
    <cfparam name="form.amount" type="string" default=""> 
    <form action="#CGI.Script_Name#" method="post"> 
     User: <input type="text" name="user" value="#form.user#" /> 
     Amount: <input type="number" name="amount" value="#form.amount#" /> 
    </form> 
</body> 
+0

Что на самом деле происходит, когда вы запускаете этот код? –

+0

Я завершаю recaptcha, отправлю форму, и она переходит в '' как и ожидалось. Но тогда мне нужно повторно опубликовать все значения формы 'form.user' в этом случае в API. В реальном приложении явно больше, чем просто значение одной формы. – MisterIsaak

+1

Похоже, вы определили соответствующие теги. У вас их в неправильном порядке, и они прокомментированы, но вы, несомненно, оказались на правильном пути. Чтобы передать значения формы в параметры http, просто поместите 'value = '# form.user #" 'в тэг cfhttpparam. –

ответ

1

Благодаря помощи @ DAN-bracuk и @Leigh, вот рабочий конечный результат, который может помочь кому-то еще по дороге:

<!doctype html> 
<html lang="en"> 
<head> 
    <title>Untitled</title> 
<script src="https://www.google.com/recaptcha/api.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    <!--- The callback function recaptcha calls when the user has completed the step---> 
    <!--- Enable the Submit button---> 
    var recaptchaResponse = function(response){ 
     document.getElementById("submit").disabled = false; 
    } 
</script> 
</head> 
<body> 
    <!--- Declare default values for the form so if it fails validation the form can be redisplayed ---> 
    <cfparam name="form.user" type="string" default=""> 
    <cfparam name="form.amount" type="string" default=""> 
    <!--- On Form Post ---> 
    <CFIF CGI.Request_method IS "post"> 
     <!--- Sends the recaptcha token to Google to verify ---> 
     <cfinvoke component="components/recaptcha" method="isRecaptchaGood" returnvariable="isValid"> 
     <!--- Only post data if valid recapthca ---> 
     <CFIF isValid> 
      <p>Good Recaptcha</p> 
      <!--- Post the to API ---> 
      <cfhttp method="Post" url="http://localhost:1249/api/users"> 
       <!--- Send Form field values except for the recaptcha response ---> 
       <cfloop list="#Form.FieldNames#" item="i"> 
        <CFIF form[i] eq "G-RECAPTCHA-RESPONSE"> 
         <cfcontinue /> 
        <CFELSE> 
         <cfhttpparam type="formfield" name="#i#" value="#form[i]#"> 
        </CFIF> 
       </cfloop> 
      </cfhttp> 
      <cflocation url="next.cfm"> 
      <CFEXIT> 
     <CFELSE> 
      <!--- Recaptcha verification failed, redisplay the Form ---> 
      <p>Bad Recaptcha</p> 
     </CFIF> 
    </CFIF> 
    <CFOUTPUT> 
     <!--- Post the Form back to itself first for server side validation ---> 
     <form action="#CGI.Script_Name#" method="post"> 
      User: <input type="text" name="user" value="#form.user#" /> 
      Amount: <input type="number" name="amount" value="#form.amount#" /> 
      <CFIF Application.recaptcha.enabled> 
       <!--- A simple wrapper around generating the recaptcha div with the appropriate key ---> 
       <cfinvoke component="components/recaptcha" method="makeRecaptcha"> 
      </CFIF> 
      <!--- Initially disable the continue button until the user completes the Recaptcha ---> 
      <button type="submit" id="submit" disabled="disabled">Continue</button> 
     </form> 
    </CFOUTPUT> 
</body> 
</html> 
Смежные вопросы