user2827213
user2827213

Reputation: 37

Getting a Input type="image" value from a request.getParameter()

Inside of my html, I have form that post back to my Servlet. I would like to get the value of my input type ="image" but instead I am receiving a null value.

My form:

 <form action="HomeController" method="post">
   <button type="submit" name="s4" value="someValue">
     <img src="s4.jpg" alt="phonestuff">
   </button>
   <input type="hidden" name="s4price" value="800">  
 </form>

my Servlet post:

double s4price = Double.parseDouble(request.getParameter("s4price"));
String s4 = request.getParameter("s4");
System.out.println(s4price);
System.out.println(s4);

My output:

800.0
null

Expected output:

800.0
Note4

Upvotes: 2

Views: 5988

Answers (2)

sharif2008
sharif2008

Reputation: 2798

You submit button will not send any image value. Because type="image" Defines an image as the submit button

try this:-

<form action="HomeController" method="post">
 <button type="submit" name="s4" value="Note4">
    <img src="s4.jpg" alt="phonestuff">
 </button>
 <input type="hidden" name="s4price" value="800">  
</form>

OR: in order to send a the s4 value you can send it as hidden field with a image submit button.

<form action="HomeController" method="post">
  <input type="image" src="s4.jpg" alt="Submit" >
  <input type="hidden" name="s4" value="Note4"> 
  <input type="hidden" name="s4price" value="800">  
</form>

servlet post:-

double s4price = Double.parseDouble(request.getParameter("s4price"));
String s4 = request.getParameter("s4");
System.out.println(s4price);
System.out.println(s4);

output:

800.0
Note4

Upvotes: 1

minion
minion

Reputation: 4353

Input type="image" does not pass on the value when you submit the form. You need to pass them in hidden value.

Upvotes: 1

Related Questions