is there a way to call a method from a controller using a button? i did this:
<g:form controller="aluno" action="pesquisar"><input type="submit" value="Pesquisar"></g:form>
it worked, but, is there another way?
You can use actionSubmit tag:
<g:actionSubmit value="My Button" action="myAction" />
In a general way, you can use createLink to generate links to actions:
<a href="${createLink(action:'myAction', controller:'myController')}">
Try this.
<g:link class="btn btn-info btn-sm" action="pesquisar" resource="${instance}">TRY IT</g:link>
Note : When you click try it Button it will automatically get controller which you currently used.
An alternative, you can use a button in place of an input:
<g:link controller="yourcontroller" action="yourfunction">
<button type="button">Press me!!!</button>
</g:link>
Related
I have a form with id theForm which has the following div with a submit button inside:
<div id="placeOrder"
style="text-align: right; width: 100%; background-color: white;">
<button type="submit"
class='input_submit'
style="margin-right: 15px;"
onClick="placeOrder()">Place Order
</button>
</div>
When clicked, the function placeOrder() is called. The function changes the innerHTML of the above div to be "processing ..." (so the submit button is now gone).
The above code works, but now the problem is that I can't get the form to submit! I've tried putting this in the placeOrder() function:
document.theForm.submit();
But that doesn't work.
How can I get the form to submit?
Set the name attribute of your form to "theForm" and your code will work.
You can use...
document.getElementById('theForm').submit();
...but don't replace the innerHTML. You could hide the form and then insert a processing... span which will appear in its place.
var form = document.getElementById('theForm');
form.style.display = 'none';
var processing = document.createElement('span');
processing.appendChild(document.createTextNode('processing ...'));
form.parentNode.insertBefore(processing, form);
It works perfectly in my case.
document.getElementById("form1").submit();
Also, you can use it in a function as below:
function formSubmit()
{
document.getElementById("form1").submit();
}
document.forms["name of your form"].submit();
or
document.getElementById("form id").submit();
You can try any of this...this will definitely work...
I will leave the way I do to submit the form without using the name tag inside the form:
HTML
<button type="submit" onClick="placeOrder(this.form)">Place Order</button>
JavaScript
function placeOrder(form){
form.submit();
}
You can use the below code to submit the form using JavaScript:
document.getElementById('FormID').submit();
<html>
<body>
<p>Enter some text in the fields below, and then press the "Submit form" button to submit the form.</p>
<form id="myForm" action="/action_page.php">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="button" onclick="myFunction()" value="Submit form">
</form>
<script>
function myFunction() {
document.getElementById("myForm").submit();
}
</script>
</body>
</html>
HTML
<!-- change id attribute to name -->
<form method="post" action="yourUrl" name="theForm">
<button onclick="placeOrder()">Place Order</button>
</form>
JavaScript
function placeOrder () {
document.theForm.submit()
}
If your form does not have any id, but it has a class name like theForm, you can use the below statement to submit it:
document.getElementsByClassName("theForm")[0].submit();
I have came up with an easy resolve using a simple form hidden on my website with the same information the users logged in with. Example: If you want a user to be logged in on this form, you can add something like this to the follow form below.
<input type="checkbox" name="autologin" id="autologin" />
As far I know I am the first to hide a form and submit it via clicking a link. There is the link submitting a hidden form with the information. It is not 100% safe if you don't like auto login methods on your website with passwords sitting on a hidden form password text area...
Okay, so here is the work. Let’s say $siteid is the account and $sitepw is password.
First make the form in your PHP script. If you don’t like HTML in it, use minimal data and then echo in the value in a hidden form. I just use a PHP value and echo in anywhere I want pref next to the form button as you can't see it.
PHP form to print
$hidden_forum = '
<form id="alt_forum_login" action="./forum/ucp.php?mode=login" method="post" style="display:none;">
<input type="text" name="username" id="username" value="'.strtolower($siteid).'" title="Username" />
<input type="password" name="password" id="password" value="'.$sitepw.'" title="Password" />
</form>';
PHP and link to submit form
<?php print $hidden_forum; ?>
<pre>Forum</pre>
I'm using iron-form and I'm trying to POST a file to a (currently local) server. I'm having two buttons, one to actually send the file and one to cancel. I'm having a problem cancelling. Here's the form:
<form is="iron-form" action="http://localhost:7733/receivedoc" id="restForm" method="post" >
<table class="starter-inputs">
<tr><td>
<px-file-upload
id="uploadComponentId"
message="Drag and drop files here, or click the button below."
multiple=false
accept=".xls,.xlsx">
</px-file-upload>
</td></tr>
</table>
<button class="btn btn--large btn--icon" id="saveDataSetButton">
<i class="fa-briefcase">Generate Pacing File</i>
</button>
<button class="btn btn--large btn--icon" id="cancelDataSetButton">
<i class="fa-briefcase">Cancel</i>
</button>
<div class="output"></div>
</form>
The cancelDataSetButton is being handled as:
this.$.cancelDataSetButton.addEventListener('click', function() {
console.log('restForm.cancelDataSetButton click')
restForm.reset();
restForm.querySelector('.output').innerHTML = 'Operation cancelled.';
});
HOWEVER, since By default, a native element (or input type="submit") will submit this form., the POST gets fired regardless. How can I prevent the Cancel button from POSTing?
it's a good question.
Couple things I would like to highlight here...
First of all, let's refresh some basics regarding HTML spec:
1) <button> without attribute type will act as type=submit as default attribute, that's why both of your buttons will submit the form.
2) <button> is supporting type="reset" which will reset all your fields into initial value(s) (e.g. clear them) and will not submit a form, so there are no JS handler code is needed for that at all.
In total, I would recommend to do some adjustments in your HTML code, related to buttons block:
<button type="submit" class="btn btn--large btn--icon" id="saveDataSetButton">
<i class="fa-briefcase">Generate Pacing File</i>
</button>
<button type="reset" class="btn btn--large btn--icon" id="cancelDataSetButton">
<i class="fa-briefcase">Cancel</i>
</button>
Please make note, that for cancelDataSetButton no JS code is needed (to clear the field), and you could delete whole event listener:
this.$.cancelDataSetButton.addEventListener('click', function() {
and so one.
Some references to catch up:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
In my app - grails 3.0 I'm trying to return custom URL: /book/showbook/2
The controller has two actions:
action1 - collecting user form and create object book
action2 - showing the information of the created book.
Spring security plugin is installed.
Problem: instead of displaying: /book/showbook/2 the result of the submit button is:
/book/showbook.
URL mappings code:
static mappings = {
"/book/showbook/$id?(.$format)?"(controller: 'game', action: 'showbook')
"/$controller/$action?/$id?(.$format)?"{
constraints {
}
}
View action 1 code:
<button type="submit" class="btn btn-danger btn-xs" params:[id: ${bookID}]>Submit data</button>
Action2 code:
def showbook(Book book) {
respond book
}
I don't think you are creating the form as it should be.
You can read the Grails official form tag.
http://docs.grails.org/latest/ref/Tags/form.html
Please try this
<g:form name="book" action="showbook" id="${bookID}">
<button type="submit" class="btn btn-danger btn-xs">Submit data</button>
</g:form>
Please make sure that ${bookID} is defined somewhere in your gsp or passed from your modal to the appropriate view otherwise, it will not work.
I have the following form:
<g:form controller="${controllerName}" action="${actionName}" params="${params}">
<g:select name="publisher_status" from="${['Active']}" value="${publisher_status}" noSelection="${[null:'No Filter']}" />
<button type="submit" class="btn btn-default btn-primary btn-sm ">Apply</button>
</g:form>
Each time I submit this form, publisher_status value is appended to the previous one, resulting in a list like publisher_status=[null,'Active'] etc. What I really need is to overwrite the previous value, so I always have just a string. I tried the following above and below the above form, but it is not working:
<g:set var="params" value="${params.remove('publisher_status')?params:params}"/>
Any suggestions as to how to get around this issue ?
You can't set params like that, but you can filter the map that you pass to g:form:
<g:form controller="${controllerName}" action="${actionName}"
params="${params.findAll {k, v -> k != 'publisher_status'}}">
Hi All,
I have an edit button
<span class="button"><g:actionSubmit class="edit" action="edit" value="${message(code: 'default.button.edit.label', default: 'Edit')}" /></span>
But when click on it the adderess on the browser was not http:\...\edit,
it was http:\...\index. Why?
I tried to delete or rename the name of Edit in controller, it still not influence. Why?
Thanks!!!
Try specifying controller's name with g:form like
<g:form controller="CONTROLLER_NAME">