<form name="myForm" method="post" onsubmit="return getComment()">
<textarea id="commentarea"></textarea>
<input type="text" name="locate" value="<%=rs.getString("location")%>">
<input type="submit" value="View Comment">
</form>
function getComment(){
<% String locate=request.getParameter("locate"); %>
var location = <%= locate%>;
document.getElementById('commentarea').value=location;
return false;
}
Everytime i click View Comment, there's no value printed.
I want to access locate in the scriptlet and print the value in the text area.
I know this is not the best way to access it, but i need to access it in this way.
Can anyone help me?
You have missed double/single quotes for the value of the location variable. If you don't need to submit the form, just use a button input element.
<form name="myForm" method="post">
<textarea id="commentarea"></textarea>
<input type="text" name="locate" value="<%=rs.getString("location")%>">
<input type="button" value="View Comment" onclick="getComment()">
</form>
function getComment(){
<% String locate=request.getParameter("locate"); %>
var location = "<%= locate%>";
document.getElementById('commentarea').value = location;
}
Related
<form action="<c:url value = "/deal/#value-that-user-enters#"/>" method="POST">
Kindly enter the deal id : <input type="text" name="dealId">
<input type="submit" value="Get the deal" />
</form>
In the above code, I want the action attribute of form to be /deal/(deal-id-entered-by-user). Is there any way to do it without using any javascript? And is it at all possible even with javascript?
You should be able to do it simply with javascript like this:
document.querySelector('form').onsubmit = function() {
this.setAttribute('action', "/baseurl/" + document.querySelector('input[name=dealId]').value)
}
<form action="/baseurl/" method="POST">
Kindly enter the deal id :
<input type="text" name="dealId">
<input type="submit" value="Get the deal" />
</form>
The best way is to change the form action by javascript:
document.forms[0].action=documnet.getElementById('dealId').value;
You can use it like this:
<form action="<c:url value = "/deal/#value-that-user-enters#"/>" method="POST">
Kindly enter the deal id : <input type="text" class="myBox" name="dealId">
<input type="submit" value="Get the deal" />
</form>
$('.myBox').on('change', function (event) {
var myVal = $(this).val();
$('form').attr('action', function(i, value) {
return value + myVal;
});
});
I have a multiple forms in a page and i would like to get the elements of a specific form. For example:
<form class="form" id="1">
<input type="text" name="message" value="message">
<button type="submit" value="submit">Submit</button>
</form>
<form class="form" id="2">
<input type="text" name="message" value="message">
<button type="submit" value="submit">Submit</button>
</form>
How to get the value of message of form id=2...... Thanks in advance
Just use attribute selectors
$('form[id=2]') // get the form with id = 2
.find('input[name=message]') // locate the input element with attribute name = message
.attr("value"); // get the attribute = value
You really shouldn't use raw numbers for ids, let's rename those to form_1 and form_2, then your jquery selector would be:
$("#form_2 [name='message']").val();
Simply query the input from the id of the form you'd like
console.log($('#2 input').val());
//more specific
console.log($('#2 input[name="message"]').val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form" id="1">
<input type="text" name="message" value="message">
<button type="submit" value="submit">Submit</button>
</form>
<form class="form" id="2">
<input type="text" name="message" value="message">
<button type="submit" value="submit">Submit</button>
</form>
You can use jQuery, or you can use basic JavaScript to return the message value. With basic JS, you would have to give your form a name, but then could return the message value.
Code:
function getValue() {
var message = document.forms["form_name"]["message"].value;
}
You would then have to return the function when the form is submitted
<form class="form" name="form_name" onsubmit="return getValue()">
We have two different forms in one jsp page one for submission and one that approves.
The code is:
<%if(rs.getInt("approve")==0) {%>
<form method="get" action="storecomment3.jsp">
Comments:<br>
<textarea name="comments" id="comments"></textarea>
<br>
<input type="hidden" name="article" value="<%=realname%>">
<input type="hidden" name="Username" value="<%=request.getParameter("Username")%>">
<input type="hidden" name="Password" value="<%=request.getParameter("Password")%>">
<input type=submit value="Submit">
</form>
<%} %>
<%if(rs.getInt("approve")==0) {%>
<form name="form1" action="usermain.jsp">
<input type=button value="Approve" onclick="validate()">
<input type="hidden" name="user" value="<%=request.getParameter("user")%>">
<input type="hidden" name="Username" value="<%=request.getParameter("Username")%>">
<input type="hidden" name="Password" value="<%=request.getParameter("Password")%>">
<script type="text/javascript">
function validate()
{
<%
r1.updateInt("approve", 1);
r1.updateRow();
%>
document.form1.submit();
}
</script>
</form>
The problem with the code is that when the submit button in the first form is clicked, the validate function is also getting executed which should not be happening!
Is there anything wrong with the code or the script?
Thanks!
I see a few possible problems here.
First, your condition rs.getInt("approve")==0 appears twice, perhaps one of them should be !=0?
Also it seems odd that you put r1.updateInt("approve", 1); and r1.updateRow(); inside the JS function. That code gets executed server side(!) as soon as the condition (rs.getInt("approve")==0) is met, no user interaction required.
I have a form on my html page which has name="submitform" and id="submitform"
I want to submit it using Javascript with the following script on that page:
document.forms("submitform").submit();
My submit button in the form has name="btnSubmit" so it doesn't override the original function.
However, it doesn't work
I get: [Error] TypeError: '[object HTMLCollection]' is not a function (evaluating 'document.forms("submitform")'
What to do?
Thanks!
UPDATE - Full Code
<form action='/post' name='submitform' id="submitform" method='post' class='pure-form'>
<textarea columns="40" rows="4" name='entry[body]' id="statement" placeholder='enter a note here to visualize the words and their connections, you can also use #hashtags and #mentions.'><% if (url) { %><%= urltitle %> <%= url %><% } %></textarea>
<div id="addToContextsLabel">contexts:</div>
<ul id="addToContexts"></ul>
<input type="hidden" id="addedContexts" name="addedContexts">
<input type="hidden" id="context" name="context" value="<%= context %>">
<input type="hidden" id="selectedContexts" name="selectedContexts" value="">
<input type="hidden" name="statementid" value="">
<br>
<input type='submit' id="submitbutton" name="btnSubmit" value="save" class="pure-button pure-button-primary">
</form>
and
document.submitform.submit();
on the page
Use
document.forms['submitform']
instead of round braces. The Background is, that document.forms is an array type and needs to be treated as such.
You can either use the index accessor
document.forms[0]
or by name as i mentioned above.
Hope that helped!
you must submit form like this
document.forms["name of your form"].submit();
I have a form in which there is one text field and ine submit button.on click of submit button,it goes to next PHP page.The user can enter text in the text field.The text field can contain n number of text string separated by space.like a user enters text as
MY name is peter
I want text to change as MY-name-is-peter in url.
when user clicks on submit button.the url should become like submit.php?search=MY-name-is-peter
<form action="submit.php" method="get">
<input type="text" name="search" id="search">
<input type="submit" name="submit" value="submit">
</form>
PLease guide on how to add hyphen in the strings in url.
<script>
var handler = function (element) {
element.form.search.value = element.form.search.value.replace(/ /g, '-');
};
</script>
<form action="" method="get">
<input type="text" name="search" id="search" />
<input type="submit" onclick="handler(this);" name="submit" value="submit" />
</form>
str_replace will work or if you want to use regex you can try preg_replace("/[-]/", " ", $yourString)
You should encode URL before submit it:
$('form').submit(function(){
var val = $(this).find('#search').val();
$(this).attr('action','submit.php'+encodeURI(val))
})
Example : http://jsfiddle.net/e3L3a/