Submit form to websocket - html

I'm trying to submit some simple log in data to a server. All of the requests and functions are working great for "testuser." I want people to be able to submit their username and password for consideration to the server (it all gets encrypted, and I can't see it.)
I can see where the registration request submits generic data "testuser" for username and pass. But I have no idea how to get it to accept user login information instead.
<!--This is where I'm attempting a login form-->
<form action="/action_page.php">
<div class="imgcontainer">
<img src="img_avatar2.png" alt="Avatar" class="avatar">
</div>
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
<!-- This is rock-solid code from the gamesparks API. Works flawlessly. -->
<button onClick='gamesparks.registrationRequest("testuser", "testuser", "testuser", registerResponse)'>Register</button>
<button onClick='gamesparks.authenticationRequest("testuser", "testuser", loginResponse)'>Login</button>
<button onClick='gamesparks.accountDetailsRequest(accountDetailsResponse)'>Account Details</button>
<button onClick='customEvent()'>Custom Event</button>
<button onClick='customEvent2()'>Custom Event 2</button>
<button onClick='testRT()'>Test RT</button>
<i>Special thanks to the awesome team at GameSparks!</i>
<div id="messages"></div>
<br/>
<br/>
All of my buttons work, spitting out data from my backend regarding "testuser." I know it works because I can alter my data and verify.
You'll notice "testuser" occupies three spaces for one of the onClicks. One is username, the other is password, and the third is displayname (in that order.)
Displayname can equal Username...if anyone wants extra credit.
But mostly, I want my modal to input the data and submit it for my buttons.

Again, I solved my own question! What I did was put my gamesparks function inside of another function. I learned this was called "nesting." I also defined variables as part of the onClick, which were then used in the function. The solution I used is below. Forgive my n00bish comments. But they help me learn.
<div class="container">
<h2>Click To Login</h2> <!-- This is a label above the button -->
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Martial Parks</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><img src="http://martialparks.com/wp-content/uploads/2017/10/cropped-mp_logo01-2.png" class="center-block img-circle logo-margin-negative"></h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="form-group col-sm-5 col-sm-offset-4">
<label class="sr-only" for="exampleInputAmount">Username</label>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
<input type="text" class="form-control" id="username" placeholder="Username">
</div>
</div>
<div class="form-group col-sm-5 col-sm-offset-4">
<label class="sr-only" for="exampleInputAmount">Password</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-key"></i></div>
<input type="text" class="form-control" id="password" placeholder="Password">
</div>
</div>
<button type="button" class="btn btn-danger btn-block" data-dismiss="modal" onClick="login()">Login</button>
<!-- There is a line of code that closes the modal once the button is clicked: data-dismiss="modal" I've removed it from the register button. -->
<!-- Also, I can call up multiple functions using this format: onclick="doSomething();doSomethingElse();" -->
<button type="button" class="btn btn-danger btn-block" onClick="register()">Register</button>
<script>
function login() {
var usernameinput = document.getElementById('username').value;
var passwordinput = document.getElementById('password').value;
console.log(usernameinput);
console.log(passwordinput);
gamesparks.authenticationRequest( usernameinput, passwordinput, loginResponse);
}
function register() {
var usernameinput = document.getElementById('username').value;
var passwordinput = document.getElementById('password').value;
console.log(usernameinput);
console.log(passwordinput);
gamesparks.registrationRequest( usernameinput, usernameinput, passwordinput, registerResponse)
}
</script>
</div>
</form>
</div>
<div class="modal-footer">
<div class="row">
<div class="col-sm-5 col-sm-offset-4">
<div id="messages"></div>
<br/>
<br/>

You need to manually grab each of your fields values. The most direct way is via document.getElementById and value:
document.getElementById( "username" ).value
In your example:
<button onClick='gamesparks.authenticationRequest( document.getElementById( "username" ).value, document.getElementById( "password" ).value, loginResponse )'>Register</button>
I would, however, suggest calling a function instead of inlining it:
<button onclick="login()">Login</button>
⋮
⋮ // Before </body>
⋮
<script>
const fetchValue = id => document.getElementById( id ).value;
function login() {
const username = fetchValue( "username" );
const password = fetchValue( "password" );
gamesparks.authenticationRequest( username, password, loginResponse );
}
</script>
Also1: You should never need to place an api secret in client-facing HTML/JavaScript.
Also2: Regarding your these lines:
<var username = username>
<var password = password>
These are invalid. <var> provides styling and nothing else. It looks like you are attempting to place JavaScript there? To do so, you need to either use <script> tags or event handlers, such as onclick.

Related

Use of tags to send information to a function

I'm pretty new to HTML5 and have inherited someone else's code, which turns out not to be working.
I have a site with 8 different categories and 'add to email list' buttons, one for each category.
We use formspree to create the form code and then connect with our marketing systems. Instead of integrating 8 different forms, the previous coder added one from as a function*, then referred to that function on each of the 8 buttons.
Edit:*Its not actually a function, what is below is the only code involved.
To determine which of the categories the form came from, its meant to pass over a tag, but it's that tag that is not working and I would appreciate help on.
<!-- Waitlist modal -->
<div class="modal fade" id="waitlist-modal" tabindex="-1" role="dialog" aria-labelledby="modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title heavy-text" id="modal-label">Request Access</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="contact-form" action="https://formspree.io/email#gmail.com" method="POST" >
<div class="form-group">
<label for="full-name">Full name</label>
<input type="text" class="form-control" id="full-name" name="name" placeholder="Enter name">
</div>
<div class="form-group">
<label for="email-address">Email address</label>
<input type="email" class="form-control" id="email-address" name="email" placeholder="Enter email">
</div>
<div class="form-group">
<input type="hidden" class="form-control" id="incubator" name="incubator">
</div>
<div class="form-group">
<input type="hidden" class="form-control" id="ip-address" name="ip-address">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="submit-form" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
The below code is for each of the 8 category's join buttons.
<a href="#" data-toggle="modal" data-target="#waitlist-modal" data-id="entrepreneur" class="yellow-link">
<p class="yellow-text call-to-action" style="font-size: 0.7rem;">JOIN THE WAITLIST ➞</p>
</a>
As far as I understand, its meant to collect the full name and email address visibly, then the IP and the "incubator" [category] silently. All but the "incubator" tag works fine, which never passes over anything. In the links, the previous coder uses data-id="entrepreneur" [a category] for example, which doesn't seem right to me.

Implement copy button functionality

I have this Bootstrap code which I would like to use to generate address and implement copy button functionality:
<div class="modal fade" id="bitcoinModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="container">
<div class="offset-top-20 text-md-left">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Copy address</h3>
</div>
<div class="section-60 offset-top-35">
<div class="offset-top-20 text-md-center">
<form class="rd-mailform form-inline-custom text-left" data-form-output="form-output-global" data-form-type="subscribe" method="post" action="http://.........">
<div class="form-group form-group-outside">
<div class="input-group">
<label class="form-label form-label-outside text-dark" for="forms-subscribe-email">Bitcoin Address</label>
<input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="#Required"/>
</div>
<div class="input-group-btn">
<button class="btn btn-width-165 btn-primary" type="submit">Copy</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
How I can copy the content from the input item and close the form when the value is copied into the clipboard?
You can use something like https://clipboardjs.com
p.s. To modify the state your site, you can use the success callback to modify the state of your site.
<script src="https://cdn.jsdelivr.net/npm/clipboard#2/dist/clipboard.min.js"></script>
<input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="#Required" />
<input type="button" value="copy" id="copy-button" data-clipboard-text="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy" />
<script>
var clipboard = new ClipboardJS('#copy-button');
var button = document.querySelector('#copy-button')
clipboard.on('success', function(e) {
button.value = "copied"
});
</script>
I showed you how to change the button text. Closing the modal should be similar.. Without knowing any details.. you probably have to remove or add a class to the modal to make it disappear.
For the delay you can use setTimeout() https://www.w3schools.com/jsref/met_win_settimeout.asp

One button should be disabled if input is null, but another button is affected as well

I'm trying to write a login/register page and make the "login" button disabled if the fields are not filled in (login and password). However it's also affection the "register" button, which is asking for the fields to be filled in as well.
I'm not sure what is causing this and removing the "required ng-model" from the labels stops the login button from working with "ng-disabled". Can someone please help me understand what's causing this behavior?
<div class = "form-group" aling="center">
<label for="exampleInputEmail" > Usuário </label>
<input type='text' placeholder = 'Usuário'
class="form-control" required ng-model = "user">
<div class="form-group">
<label for="exampleInputPassword"> Senha </label>
<input type='password' placeholder = 'Senha'
class="form-control" required ng-model = "password">
</div>
<p>
<div class="main" ng-controller="loginController">
<button class="btn btn-default" type="submit"
ng-disabled="form1.$invalid" ng-click="login(user,password)">
Login</button>
</div>
<button href="#" class="btn btn-default" type="submit"
ng-click="register()">
Register
</button>
<p></p>
Currently what happening is when you're clicking on Register button it tries to submit a form. You should change type="submit" of Register button to type="button". Also even if you don't specify any type on button, it considered as type="submit".
<button class="btn btn-default" type="button" ng-click="register()">
Register
</button>
You Register button is in the same form as all the other fields and has type submit and since the two inputs are required the submit button is automatically disabled
Some observations :
You define the controller loginController only for Login button. Hence, username and login fields are out of scope for this controller.
Define the controller in the parent element(outside of the form) in the DOM.
DEMO
var app = angular.module('myApp', []);
app.controller("loginController", function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="loginController">
<form name="myForm" novalidate>
<div class = "form-group" aling="center">
<label for="exampleInputEmail" > Usuário </label>
<input type='text' placeholder='Usuário'
class="form-control" required ng-model="user">
<div class="form-group">
<label for="exampleInputPassword"> Senha </label>
<input type='password' placeholder='Senha'
class="form-control" required ng-model="password">
</div>
<p>
<div class="main">
<button class="btn btn-default" type="submit"
ng-disabled="myForm.$invalid" ng-click="login(user,password)">
Login</button>
</div>
<button href="#" class="btn btn-default" type="submit" ng-click="register()">Register
</button>
</div>
</form>
</div>

modal appears on form submission even if the form is not validated

I am working on a contact us form. After clicking on the submit button i want the form to be validated for example if text is entered in the mobile number field i want an error message to be displayed.
if all the data types in the form are correct i want a modal to be displayed which says thank you.
heres the code:
<form class="form">
<div class="line"><label style="font-size: 18px;"for="fname">First Name *: </label><input type="text" id="fname" required /></div>
<div class="line"><label for="lname">Last Name *: </label><input type="text" id="lname" required /></div>
<!-- You may want to consider adding a "confirm" password box also -->
<div class="line"><label for="email">Email *: </label><input type="email" id="email"required /></div>
<!-- Valid input types: http://www.w3schools.com/html5/html5_form_input_types.asp -->
<div class="line"><label for="tel">Mobile *: </label><input type="tel"pattern="[789][0-9]{9}" id="tel" required/></div>
<div class="line"><label for="add">Address *: </label><input type="text" id="add" required/></div>
<div class="line"><label for="ptc">Post Code *: </label><input type="number" maxlenght="6" id="ptc" required /></div>
<div><button data-target="modal1" type="submit"class=" waves-effect waves-light btn modal-trigger">Submit<i class="material-icons right">send</i></button></div>
<p>Note: Please make sure your details are correct before submitting form and that all fields marked with * are completed!.</p>
</form>
below is the javascript
$(document).ready(function(){
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
$('.modal-trigger').leanModal();
});
here is the modal structure.
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Thank You</h4>
<p>Our Customer service team will contact you shortly</p>
</div>
<div class="modal-footer">
OK
</div>
</div>
The problem about your code is that you are handling the wrong event. I don´t know what framework you are using but your button probably shows the modal when the button is clicked and not when the form is submitted. It would be better to watch for the submit event of the form.
(function($){
$(document).ready(function(){
$('.form').on('submit', function(e){
e.preventDefault();
$('.modal-trigger').leanModal();//Show your modal?
});
});
})(jQuery);
Here is an example I prepared using Twitter bootstrap. I used your form and a the modal of the framework. The functionality must be very similar to the one you desire.
(function($) {
$(document).ready(function() {
$('form.form').on('submit', function(e) { //Handle the submit event of the form
e.preventDefault(); //This prevents the form from submitting, You might need to delete it in your actual code
$('#myModal').modal('show'); //In bootstrap this is the function that shows the modal. It might differ on your actual code.
});
});
})(jQuery);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<form class="form">
<div class="line">
<label style="font-size: 18px;" for="fname">First Name *:</label>
<input type="text" id="fname" required />
</div>
<div class="line">
<label for="lname">Last Name *:</label>
<input type="text" id="lname" required />
</div>
<!-- You may want to consider adding a "confirm" password box also -->
<div class="line">
<label for="email">Email *:</label>
<input type="email" id="email" required />
</div>
<!-- Valid input types: http://www.w3schools.com/html5/html5_form_input_types.asp -->
<div class="line">
<label for="tel">Mobile *:</label>
<input type="tel" pattern="[789][0-9]{9}" id="tel" required/>
</div>
<div class="line">
<label for="add">Address *:</label>
<input type="text" id="add" required/>
</div>
<div class="line">
<label for="ptc">Post Code *:</label>
<input type="number" maxlenght="6" id="ptc" required />
</div>
<div>
<button type="submit" class=" waves-effect waves-light btn modal-trigger">Submit</button>
</div>
<p>Note: Please make sure your details are correct before submitting form and that all fields marked with * are completed!.</p>
</form>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
your modal
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

password input on modal makes modal disappear

The html for a modal popup for a login is below (html). When a customer tries to login they must click on each input field, put the username and then password and click the submit submit button. If they click "enter" after inputting the username or login, the modal popup disappears.
Is there a way to have proceed to being logged in when clicking "enter/return" after in the password input box instead of clicking on the "Login" button. I would like both to work-- login button and hitting return after typing in the password.
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-forward"></span> Login
</li>
</ul>
<div class="modal fade" id="customer-login" tabindex="-1" role="dialog" aria-labelledby="customer-login-header" aria-hidden="true">
<div class="modal-dialog">
<form action="https_link" method="POST" name="log-in">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">x</button>
<h2 id="customer-login-header">Customer Login</h2>
<p class="hidden-xs">Log in below to receive access to your existing policies and quotes. The log in information was emailed to your after purchasing your policy or saving a quote.</p>
</div>
<div class="modal-body">
<fieldset>
<input type="text" class="form-control" id="cl_username" name="username" placeholder="Username?">
<br>
<input type="password" class="form-control" id="cl_password" name="password" placeholder="Password?">
</fieldset>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<input type="submit" class="btn btn-primary" value="Login">
</div>
<div class="modal-footer">
<p>Forgot your Username or Password?<br><a class="forgot-password" href="https://www.mexpro.com/client_login/retrieve_password.mhtml?aff_id=2149&lang=en&agtdst=www">Click here to Retrieve »</a></p>
</div>
</div>
</form>
</div>
</div>
Here is a possible solution: https://jsfiddle.net/99x50s2s/73/
When the user clicks on the Login button, alert will be displayed if either User Name or Password is left blank.
I have made minor changes in your HTML and used jquery as shown below,
HTML:
<button type='button' data-toggle="modal" data-target="#customer-login" data-backdrop="static">Click here</button>
<div class="modal fade" id="customer-login" tabindex="-1" role="dialog" aria-labelledby="customer-login-header" aria-hidden="true">
<div class="modal-dialog">
<form action="https://www.mexpro.com/client_login/do/login.mhtml?aff_id=2149" method="POST" name="log-in">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">x</button>
<h2 id="customer-login-header">Customer Login</h2>
<p class="hidden-xs">Log in below to receive access to your existing policies and quotes. The log in information was emailed to your after purchasing your policy or saving a quote.</p>
</div>
<div class="modal-body">
<div id='error' class='alert alert-warning hide'></div>
<fieldset>
<input type="text" class="form-control" id="cl_username" name="username" placeholder="Username?"/>
<br/>
<input type="password" class="form-control" id="cl_password" name="password" placeholder="Password?"/>
</fieldset>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<input type="submit" class="btn btn-primary" value="Login" id="SubmitBtn"/>
</div>
<div class="modal-footer">
<p>Forgot your Username or Password?<br/><a class="forgot-password" href="https://www.mexpro.com/client_login/retrieve_password.mhtml?aff_id=2149&lang=en&agtdst=www">Click here to Retrieve »</a></p>
</div>
</div>
</form>
</div>
</div>
JS
$('#SubmitBtn').on('click', function(event){
var errorDiv = $('#error');
errorDiv.addClass('hide').empty();
var userName = $('#cl_username').val();
var password = $('#cl_password').val();
if ($.trim(userName) == '' || $.trim(password) == '')
{
errorDiv.removeClass('hide').append('User Name and Password is required');
event.preventDefault();
}
});
1.) Is there a way to make an error show up if they hit return without typing anything?
Yes. Handle the onSubmit event on the form and validate that you have entries in the fields you want.
2.) Is there a way to have proceed to next step when clicking "enter/return" -- i.e. cursor moves to the password/and submit takes
place?
Change the Login button from type="submit" to type="button" and write javascript code to handle the onClick event. By having it as type="submit" the browser automatically submits the form when the Enter key is pressed.
Bear in mind that many users expect the Enter key to submit a login form, not move them between fields. What you are trying to do goes against expected browser behavior.