I am using onclick event in anchor tag. when i clcik the anchor, I am invoking a java script function where I change the value of one input element and submit the form. The value is changed but the form does not submit the value. Please help me on this. FYI that if i create an element and add in the form before submission, it works. Why is not working in the first scenario.
<form name="form" >
<input type="hidden" name="input" value="test"/>
<a onclick='testMethod("test")'>click </a>
</form>
script used is
function testMethod(value)
{
if(serviceName != null && jQuery('#form') != null)
{
document.forms['form'].getElementById('input').value = value;
document.forms['form'].action = jQuery('#newAction').val();
document.forms['form'].submit();
}
}
The main problem here is you're trying to access the input by id when it doesn't have one set:
(Added id to form and id to input so we can select them easily):
<form name="form" id="form">
<input type="hidden" name="input" id="input" value="test" />
<a onclick='testMethod("test")' >click</a>
</form>
And the javascript to match (updating to use jQuery's selectors since you indicated you have jQuery support available):
function testMethod(value)
{
var form = $('#form');
if(serviceName != null && form.length)
{
$('#input').val(value);
form.attr('action', $('#newAction').val());
form.submit();
}
}
You can also update your code such that you aren't including the binding to the onclick method in the DOM, but attaching to it directly in javascript:
Changing the a element to have an ID:
<form name="form" id="form">
<input type="hidden" name="input" id="input" value="test" />
<a id="submitLink">click</a>
</form>
The javascript could now look like this:
$(document).ready(function() {
$('#submitLink').on('click', function(event) {
event.preventDefault();
var form = $('#form');
if(serviceName != null && form.length)
{
$('#input').val(value);
form.attr('action', $('#newAction').val());
form.submit();
}
});
});
Related
In a form I have a field called Description. Through CKEditor I need to pass that value and store it in my database. Can anyone help me out? Here is my code:
<div id="descriptionMore" style="margin-bottom:20px;margin-top: 38px;margin-left: 101px;">
<aui:layout>
<aui:column columnWidth="240">
<liferay-ui:input-editor width="880" cssClass="richText" />
</aui:column>
</aui:layout>
</div>
First of all, make sure you are using aui:form.
Link which will be useful for you.
Save Content of CKeditor via "aui:form"
Add HTML Editor to Portlet
CK editor in Liferay 6.0
Adding CK Editor in Liferay
This will defiantly be useful for you.
<%
Content content = (Content) request.getAttribute(ApplicationConstants.CONTENT);
%>
<portlet:actionURL var="saveOrUpdateContentUrl" name="saveOrUpdateContent">
<portlet:param name="redirect" value="<%=currentURL%>" />
</portlet:actionURL>
<liferay-ui:header title='<%= (content==null) ? "new-content" : "edit-content" %>' backURL="<%=redirect %>" />
<aui:form method="post" name="content_fm" onSubmit='<%= "event.preventDefault(); " + renderResponse.getNamespace() + "saveEntry();" %>'>
<aui:fieldset>
<aui:field-wrapper label="product-content">
<liferay-ui:input-editor />
<aui:input name="content" id="content" type="hidden" />
</aui:field-wrapper>
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
</aui:fieldset>
</aui:form>
<script type="text/javascript">
function <portlet:namespace />initEditor() {
return '<%= UnicodeFormatter.toString((content==null)? StringPool.BLANK : content.getContent()) %>';
}
function <portlet:namespace />getContent() {
return window.<portlet:namespace />editor.getHTML();
}
function <portlet:namespace />saveEntry() {
document.<portlet:namespace />content_fm.action = '<%=saveOrUpdateContentUrl%>';
document.<portlet:namespace />content_fm.<portlet:namespace />content.value = <portlet:namespace />getContent();
alert(<portlet:namespace />getContent());
submitForm(document.<portlet:namespace />content_fm);
}
</script>
This above code is properly working in Liferay 6.2. You should fetch CKEditor content using alloy script or javascript and set in hidden parameter for storing the value.
I am doing encryption for username and password in JavaScript using Jcryption algorithm. and decryption using Java . in that process before encryption process completion in JavaScript form is submitted .In Java I am not getting encrypted username and passwords. and I am getting normal values so I am prevented form submit and written code in the form submit after the encryption process completed I am calling submit event but form submitting multiple times.
give me suggestions is there any way to solve issues .or any alternate way to solve issues.
Thanks In Advance
My Html code
<form name="frm" method="post" action="/xsl-portal/xlogin" onSubmit="" target="_parent" autocomplete="off">
<input type="text" maxlength="45" name="eid1" id="eid1" />
<input type="hidden" maxlength="45" id="eid" name="eid" />
<input type="password" id="pw" name="pw" />
<input type="image" src="home-images/button-submit.png" id="submitButton" value="Submit" name="image" onclick="submitButtonClick();" />
<input type="hidden" value="" name="submit" id="submit"/>
</form>
Javascript code
var keys='';
function getKeys() {
$.jCryption.getKeys("/sakai-login-tool/EncryptionServlet?generateKeypair=true", function(
receivedKeys) {
keys = receivedKeys;
});
}
getKeys();
var encryptedUser='';
var encryptedPassword='';
/* form submit code*/
// $('#submitButton').on('click',function(){
function submitButtonClick(){
}
// });
$('form').submit(function(e) {
e.preventDefault();
var user=$('#eid1').val();
var password=$('#pw').val();
encryptionProcess(user,password).pipe(applyValues).pipe(function(){
console.log("eid"+$('#eid').val());
console.log("pw"+$('#pw').val());
$('form').submit();
});
})
;
function encryptionProcess(user,password){
var d= $.Deferred();
setTimeout(function() {
$.jCryption.encrypt(user, keys, function(encrypted) {
encryptedUser = encrypted;
console.log("printing user encryption:"+encryptedUser);
});
$.jCryption.encrypt(password, keys, function(encryptedPasswd) {
encryptedPassword = encryptedPasswd; /**
* As both userName and password are encrypted now Submit login
*/
console.log("printing password encryption:"+encryptedPassword);
});
d.resolve();
}, 10);
return d.promise();
}
function applyValues(){
var d1= $.Deferred();
setTimeout(function() {
$('#eid').val(encryptedUser);
$('#pw').val(encryptedPassword);
d1.resolve();
}, 10);
return d1.promise();
}
It is because of $('form').submit();. Since you are calling the jQuery submit() method, it will call the associated submit event handler causing a recursive cycle
Change it to use the dom element's submit() method, which will not result in the invocation of submit event handlers.
$('form')[0].submit();
//or e.target.submit();
Change your submit button to be a normal button. Listen for the click on this button and within that handler call $('form').submit();
The way you are doing at present will result in recursive submits.
I have a JSP page which has several input fields and a servlet validates the fields when I click on the submit button and redirects to another page.
I would like to add a popup window: "all the fields are ok" after a correct server-side validation.
So I started with:
<form ="alert('success');">
<input type="submit" value="submit">
</form>
The problem is that "success" is printed even if the fields are incorrect.
I thought about setting a parameter in the
protected void doPost(HttpServletRequest request,HttpServletResponse response, which calls an execute() function to say if things are OK or not but I do not know how to get this parameter just after the submit so I could make a conditional:
if (checkSubmitParameter == OK )
callsPopup()
Try something like this (see jsfiddle):
<script>
function checkit(){
var val = document.getElementById('txt').value;
if(val == "good"){
alert("success");
}else{
alert("failure!");
}
}
</script>
<form onsubmit='checkit()'>
<input type='text' id="txt" />
<input type='submit' value='Submit' />
</form>
I have .jsp in liferay, and use javascript with applet, but after the form sent to the portlet, on the server side, portlet doesn't catch the form, and do not show in logs additional messages.
jsp page's snippet:
<script type="text/javascript">
function processSigning(){
var applet = document.applets["SignApplet"];
var path_to_certificate = document.getElementById("certificate").value;
var pass = document.getElementById("password").value;
var filePath = document.getElementById("documentSign").value;
applet.filePath = document.getElementById("documentSign").value;
applet.profileTestPKCS12(path_to_certificate, pass);
document.getElementById("file").value = applet.getDocumentString(filePath);
document.getElementById("sign").value = applet.getSignString();
document.getElementById("cert").value = applet.getCertificateString();
document.getElementById("mainForm").submit();
}
</script>
<form id="mainForm" action="<portlet:actionURL>
<portlet:param name="COMMAND" value="LOAD"/>
</portlet:actionURL>">
<hidden id="file" value="asdf"></hidden>
<hidden id="cert" value="asdf"></hidden>
<hidden id="sign" value="asdf"></hidden>
<input type="button" onClick="processSigning();" value="click here!" >
</form>
portlets snippet:
public void processAction(ActionRequest request, ActionResponse response) throws PortletException {
session = request.getPortletSession(true);
String command = request.getParameter("COMMAND");
System.out.println("command=" + command);
log.info("command=" + command);
if ("LOAD".equals(command)) {
{
System.out.println("file");
log.info("file");
String fileBase64 = request.getParameter("file");
System.out.println(request.getParameter("file"));
log.info(request.getParameter("file"));
}
}
}
Check your portlet.xml if the portlet-class is pointing to MVCPortlet or your custom portlet class. It should point to the custom portlet class.
Try this form and see if it works for you:
<portlet:actionURL var="myActionURL"></portlet:actionURL>
<form id="mainForm" action="${myActionURL}" method="post">
<input type="hidden" name="COMMAND" id="COMMAND" value="LOAD" />
<input type="hidden" name="file" id="file" value="asdf" />
<input type="hidden" name="cert" id="cert" value="asdf" />
<input type="hidden" name="sign" id="sign" value="asdf" />
<input type="button" onClick="processSigning();" value="click here!" >
</form>
Hope this helps.
There is method for the form must be specified, because Liferay Portal works with "post" method, also the names of hidden parameters must be used with "name" attribute, because request works with name, not ids
This question already has answers here:
How do I call a specific Java method on a click/submit event of a specific button in JSP?
(4 answers)
Closed 7 years ago.
I am new to JavaEE and have a query regarding a servlet that has multiple methods.
I want to know how I can call a specific method on a servlet, when I click "Submit" button in JSP.?
Someone have suggested to use HTML hidden fields but I have no idea on how to implement them in Jsp.
You can just give the submit button a specific name.
<input type="submit" name="action1" value="Invoke action 1" />
<input type="submit" name="action2" value="Invoke action 2" />
<input type="submit" name="action3" value="Invoke action 3" />
The name-value pair of the pressed button is available as request parameter the usual way.
if (request.getParameter("action1") != null) {
// Invoke action 1.
}
else if (request.getParameter("action2") != null) {
// Invoke action 2.
}
else if (request.getParameter("action3") != null) {
// Invoke action 3.
}
Hidden fields in a JSP are the same as in HTML:
<input type="hidden" name="name" value="value">
Then in your servlet you can do
if (request.getParameter("name").equals("value")) { /* do something */ }
Depends on which method you want to call. Assuming that the URL Pattern declared for your servlet in web.xml is /myservlet*,
For doGet, just use a URL
http://localhost:8080/myservlet?name=value
For doPost, use a form.
<form action="/myservlet" method="post">
<input type="text" value="value" name="name" />
</form>
i dont think having 3 buttons is the best solution,basis on the logic and parameters method asked,
i believe this can be an accurate solution-
On the basis of parameters passed ,we can have 2 methods to access different actions via javascript-
1)getting values from user,checking them in javascript.
2)getting values from user,checking them in javascript,assigning values to hidden variables accordingly,calling servlets and using those hidden values.i have elaborated method1.
<html>
<head>
<script type="text/javascript">
function nawab() {
param = document.getElementById('param1').value;
alert('in nawab');
if (param != "") {
if (param === 'abc') {
alert('abc');
document.forms[0].action = "nawabServlet";
document.forms[0].submit();
}
if (param === 'def') {
alert('def');
document.forms[0].action = "nawabServlet2";
document.forms[0].submit();
}
}
else{
alert('empty');
document.forms[0].action = "nawabServlet";
document.forms[0].submit();
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>nawab has come</title>
</head>
<body>
<form>
param1:<input type="text" name="param1" id="param1"></select>
<input type="submit" onclick="nawab()">
</form>
</body>
</html>