PDA

View Full Version : js to cold fusion


dfred
09-30-2002, 08:12 PM
here is my issue. i have a field i have types a value in with javascript.
I now need to take that value and "convert" it over to a cold fusion variable so when i submit the page, i can use that cold fusion variable.

//what my js looks like after everything has been done
option.text = option.text.substring(0,option.text.length)+ " added";
var alt_string = option.text;

//form field
<select name="NumericAlts#loopcounter#" onChange="return beginEditing(this);">
<option default=""></option>
<option value="editable">Type New Alt String Here</option>
<OPTION value=some value >some value</option>

<input type="hidden" name="alt_string>
i am using the input type hidden field to try to pass the js value, but it is not working.

help!!
dan

ShriekForth
09-30-2002, 08:37 PM
Something like this ?


<script>
function beginEditing(option1){
document.forms[0].alt_string.value = option1.options[option1.selectedIndex].text + "added";
}
</script>
<form>
<select name="NumericAlts#loopcounter#" onChange="return beginEditing(this);">
<option default=""></option>
<option value="editable">Type New Alt String Here</option>
<OPTION value=some value >some value</option>

<input type="text" name="alt_string">


ShriekForth

dfred
10-01-2002, 12:53 PM
Well that was not the answer. From some of the posting i have seen, it is not easy to pass js variables to cold fusion.
i found the js i am using to be able to type an addition to an html created select box. the value is displayed in the select box, but because the <option value="editable">, that value is what is submitted to the next page not the value the user typed in. this code in the js is what the user typed in and i need it to pass instead of "editable".

option.text = option.text.substring(0,option.text.length)+ " added";

I have been able to do something similar, but the data actually came from a popup window and was passed back to the main page and became a cold fusion variable. since this operation is done on the same page, i am not sure if I can do it.

If anybody has any other ideas or can see from the posted code how to create WDDX or a workaround JS code to CF code, please help me.

thanks

Dan

ShriekForth
10-01-2002, 03:44 PM
So you want "editable" to be "editableadded"?
If that is the case there is only one small change to make to the script I have there. Instead of referencing .text, which is what the user sees, edit value. Value is actually passed along with the form post.

function beginEditing(option1){
document.forms[0].alt_string.value = option1.options[option1.selectedIndex].value+ "added";
}

If you don't set a value on the option, the .text portion is what will be passed as the form is posted as well.

<OPTION>some value</option> would be

NumericAlts#loopcounter#=some%20value


ShriekForth

dfred
10-01-2002, 04:19 PM
well i am not sure if we are on the same page yet. I will post my total js and my form tag:

<script LANGUAGE="JavaScript" >
<!-- Begin


var o = null;
//var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var isIE = (navigator.appName == "Microsoft Internet Explorer");
function beginEditing(menu){
finish();
if(menu[menu.selectedIndex].value == "editable"){
o = new Object();
o.editOption = menu[menu.selectedIndex];
o.editOption.old = o.editOption.text;
o.editOption.text = "";
menu.blur();
//window.focus();
document.onkeypress = keyPressHandler;
document.onkeydown = keyDownHandler;
document.onmousedown = finish;
}
//these functions are being called in the previous function
function keyDownHandler(e){
var keyCode = (isIE && event.keyCode); //issues
return (keyCode!=8 || keyPressHandler(e));
}
function keyPressHandler(e){
var option = o.editOption;
var keyCode = (isIE && event.keyCode); //issues
if(keyCode==8 || keyCode==37) {
option.text = option.text.substring(0,option.text.length-2)+" ";
}else if(keyCode==13){
finish();
}else if(keyCode!=0){
option.text = option.text.substring(0,option.text.length-1) + String.fromCharCode(keyCode) + " ";
return false;
}
}
function finish(){
if(o!=null){
option = o.editOption;
if(option.text.length > 1)
option.text = option.text.substring(0,option.text.length-1);
else
option.text = option.old;
document.onkeypress = null;
document.onkeydown = null;
o = null;

option.text = option.text.substring(0,option.text.length)+ " added";
// menu[menu.selectedIndex].value == option_text
var alt_string = option.text;
// alert(option.text);


}
}
}



<select name="NumericAlts#loopcounter#"onChange="return beginEditing(this);">
<option default=""></option>
<option value="editable" id="alt">Type New Alt String Here</option>

<CFLOOP INDEX="loopcount" FROM="1" TO=#arraylen(sMyArray)#>

<OPTION value="#sMyArrayAlpha[loopcount]#~#sMyArray[loopcount]#">#sMyArray[loopcount]#</option>
</CFLOOP>
<!--- <input type="hidden" name="alt_string#loopcounter#"> --->
</select>

not sure if this well help, but since you seem to know js well, the full code may help

thanks

dan

ShriekForth
10-01-2002, 05:20 PM
Hmm.. Ok, I think I see the problem. alt_string is not being populated the way you are expecting it to?

This line...
var alt_string = option.text;

Is going to copy what you see in the input box to a local variable called alt_string. But when that fucntion is done running that string goes away. What you need to do is make the string you are looking for go to the alt_string in the form.

document.forms[0].alt_string.value = option.text;

That will place whatever is in the option at the time into the form field, that is hidden. You may also want to do that before you append " added" to it, unless you want or need that server side.

So uncomment the hidden alt_string, and set it to text, then you can see it happen when you hit enter after entering a new string.

You could also do as I had in the last message and replace the option with the value of editable to the text they typed in by referencing the value.

ShriekForth

dfred
10-02-2002, 12:46 PM
sorry i wanted to post a reply yesterday, but our network was down.
I know it is a pain, but could you please post your code into my code so i can see exactly where you are telling me to insert your potential answer. I tried to insert it into where i thought it may go, but i am not sure if i put it into the right place.

I figure if I insert it into the right place, it will pass a value to the cold fusion hidden variable and when i submit the page to my action page, the hidden variable should pass and i could output it on the next page.

Sorry for making some extra work, but this script is confusing me and it is important and i need to get it right.

thanks
dan

ShriekForth
10-02-2002, 03:18 PM
Here it is, you will need to move the script below the form, as you will need the value from loop counter to make sure the form field name is the same in the form and the script.

<select name="NumericAlts#loopcounter#"onChange="return beginEditing(this);">
<option default=""></option>
<option value="editable" id="alt">Type New Alt String Here</option>

<CFLOOP INDEX="loopcount" FROM="1" TO=#arraylen(sMyArray)#>

<OPTION value="#sMyArrayAlpha[loopcount]#~#sMyArray[loopcount]#">#sMyArray[loopcount]#</option>
</CFLOOP>
<input type="text" name="alt_string#loopcounter#">
</select>

<script LANGUAGE="JavaScript" >
<!-- Begin


var o = null;
//var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var isIE = (navigator.appName == "Microsoft Internet Explorer");
function beginEditing(menu){
finish();
if(menu[menu.selectedIndex].value == "editable"){
o = new Object();
o.editOption = menu[menu.selectedIndex];
o.editOption.old = o.editOption.text;
o.editOption.text = "";
menu.blur();
//window.focus();
document.onkeypress = keyPressHandler;
document.onkeydown = keyDownHandler;
document.onmousedown = finish;
}
//these functions are being called in the previous function
function keyDownHandler(e){
var keyCode = (isIE && event.keyCode); //issues
return (keyCode!=8 || keyPressHandler(e));
}
function keyPressHandler(e){
var option = o.editOption;
var keyCode = (isIE && event.keyCode); //issues
if(keyCode==8 || keyCode==37) {
option.text = option.text.substring(0,option.text.length-2)+" ";
}else if(keyCode==13){
finish();
}else if(keyCode!=0){
option.text = option.text.substring(0,option.text.length-1) + String.fromCharCode(keyCode) + " ";
return false;
}
}
function finish(){
if(o!=null){
option = o.editOption;
if(option.text.length > 1)
option.text = option.text.substring(0,option.text.length-1);
else
option.text = option.old;
document.onkeypress = null;
document.onkeydown = null;
o = null;

option.text = option.text.substring(0,option.text.length)+ " added";
// menu[menu.selectedIndex].value == option_text
document.forms[0].alt_string#loopcounter#.value = option.text;
// alert(option.text);


}
}
}

whammy
10-03-2002, 02:03 AM
Cold Fusion has its own methods for retrieving variables from posted forms that have nothing to do with javascript - I'd suggest you read the documentation that comes with Cold Fusion, or some CF tutorials.

P.S. I don't know Cold Fusion syntax (since I've never used it), but I work with people that have used it before, and from what I've heard/seen you shouldn't have a problem with what you are asking, at all... and you definitely shouldn't have to use javascript!

Unless what you posted was actually what you're using, in which case:

<input type="hidden" name="alt_string">

You are missing a closing quote...

ShriekForth
10-03-2002, 01:36 PM
I dont' use cold fusion either, so I am not familiar with the syntax, or scope of variables on the page. I think the problem was copying the new user entered item from the selection list into the string to be posted alter on.

ShriekForth