send data to email

  • jilshi
  • Novice
  • Novice
  • User avatar
  • Posts: 32

Post 3+ Months Ago

i face a problem here. for your information, i'm using jsp to handle my wen site. the problem is i have created list box in my web page. i am able successfully enter the data selected in database(mysql). the data that was inserted in database is code that represent the detail in the list box. for example, when i choose 'apartment' from list box, 'AP' will be inserted into database. i can do all these except how can i send the information to email. i know how to send email but when the recepient receive, they will get the code data. For example, they will get 'AP' instead of 'apartment'. how can i send the full word to email???

below is some part of my coding:
Code: [ Select ]
<option selected="selected" value="done">--Select--</option>
<% String select_2 = "SELECT smd_cde2_typ, smd_cde2, smd_cde2_desc "+
"FROM smcde2 "+
"WHERE smd_cde2_typ = 'T' "+
"ORDER BY smd_cde2_desc ";
Statement stmt_2 = conn.createStatement();
ResultSet rset_2 = stmt_2.executeQuery(select_2);
while (rset_2.next()) {
property_code = rset_2.getString("smd_cde2");
property_desc = rset_2.getString("smd_cde2_desc");%>
<option value="<%=property_code%>"><%=property_desc%></option>
<% } %>
</select>
  1. <option selected="selected" value="done">--Select--</option>
  2. <% String select_2 = "SELECT smd_cde2_typ, smd_cde2, smd_cde2_desc "+
  3. "FROM smcde2 "+
  4. "WHERE smd_cde2_typ = 'T' "+
  5. "ORDER BY smd_cde2_desc ";
  6. Statement stmt_2 = conn.createStatement();
  7. ResultSet rset_2 = stmt_2.executeQuery(select_2);
  8. while (rset_2.next()) {
  9. property_code = rset_2.getString("smd_cde2");
  10. property_desc = rset_2.getString("smd_cde2_desc");%>
  11. <option value="<%=property_code%>"><%=property_desc%></option>
  12. <% } %>
  13. </select>

:(
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I have limited experience with JSP, but I think the problem is the same here for any server-side script. When the form is submitted the text between the option tag itself isn't submitted if the value attribute is present (as far as I know anyway). If there is some way to access the text value server-side when the value attribute exists someone please correct me.

I'm assuming you want to put a record into your database and send the user some sort of confirmation email, with the code going into the db and the text going into the email. If that's the case then I think you could add a hidden field to the form and use client-side javascript to set its value when the user makes a choice from the list box - using the text inside the option instead of the code inside the value attribute.

Then when the form is submitted you could retrieve the value of the list box and enter it into your db as you're already doing, but when you go to send the email you could retrieve the value of the hidden field containing the text of the selection and send that instead. I can't show you an example in JSP, but the javascript code should work regardless of the server-side language. This is how I threw it together with PHP:

Code: [ Select ]
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function setHiddenField()
{
    var f = document.forms['nameOfYourForm'];
    var hidden = f.elements['property_desc'];
    var i = f.elements['property_code'].selectedIndex
    var textVal = f.elements['property_code'].options[i].text;
    hidden.value = textVal;
}
// -->
</script>
</head>
<body>

<form name="nameOfYourForm" action="<?=$PHP_SELF ?>" method="post">

<select name="property_code" onchange="setHiddenField()">
<option value="">-----Select-----</option>
<option value="AP">Apartment</option>
<option value="CD">Condo</option>
<option value="LF">Loft</option>
</select>

<input type="hidden" name="property_desc" value="">

<br><br>
<input type="submit" />
</form>

<?
    $codeVal = $_POST['property_code'];
    $descVal = $_POST['property_desc'];
    echo "code value = " . $codeVal . "<br>";
    echo "desc value = " . $descVal . "<br>";
?>

</body>
</html>
  1. <html>
  2. <head>
  3. <title>Untitled</title>
  4. <script type="text/javascript">
  5. <!--
  6. function setHiddenField()
  7. {
  8.     var f = document.forms['nameOfYourForm'];
  9.     var hidden = f.elements['property_desc'];
  10.     var i = f.elements['property_code'].selectedIndex
  11.     var textVal = f.elements['property_code'].options[i].text;
  12.     hidden.value = textVal;
  13. }
  14. // -->
  15. </script>
  16. </head>
  17. <body>
  18. <form name="nameOfYourForm" action="<?=$PHP_SELF ?>" method="post">
  19. <select name="property_code" onchange="setHiddenField()">
  20. <option value="">-----Select-----</option>
  21. <option value="AP">Apartment</option>
  22. <option value="CD">Condo</option>
  23. <option value="LF">Loft</option>
  24. </select>
  25. <input type="hidden" name="property_desc" value="">
  26. <br><br>
  27. <input type="submit" />
  28. </form>
  29. <?
  30.     $codeVal = $_POST['property_code'];
  31.     $descVal = $_POST['property_desc'];
  32.     echo "code value = " . $codeVal . "<br>";
  33.     echo "desc value = " . $descVal . "<br>";
  34. ?>
  35. </body>
  36. </html>


I tested the client-side code in Netscape 4.08, FireFox 0.8, IE 6 and Opera 7. Obviously it won't work for the small number of people who have javascript disabled, so you might want to check to see whether the hidden field is just an empty string, in which case I think your only option would be to substitute the code from the list box.

Here's a test page of the PHP example:

http://www.gotrivia.com/testing/test1.php
  • jilshi
  • Novice
  • Novice
  • User avatar
  • Posts: 32

Post 3+ Months Ago

:oops:
i would like to ask is java same with javascript?? i also use java in my jsp program.......
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

No, javascript and java are two entirely different languages despite the unfortunate similarity in names.
  • jilshi
  • Novice
  • Novice
  • User avatar
  • Posts: 32

Post 3+ Months Ago

the code that you provide to me seem useful however i don't know how to integrate your code with my jsp program. thank you for your kind answer. i wish there is someone that could help me.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Place the script before the </head> as in the example:

Code: [ Select ]
<script type="text/javascript">
<!--
function setHiddenField()
{
  var f = document.forms['nameOfYourForm'];
  var hidden = f.elements['property_desc'];
  var i = f.elements['property_code'].selectedIndex
  var textVal = f.elements['property_code'].options[i].text;
  hidden.value = textVal;
}
// -->
</script>
  1. <script type="text/javascript">
  2. <!--
  3. function setHiddenField()
  4. {
  5.   var f = document.forms['nameOfYourForm'];
  6.   var hidden = f.elements['property_desc'];
  7.   var i = f.elements['property_code'].selectedIndex
  8.   var textVal = f.elements['property_code'].options[i].text;
  9.   hidden.value = textVal;
  10. }
  11. // -->
  12. </script>


Inside the form tag give your form a name:

Code: [ Select ]
<form name="nameOfYourForm".....


you don't have to name it that of course, if you want to name it something else or it already has a name just change 'nameOfYourForm' in the first line of the function so that it matches.

Then add the hidden field anywhere in your form::

Code: [ Select ]
<input type="hidden" name="property_desc" value="">


Then add the onchange call of the function to the drop-down list box:

Code: [ Select ]
<select name="property_code" onchange="setHiddenField()">


If your drop-down list box already has a name other than property_code then you will need to change 'property_code' in the two places that it appears inside the javascript function to match whatever name it has. Then you can use your jsp code to access the hidden field's contents the same way you get the other form information.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 112 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.