Author Topic: java, forms, invalid character, escaping it?  (Read 1105 times)

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
java, forms, invalid character, escaping it?
on: March 29, 2010, 13:59:59 PM
So Im working on the next thing, sorting out a survey form using the webform module in drupal.

The problem I am encountering is that the form names are autogenerated and include hyphens.

I want to use a simple piece of code which will update a text box that shows the sum of other text boxes, but the code is having hissy fits because of said hyphens. The below works when left alone:

Code: [Select]
function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  one = document.autoSumForm.firstBox.value;
  two = document.autoSumForm.secondBox.value;
  document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
  clearInterval(interval);
}


However if I change the fields to represent the auto-generated names:

Code: [Select]
function calc(){
  one = document.webform-client-form.submitted[food].value;
  two = document.webform-client-form.submitted[timber].value;
  three = document.webform-client-form.submitted[flood_protection].value;
  four = document.webform-client-form.submitted[soil_protection].value;
  five = document.webform-client-form.submitted[carbon_storage].value;
  six = document.webform-client-form.submitted[recreation].value;
  seven = document.webform-client-form.submitted[aesthetics].value;
  eight = document.webform-client-form.submitted[cultural_heritage].value;
  document.webform-client-form.thirdBox.value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1) + (six * 1) + (seven * 1) + (eight * 1);
}


I get all sorts of errors, mostly that client is not defined. Im guessing this is because of the -. Is it possible to escape these somehow?? Ive tried adding double and single quotes, as well as \, none works. From what I can see of the module code, changing the naming convention is out of the question...

java, forms, invalid character, escaping it?
Reply #1 on: March 29, 2010, 14:09:49 PM
When you say Java do you mean JavaScript ?

It looks like you could use getElementById to solve the problem. What type of object is webform-client-form-submitted ?


  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:java, forms, invalid character, escaping it?
Reply #2 on: March 29, 2010, 14:50:17 PM
To be brutally honest when I say java I dont know whether I mean java or javascript! I have no programming training and am simply muddling along by sheer force of will and trial and error most of the time, with lots of questions being asked on forums when I get stuck!

webform-client-form-submitted is a form field: http://www.zpyder.co.uk/tess/survey/node/2

A snippet of the form source:

Code: [Select]


 

 


Re:java, forms, invalid character, escaping it?
Reply #3 on: March 29, 2010, 19:32:42 PM
Based on this....

Quote from: zpyder
Code: [Select]


 

 




Try making the following changes:

Code: [Select]
function calc(){
  // Only changed one as an example...youd need to change them all accordingly
  one = document.getElementById(edit-submitted-food).value;

  two = document.webform-client-form.submitted[timber].value;
  three = document.webform-client-form.submitted[flood_protection].value;
  four = document.webform-client-form.submitted[soil_protection].value;
  five = document.webform-client-form.submitted[carbon_storage].value;
  six = document.webform-client-form.submitted[recreation].value;
  seven = document.webform-client-form.submitted[aesthetics].value;
  eight = document.webform-client-form.submitted[cultural_heritage].value;

  //Need to change id-of-the-box-that-you-want-to-contain-the-total as appropriate
  document.getElementById(id-of-the-box-that-you-want-to-contain-the-total).value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1) + (six * 1) + (seven * 1) + (eight * 1);
}

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:java, forms, invalid character, escaping it?
Reply #4 on: March 29, 2010, 19:36:40 PM
Cheers, will give it a go tomorrow when Im back in the office :D

java, forms, invalid character, escaping it?
Reply #5 on: March 29, 2010, 19:57:07 PM
also, why are you * 1 in the sum? Cant see a reason for it.
Formerly sexytw

  • Offline neXus

  • Posts: 8,749
  • Hero Member
Re:java, forms, invalid character, escaping it?
Reply #6 on: March 29, 2010, 20:47:25 PM
Zypyder :
Best tip if your using javascript - Use a framework - jQuery.
You will use less code and have more abilities which are easier to implement.

A quick example of me copying data from one field from another where Later I used the function when changes etc were done in another function..

Code: [Select]
function copyAddress()
{
if ($("input#BillingAddress").val() == "") {
$("input#BillingAddress").val($("input#ShippingAddress").val());
}
if ($("input#BillingCity").val() == "") {
$("input#BillingCity").val($("input#ShippingCity").val());
}
if ($("input#BillingState").val() == "") {
$("input#BillingState").val($("input#ShippingState").val());
}
if ($("input#BillingZip").val() == "") {
$("input#BillingZip").val($("input#ShippingZip").val());
}
var billIndex = $("#ShippingCountry option").index($("#ShippingCountry option:selected"))
$("#BillingCountry option:eq("+billIndex+")").attr("selected", "selected")
}


Wont mean a lot but the selector control etc you have is not only more efficient but a lot easier. If you plan to do more coding from now on and use javascript I highly recommend you take up learning jQuery and use of the plugins available.

java, forms, invalid character, escaping it?
Reply #7 on: March 29, 2010, 21:08:21 PM
Quote from: sexytw
also, why are you * 1 in the sum? Cant see a reason for it.


Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.

  • Offline neXus

  • Posts: 8,749
  • Hero Member
java, forms, invalid character, escaping it?
Reply #8 on: March 29, 2010, 21:25:56 PM
Quote from: DeltaZero
Quote from: sexytw
also, why are you * 1 in the sum? Cant see a reason for it.


Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.

Another reason to use a framework.

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:java, forms, invalid character, escaping it?
Reply #9 on: March 29, 2010, 23:41:53 PM
The thing to remember is...I have no experience in coding beyond a bit of basic html. The code pasted is basically the first bit of code I found which was easy enough for me to understand...ish. Id use jquery if I actually knew wtf it was and that itd work in the system without too much hassle ><

  • Offline zpyder

  • Posts: 6,946
  • Hero Member
Re:java, forms, invalid character, escaping it?
Reply #10 on: March 30, 2010, 10:10:50 AM
Sadly the getelementbyid didnt work. I think I am limited somewhat in what I can do without modifying whatever template file is responsible for the webform. Im looking into the validation settings to see if I can get it to sum the text boxes on submit and prompt the user if they dont add up to 100.

  • Offline Sam

  • Posts: 3,943
  • Hero Member
java, forms, invalid character, escaping it?
Reply #11 on: April 20, 2010, 14:05:06 PM
Quote from: DeltaZero
Quote from: sexytw
also, why are you * 1 in the sum? Cant see a reason for it.


Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.


Wrong terminology, you dont cast to convert ;)

java, forms, invalid character, escaping it?
Reply #12 on: April 20, 2010, 16:52:20 PM
Quote from: Sam
Quote from: DeltaZero
Quote from: sexytw
also, why are you * 1 in the sum? Cant see a reason for it.


Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.


Wrong terminology, you dont cast to convert ;)


Maybe. Using parseFloat is definitely converting rather than casting. I don’t think if doing something like appending * 1 in a loosely typed language like JavaScript is clear cut either way. I’d call it casting, others might not.

0 Members and 1 Guest are viewing this topic.