How to add button handler in Scala Play framework? - scala

I'm using scala play framework + scala template engine for front end. How can I add button handler? I need to call some function when clicking on it.
<body>
<p>Database interface<br />
<textarea style="margin: 0px; height: 193px; width: 533px;" cols="40" name="comment" rows="3"></textarea>
</p>
<p><input type="submit" value="Select" /> <input type="submit" value="Insert" /> <input type="submit" value="Update" /></p>
</body>

There is no special button handlers. You can write it in Javascript or plain HTML. The only trick thing is to get a link the server function. You can get the link automatically by reverse routing.
For example you have the route
GET /count controllers.CountController.count
Then you can get the link by calling controllers.routes.CountController.count in your code.
In the template engine, link:
Count
Plain HTML button:
<form action="#controllers.routes.CountController.count">
<input type="submit" value="HTML only" />
</form>
Button with Javascript:
<input type="submit" onclick="location.href='#controllers.routes.CountController.count';" value="Javascript" />
If you need some special thing on the client side to process then you need to write your own code in Javascript.

There's nothing in Scala/Play/Twirl that would handle button clicks, the simplest approach would be to use jQuery .click()

Related

I need help putting all the elements of an email signup form on the same line

I have a WordPress website with a bar at the top that I can add custom code to.
I'm trying to add an Infusionsoft email signup form to this bar and I want everything to be on the same line. Currently it's showing the field label on the first line, then the email field on the second line and then the submit button on the third line.
I can add whatever code I need to this form and this custom code box and I can also add custom CSS to the theme options themselves.
I've tried a bunch of things and some of them almost work but I can't figure this out.
Any help would be greatly appreciated.
Here's my form code:
<form accept-charset="UTF-8" action="https://ca383.infusionsoft.com/app/form/process/647e604918a3f0a0c52cd6b907f9d7d3" class="infusion-form" id="inf_form_647e604918a3f0a0c52cd6b907f9d7d3" method="POST">
<input name="inf_form_xid" type="hidden" value="647e604918a3f0a0c52cd6b907f9d7d3">
<input name="inf_form_name" type="hidden" value="Web Form submitted">
<input name="infusionsoft_version" type="hidden" value="1.70.0.80421">
<div class="infusion-field">
<label for="inf_field_Email">Sign Up For Weekly Tips and Specials Offers!</label>
<input class="infusion-field-input" id="inf_field_Email" name="inf_field_Email" placeholder="Email *" type="text">
</div>
<div>
<div> </div>
</div>
<div class="infusion-submit">
<button type="submit">Submit</button>
</div>
I moved the infusion submit div inside the infusion field div. I used display flex and flex-direction row to achieve them on the same row.
I know you're using word press and probably just copy and pasting code. If you plan to do web design work it would be well worth it to study up on HTML and CSS. It's actually not that complicated and you'll have so much control over how things look on your site!
*Edit to center content
<form accept-charset="UTF-8" action="https://ca383.infusionsoft.com/app/form/process/647e604918a3f0a0c52cd6b907f9d7d3" class="infusion-form" id="inf_form_647e604918a3f0a0c52cd6b907f9d7d3" method="POST">
<input name="inf_form_xid" type="hidden" value="647e604918a3f0a0c52cd6b907f9d7d3">
<input name="inf_form_name" type="hidden" value="Web Form submitted">
<input name="infusionsoft_version" type="hidden" value="1.70.0.80421">
<div class="infusion-field" style="display:flex; flex-direction: row; justify-content: center;">
<label for="inf_field_Email">Sign Up For Weekly Tips and Specials Offers!</label>
<input class="infusion-field-input" id="inf_field_Email" name="inf_field_Email" placeholder="Email *" type="text" style="margin-right: 10px;">
<div class="infusion-submit">
<button type="submit">Submit</button>
</div>
</div>
</form>

Strange IE form file select bug

I am making a simple image uploader. Since normal file selected cannot be styled do i use a bit of javascript to overcome this. There's just one problem, when someone using IE trys to upload a file by pressing the "fake" file select bar and press submit the selected image disappear. Do anyone know why this happens?
Try it for yourself
File score (same as in link above):
<form action="fileUploadBugIE.php" method="POST" enctype="multipart/form-data" character_set="UTF_8" >
<input type="text" readonly="readonly" id="filePath" onclick="document.getElementById('fileUL').click();" /><br /><br />
<input type="file" name="imageFile" id="fileUL" onchange="document.getElementById('filePath').value = this.value;"/><br />
<input type="submit" value="Submit" />
</form>

Binding multiple text boxes with multiple submitt buttons in same form

I have a form where I have a search functionality with a text box(TextBox1) and a submitt button(Button1). Apart from search, there is another set of textbox(TextBox2) and submitt button(Button2). When I write something in search box(TextBox1), and hit enter, the validation message of the second textbox(TextBox2) is shown.
I am not sure how to bind the respective textboxes with submitt buttons. Please help.
Thanks in advance
Depending on how much control you have, the easiest way would be to have separate forms for each texbox/button combo.
<form action="dosomething.php">
<input type="text" name="foo" />
<input type="submit" />
</form>
<form action="dosomethingelse.php">
<input type="text" name="bar" />
<input type="submit" />
</form>
If you can't or don't want to do that, you'll need to look at handling the onkeydown event on the text boxes to prevent an automatic submission. Something like <input type="Text" name="foo" onkeydown="doSubmit(this); return false;" />. Note the return false;, which prevents the default action from taking place.

My HTML5 form displays inline and without line breaks

I'm doing this HTML5 form, but when I check it on the browser (running it locally) it displays the label next to the input space next to le label and so.
heres the code:
<form>
<label for="name">Nombre:</label>
<input type="text" id="name" placeholder="Ingresa tu Nombre" required />
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Ingresa tu e-mail " required />
<label for="message">Mensage:</label>
<textarea id="message" placeholder="Ingresa tu Mensaje" required></textarea>
<input type="submit" value="Envia tu mensage" >
</form>
should I use <br />? I already checked on other web pages and they dont use it
thank you.
As Kolink pointed out, the form displays inline as all the elements inside it are inline.
You shouldn't be using <br/> or <p> as they are not intended for that purpose (You shouldn't be using a toothbrush to clean a toilet). Better use a <ul> with <li> for each field. This makes sense as the form is nothing but a list of fields.
The mark-up would be like this:
<form>
<ul>
<li>
<label for="something">some label</label>
<input id="something" />
</li>
</ul>
</form>
Alternatively, you can go ahead and use <div> as well (but not <p>).
Well, <label> is inline, <input> is inline, <textarea> is inline...
If all your elements are inline, of course your overall form will be.
Try using the <br /> tag, or maybe <p>...</p>.
Kolink is correct. Another way to address this is to add
display: block;
to all input and label elements using CSS.

How do I submit a form with a <li> instead of a submit button?

I want to be able to submit a form but instead of having to click on a submit button, I'd like to be able to click on an <li> element and have it submit.
Any help would be GREAT!
Thanks in advance!
Ncoder
You could put an onclick event on the LI that calls the forms submit event:
<form id="myForm" action="foo.htm" method="post">
<ul>
<li onclick="myForm.submit();">Click me</li>
</ul>
</form>
Your form would be non-standard and not very accessible though.
<li onclick="formName.submit();">
Although the above method will work, it seems such a strange requirement, and I'd advise re-thinking the logic behind the program.
JavaScript.
Wire the onclick event of the element. Get a reference to the form.
var frm = document.getElementById('formid');
Submit it.
frm.submit()
Add click handler on <li> element and use javascript to submit the form document.getElementById('formid').submit();
<form id="myForm">
...
<li id="something" onclick="mySubmit();"><p>hello world!</p></li>
...
</form>
<script type="text/javascript">
function mySubmit(){
document.getElementById("myForm").submit();
}</script>
I believe I was attempting to accomplish something quite similar to you, but I decided to code it the "correct" way. Here is an alternative approach that basically stylizes the submit button like an li element.
HTML/Coldfusion
<div id="student_options">
<ul>
<cfoutput query="student_specs">
<cfif #currentStudent# IS NOT #student_specs.GS1FNFP#>
<form action="" method="POST" name="dropdown">
<input type="hidden" name="change_stnum" value="#Trim(student_specs.STNUM)#" />
<input type="submit" name="submit_stu_change" value="#Trim(student_specs.GS1FNFP)#" />
</form>
</cfif>
</cfoutput>
</ul>
</div>
CSS
#student_options input[type="submit"] {
padding:15px 20px 15px 20px;
font-size:90%;
color:#555;
background-color:#eee;
display:block;
width:100%;
text-align:left;
}
#student_options input[type="submit"]:hover {
background-color:#F4F4F4;
cursor:pointer;
}
You will have to customize elements to your liking, but this is a better approach than using javascript to submit the form.