|
| 1 | +--- |
| 2 | +layout: socket_example |
| 3 | +title: Custom Enquiry Form Example |
| 4 | +permalink: /examples/custom-enquiry-form |
| 5 | +description: An example of a custom form used instead of Socket Frontend to submit to Socket Server |
| 6 | +--- |
| 7 | +{% highlight html %} |
| 8 | +<body> |
| 9 | + |
| 10 | + ... |
| 11 | + |
| 12 | + <div id="enquiry-form"> |
| 13 | + <form action="#" method="POST"> |
| 14 | + <input type="text" id="client_name" name="client_name" placeholder="Name (Required)" required="required" maxlength="255"> |
| 15 | + <input type="text" id="service_recipient_name" name="service_recipient_name" placeholder="Student name" maxlength="255"> |
| 16 | + <input type="email" id="client_email" name="client_email" placeholder="Email" maxlength="255"> |
| 17 | + <input type="text" id="client_phone" name="client_phone" placeholder="Phone number" maxlength="255"> |
| 18 | + <textarea id="attributes-tell-us-about-yourself" attrtype="true" name="attributes-tell-us-about-yourself" placeholder="Tell us about your needs" maxlength="2047" rows="5"></textarea> |
| 19 | + <div class="text-center"> |
| 20 | + <div class="g-recaptcha" data-sitekey="6LdyXRgUAAAAADUNhMVKJDXiRr6DUN8TGOgllqbt"></div> |
| 21 | + </div> |
| 22 | + <button type="submit">Submit</button> |
| 23 | + </form> |
| 24 | + </div> |
| 25 | + |
| 26 | + ... |
| 27 | + |
| 28 | + <script src="https://www.google.com/recaptcha/api.js" async defer></script> |
| 29 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
| 30 | + <script> |
| 31 | + $('form').submit(function(e) { |
| 32 | + e.preventDefault() |
| 33 | + const v = grecaptcha.getResponse() |
| 34 | + if (v.length === 0) { |
| 35 | + $('#captcha').html("You can't leave Captcha Code empty") |
| 36 | + return false |
| 37 | + } |
| 38 | + const data = { |
| 39 | + client_name: $('#client_name').val(), |
| 40 | + client_email: $('#client_email').val(), |
| 41 | + client_phone: $('#client_phone').val(), |
| 42 | + service_recipient_name: $('#service_recipient_name').val(), |
| 43 | + grecaptcha_response : v, |
| 44 | + // Make sure any extra attribute fields are in a separate object. |
| 45 | + attributes: { |
| 46 | + 'attributes-tell-us-about-yourself': $('#attributes-tell-us-about-yourself').val() |
| 47 | + } |
| 48 | + } |
| 49 | + $.ajax({ |
| 50 | + type: 'POST', |
| 51 | + url: 'https://socket.tutorcruncher.com/{{ site.socket_key }}/enquiry', |
| 52 | + data: JSON.stringify(data), |
| 53 | + dataType: 'json' |
| 54 | + }).done(function() { |
| 55 | + $('form').html('Form successfully submitted!') |
| 56 | + }) |
| 57 | + }) |
| 58 | + </script> |
| 59 | +</body> |
| 60 | +{% endhighlight %} |
| 61 | + |
| 62 | +<style> |
| 63 | + input[type=text], input[type=email], textarea { |
| 64 | + width: 100%; |
| 65 | + padding: 12px 20px; |
| 66 | + margin: 8px 0; |
| 67 | + display: inline-block; |
| 68 | + border: 1px solid #ccc; |
| 69 | + border-radius: 4px; |
| 70 | + box-sizing: border-box; |
| 71 | + } |
| 72 | + |
| 73 | + button { |
| 74 | + width: 100%; |
| 75 | + background-color: #4CAF50; |
| 76 | + color: white; |
| 77 | + padding: 14px 20px; |
| 78 | + margin: 8px 0; |
| 79 | + border: none; |
| 80 | + border-radius: 4px; |
| 81 | + cursor: pointer; |
| 82 | + } |
| 83 | + |
| 84 | + button:hover { |
| 85 | + background-color: #45a049; |
| 86 | + } |
| 87 | + |
| 88 | + div#enquiry-form { |
| 89 | + border-radius: 5px; |
| 90 | + background-color: #f2f2f2; |
| 91 | + border: 1px solid #ccc; |
| 92 | + padding: 20px; |
| 93 | + width: 50%; |
| 94 | + margin: 0 auto; |
| 95 | + } |
| 96 | + |
| 97 | + .g-recaptcha { |
| 98 | + display: inline-block; |
| 99 | + } |
| 100 | +</style> |
| 101 | +<div id="enquiry-form"> |
| 102 | + <form action="#" method="POST"> |
| 103 | + <input type="text" id="client_name" name="client_name" placeholder="Name (Required)" required="required" maxlength="255"> |
| 104 | + <input type="text" id="service_recipient_name" name="service_recipient_name" placeholder="Student name" maxlength="255"> |
| 105 | + <input type="email" id="client_email" name="client_email" placeholder="Email" maxlength="255"> |
| 106 | + <input type="text" id="client_phone" name="client_phone" placeholder="Phone number" maxlength="255"> |
| 107 | + <textarea id="attributes-tell-us-about-yourself" attrtype="true" name="attributes-tell-us-about-yourself" placeholder="Tell us about your needs" maxlength="2047" rows="5"></textarea> |
| 108 | + <div class="text-center"> |
| 109 | + <div class="g-recaptcha" data-sitekey="6LdyXRgUAAAAADUNhMVKJDXiRr6DUN8TGOgllqbt"></div> |
| 110 | + </div> |
| 111 | + <button type="submit">Submit</button> |
| 112 | + </form> |
| 113 | +</div> |
| 114 | +<script src="https://www.google.com/recaptcha/api.js" async defer></script> |
| 115 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
| 116 | +<script> |
| 117 | + $('form').submit(function(e) { |
| 118 | + e.preventDefault() |
| 119 | + const v = grecaptcha.getResponse() |
| 120 | + if (v.length === 0) { |
| 121 | + $('#captcha').html("You can't leave Captcha Code empty") |
| 122 | + return false |
| 123 | + } |
| 124 | + const data = { |
| 125 | + client_name: $('#client_name').val(), |
| 126 | + client_email: $('#client_email').val(), |
| 127 | + client_phone: $('#client_phone').val(), |
| 128 | + service_recipient_name: $('#service_recipient_name').val(), |
| 129 | + grecaptcha_response : v, |
| 130 | + // Make sure any extra attribute fields are in a separate object. |
| 131 | + attributes: { |
| 132 | + 'attributes-tell-us-about-yourself': $('#attributes-tell-us-about-yourself').val() |
| 133 | + } |
| 134 | + } |
| 135 | + $.post( |
| 136 | + 'https://socket.tutorcruncher.com/{{ site.socket_key }}/enquiry', |
| 137 | + JSON.stringify(data), |
| 138 | + () => $('form').html('Form successfully submitted!'), |
| 139 | + 'json' |
| 140 | + ) |
| 141 | + }) |
| 142 | +</script> |
0 commit comments