How can I pass a parameter via submit button? - html

In my code for an mini online book store i have a following line repeating 5 times with different value for 'name' parameter
<input name="JSP-2" type="submit" value="Buy">
On clicking the button Buy, the application redirects to a file buy.jsp where it gets the value of name and displays corresponding details of the book.
In my buy.jsp, I have included
<% String bname= request.getParameter("name");
out.print(bname);
%>
But the name doesnt get assigned to bname and it shows the value as null. How do I pass a parameter from the submit type input?
Please help.

You have to pass the parameter in the request.
Since you are having a form, and submitting the form, you can have a hidden field in the form called, say "submitType", and populate it whenever you click the button, using javascript. Then this will be available in the next request.
Somewhere inside the form :
<input type="hidden" name="submitType">
in the submit buttons:
<input name="JSP-2" type="submit" onclick="setType('Buy')">
Javascript:
formName is the name of your form
<script>
function setType(type)
{
//formName is the name of your form, submitType is the name of the submit button.
document.forms["formName"].elements["submitType"].value = type;
//Alternately, you can access the button by its Id
document.getElementById("submitId").value = type;
}
</script>

Related

using a variable to populate the "mailto:" value using "form action"

Newbie question no.2, sorry in advance!
I have somehow managed to create a form with various selection boxes, one of which is the email that the form should send the email to (using mailto:). I've managed to get the value of the email field stored as a variable ("emailtouse"), and now I am trying to use the variable in the "mailto:" code but it's not having it, I either get blank or the variable name itself when I attempt the process.
Thanks
Ian
***variable setting within script in header***
var emailtouse = "mailto:"+emailgoto[value]
***form action***
<form action='+emailtouse+'?
cc=u16#myleague.co.uk&subject=Match%20Postponement/%20Cancellation%20Request" method="post"
enctype="text/plain">
Even if your variable is updated, the "action" is not updated after the variable changes, so it contains the original value, calculated upon rendering the page.
Please see the following CodePen example on how to update the form action before submit:
<form
id="form1"
onsubmit="return updateAction(this)"
action="javascript:;"
method="post">
<button type="submit">Do it!</button>
</form>
... and the JS to update the form action, and to test that it really worked:
let emailtouse = "testemail#somewhere.com";
function updateAction(element) {
element.action =
emailtouse +
"&cc=u16#myleague.co.uk&subject=Match%20Postponement/%20Cancellation%20Request";
checkIfItReallyWorks();
return false; // change to true to submit!!!
}
function checkIfItReallyWorks() {
let form = document.getElementById("form1");
alert(form.action);
}
The above code on CodePen: https://codepen.io/cjkpl/pen/vYxPJQd

Angular 2 html form validation

I've created a form using html validations with Angular 2.
I want to to check the sate of the inputs (no empty, correct format, etc) when the user click to a certain button. At the moment I'm doing it as following:
<form id="memberForm" #memberForm="ngForm" >
<input
type="text"
id="MemberName"
required
name="MemberName"
[(ngModel)]="newMember.name">
</form>
<div
[ngClass]="{'button_disabledButton' : !memberForm?.valid}"
(click)="onSubmit(memberForm?.valid, memberForm);">
<span>Next</span>
</div>
With this, I'm only evaluating the input once clicked and focus out. How can I make it hapens when the user click in the "Next" element?
You should make getter/setter solution for your ngModel input.
In the .ts file in the appropriate class put this:
savedVar:string = '';
get variable(): string {
return this.savedVar;
}
set variable(str: string) {
this.savedVar = str;
// do your validation
}
In template use ngModel=variable like this:
<input [(ngModel)]="variable">

mvc - give submit buttons same name when using dynamic value

I have a list of items in my view and each has the below submit input type. Each input type has a dynamic value that is then passed through to the parameter 'int button' so the controller is aware of which button has been selected.
View
<input type="submit" name="button" value="#Model.Sites[i].Id" />
Controller
[HttpPost]
public ActionResult ViewInvoice(List<string> invoice, List<int> site, int button = 0)
{
}
The dynamic value is posted correctly to the controller however the buttons are named in the view according to this dynamic value "#Model.Sites[i].Id". How can i set all the button names to "Select" while keeping the dynamic value for my controller?
Thanks in advance.
try using this:
<button type="submit" name="button" value="#Model.Sites[i].Id">Select</button>
the timing will be a little tricky but you can try this
<input type="submit" value="Select" class="btnSubmit1" />
then in your script set a hidden field based on the button click
$(document).ready(function(){
$('.btnSubmit1').on('click', function(){
$('.hdnButton').val('btnSubmit1');
});
});
If the form submits before the field is set then you may need to change the button type to button, set the field and submit the form via ajax Submit a form using jQuery

HTML: How to redirect users to a variable url depending on their form input?

I've the problem that I want to use a payment system for my website for which I need to setup a system by which users get redirected to a url. This url needs to contain their own username on the location of the text [USER_ID]. The problem is that the url is built up like: &uid=[USER_ID]&widget=m2_1 How can it get the [USER_ID] to change to exactly the same thing the user entered in a form before:
<form>
User: <input type="text" name="url1" id="url1" value=""><br>
<input type="submit" name="submit" value="Goto URL" onclick="redirect()">
</form>
And use the text the user submitted in the form box to get it on the place of [USER_ID]?
This approach uses jquery's val() to retrieve the value from the form input, then it concatenates it to the url. I hope you are doing some sort of user validation...
function redirect()
{
...
var userId = $("#url1").val();
var url = "redirect" + "&uid=" + userId + "&widget=" + widget;
...
}
In the redirect function that you are using, you can extract the data of the input box and redirect the user as
window.location = yoursite.com/yourpage.php?user_id=getElementById('url1').value;
If you attach an action attribute to the form tag say : action='submit.php', and also attach form tag method='post'. Also, add a then in the file 'submit.php' you would use the following code (indexed by the name attribute of the input tag). The last line is how to do a redirect in php.
<?php
//submit.php
$root = 'www.foo.bar/';
$user = $_POST['user'];
$url= $root.'&uid=[$user]&widget=m2_1';
header('Location: $url');
?>
checkout:
http://myphpform.com/php-form-tutorial.php
also, if you prefer to use javascript or jQuery you can use ajax to post to the server and get the response.

session in jsp/struts

I have 3 jsp, jsp1..jsp & jsp2.jsp have a button name "TEST", when user click on it - he gets forwarded to Test.jsp which dynamically changes depending on which jsp user has pressed TEST.
so depending on user where he comes from, I change the logic in action class to direct the user, for that I am passing sessions.
jsp1.jsp
<input type="hidden" name="jspType" value="M" property="jspType">
jsp2.jsp
<input type="hidden" name="jspType" value="C" property="jspType">
In the action class of my Test.jsp
TestAction.java
String jspTypeVariable = (String) request.getParameter("jspType");
later down in code
if(jspTypeVariable=="M")
{
system.out.println("Magic");
}
else if (jspTypeVariable=="C")
system.out.println("Cash");
==================================
It doesnt work? Any one help
You can't compare Strings with ==. == tests if both objects are the same instance, not if their contents is the same. Use if ("M".equals(jspTypeVariable)) instead.