Jump to content

Function - set values of one form field to another


Skemcin

Recommended Posts

Im working in a new .NET environment of whihc I have no control over. On a webpage that the system creates, I can either have my own HTML code or their ugly prebuilt crap. But, the only way to make a form work is if I use their form fields - I can not manually create and name mine, theirs are tied to the page.So, what I need to do is this:a.) create my own manual form (done)b.) place their form on the page too (done)c.) cover up their form with a div so you can see it (done)d.) when an image is clicked by my fields, I need to take the values submitted there and put them in their form and submit the form as well. (pending)here is some sample code to understand it, remember, I can only write javascripts and html in this environment, and I do not have any access to the form tag.my form (abridged)

ID: <input type="text" name="uname">Password <input type="password" name="pword"><img src="images/go.gif">

their form (abridged)

email address: <input type="text" name="useremailaddress">Password <input type="password" name="userpassword">

So, is there a particularly easy way to add function that can be called in the onlick attribute of my code to do this?Again, the assumptions are that the field names you see are static and all this is within one form but everything else is dynamic. I'm figuring that there will be some this.form uname.value code somewhere - I'm rusty on this and too tired to spend the afternoon on google.Thanks in advance.

Link to comment
Share on other sites

Give this a try

<script type="text/javascript">function submitForm(){	var oldForm = document.forms[0];	var newForm = document.forms[1];	oldForm.useremailaddress.value = newForm.uname.value;	oldform.userpassword.value = newForm.pword.value;	oldForm.submit();}</script><form action="">email address: <input type="text" name="useremailaddress">Password <input type="password" name="userpassword"></form><form>ID: <input type="text" name="uname">Password <input type="password" name="pword"><img src="images/go.gif" onclick="submitForm()"></form>

Link to comment
Share on other sites

Thanks - thats was a good start. There is only one form on the page which simplifies the code a little, this is what I have thus far:

function loginSubmit() {	eWebForm.eWebLoginControl_TextBoxLoginName.value = uname.value;	eWebForm.eWebLoginControl_TextBoxPassword.value = pword.value;	eWebForm.submit();}

but it doesn't work in IE, but does in all other browsers - damn object reference syntax. Do you kow what syntax would work - I tried document.eWebForm.submit() and that doesn work either.I also need to capture the return carriage on the password field keydown - any ideas for that too?Thanks in advance.

Link to comment
Share on other sites

This might be the correct syntax that should work in all browsers:document.forms['formname'].elements['elementname'].property ordocument.forms[3].elements[2].propertySo you'd use:document.forms['eWebForm'].submit()

Link to comment
Share on other sites

I ended up finding out that using the same syntax works in IE and FF if you use it in the onblur attribute.I am now on to the retrun carriage trapping. . . .

Link to comment
Share on other sites

You mean catching the carriage returns (\n)s and removing them?If so, this is a useful page for you:http://jennifermadden.com/javascript/stringEscape.htmlIf you tell me exactly what you'd like, I can fix it up so it works for you. :)

Link to comment
Share on other sites

Choco, thanks for the post but I'm trying to trap the act of pressing the carriage return key. This is because I do not have control over the dynamic form the application makes. Without that control I cannot control the tab index. Without that control, and with .NET having everything processed under one form, I have to tell the server which submit button I am trying to simulate pressing.For instance, there is my username and password fields, plus a search box - all are executed in one form. The submit button you press determines which part of the one form gets processed - hit theone by the search and it only worries about that one, hit the one by the u/p and that only gets paid attention to.So, I am trying to find a script like the one below that works in more than IE. If I put this in my password field then every key press will be analized by javascript and when the one that is pressed is the "return carriage", it will resimulate submitting this part of the form - otherwise every return carriage is defaulted for the search.

<script language="JavaScript"><!--function handler(e) {    if (document.all) {        e = window.event;    }        var key;    if (document.layers)        key = e.which;    if (document.all)        key = e.keyCode    var chr = String.fromCharCode(key);    alert('Character representation of pressed was ' + chr);    alert('Unicode value of key pressed was ' + chr.charcodeAt(1));}//--></script>

Link to comment
Share on other sites

Give me a sec...wrong code...Here we go!

<script language="JavaScript">function doSomething(e){var code;if (!e) var e = window.event; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; var character = String.fromCharCode(code); var unChr= character.charCodeAt(0) if(unChr=="13") {document.eWebForm.submit()}}</script><body onkeypress="doSomething(event)"><form name="eWebForm"><input type="text" value="Submit!" /><input type="button" value="submit!" /></form>

Link to comment
Share on other sites

Hi everybody,Skemcin,Any help here:

  <head>  <meta http-equiv="content-type" content="text/html; charset=windows-1250">  <meta name="generator" content="PSPad editor, www.pspad.com">  <title></title>  <script type="text/javascript">  function getInfo(form){  var fields = { user: "ID", pass: "pword" };    for(var nm in fields){        if(form.elements[fields[nm]].value == '') return false;        form.elements[nm].value = form.elements[fields[nm]].value;    }  document.getElementById('f1').submit();    }  </script>  </head>  <body>  <noscript>  <h3>This form will not work, you must enable javascript.</h3>  </noscript>    <form id="f1">      <input type="hidden" name="user">      <input type="hidden" name="pass">      <input type="text" name="ID">      <input type="text" name="pword">      <input type="button" onclick="getInfo(this.form)" value="Test This">    </form>

HTH :)

Link to comment
Share on other sites

Oh man. Just downloaded opera and it's not working for me :) does anyone by any chance know how to make it work for opera?

Link to comment
Share on other sites

I'm trying to trap the act of pressing the carriage return key.
This will correctly identify when the return key is pressed in IE, FF and Opera.Good luck :)
<head><script>function initPage(){  window.document.onkeydown=findKey;}function findKey(evt) {  var evt = (evt) ? evt : ((window.event) ? event : null);  if (evt.type == 'keydown')     {       var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;       	if(charCode==13)        {          alert ("You just pressed the return key");        }	else	{          alert ("wrong key ");	}    }}</script></head><body onload="initPage()">Press return<br /></body></html>

Link to comment
Share on other sites

thanks for the help guys. I've been able to combine many aspects of almost each post to get this working the way it needs to - almost - but frist thigns first, thanks.So, here is what seems to be my last hurdle in this one, It seems that after all my onblur statements and all the functions that I've written the proprietary system in which I am forced to code is built to only process the section I want based on the ID of the action that initiated the process. In other words, my <img> onclicked worked because I was able to give it an ID which was inturn what the action pages checks to know what parts of what form to process. That means, that I now need to clone the ID value into the action of hitting the return carriage. Is this possible?

Link to comment
Share on other sites

I am not sure what you mean.This system sounds like a piece of crap! I hope this will not taint your view of .Net. :)Is it a system available on the web or is it a private system developed by someone.

Link to comment
Share on other sites

I actually can't reveal the name of the product or the vendor - sorry, just not allowed to.But what I mean in my last post is this:a.) when you click on a submit button, it acts as an input, so, the action page will always see an additional name/value pair in the form scopeb.) action pages are often coded to sniff that added name/value pair to see what to doc.) for instance, if I load a record in an edit form and have an update and delete button named respectively, them my action page might be written with a condition like, if form.update is defined do this (update sql script), if form.delete is defined do this (delete record sql script).d.) so, on my form, when I get all my manually created neat fields transported into the system generated page (as assisted in the early oart of this post) then I can submit the form with the image because the image the following attribute:id="LoginGoButton"and that works fine if you enter your username and your password then manually click the Go button (thanks again to those who helped in this post)e.) BUT, now that I have the return key press trapped and submitting the form (thanks again et all) my new problem lies in the fact that trapping the return carriage does get me the action of submitting the form, but it provides no means of communicated the fact that:id="LoginGoButton"is doing the submitting. I need that to tell the action page to just use the fields on the page associated with that action. Without it, the action page resorts to the default button on the page, which is defined by the first submit button coded on the page - which is not this one. (and I can't move anything).This is, unfortunately, tainting my feelings about .NET. The whole each page is one big form is really irritating. But, I have to disclaim the fact that I understand why it is that way and that I do see the benefits of it - I simply just don't like it - I can live iwth it, but I just don't like it.(but that's a whole 'nother conversation):-)Did I articulate the return carriage needs to also pass and "ID" value issue well?

Link to comment
Share on other sites

ok I see what you mean.Why not when you find the ENTER key has been pressed just execute the same javascript that is executed on the onclick of the image.I am not sure how that is done but I know you can disable the form submission when someone hits ENTER and execute another function instead.

Link to comment
Share on other sites

thats actually what I am doing - running the same script that the <img> is executing.but the image is defined with id="LoginGoButton" which is the trigger on the action page - without it, it has no idea that my intent is to login.. Running the same script, just mimicks posting the form, it does not at this time assign the return carriage the same id="LoginGoButton" that would make the action aware of my intent.So, when I enter a username and password and CLICK the image to submit, everything is fine, and I am logged in - the form works.But, when I enter the same username and password and PRESS enter, the same script is submitting the same form with the same form values as before, EXCEPT no id="LoginGoButton" is assigned to anything so the action pages tells me that I must enter keywords in order to conduct a search. Remember there are one form, but two functions, login and search. The search submit button appears in the object index before my button does - hence it is assigned the return carriage association.pretty crazy isn't?

Link to comment
Share on other sites

oh ok I see what you mean now...that is really poor planning ot run the login and search on the same form. Is it like these forums (it always is index.php with different params)?I don't like that type of design...just makes things more complicated.

Link to comment
Share on other sites

Well, I took a step back and broke down the process. Here is what I found out:a.) recall I have one form that performs two function (login and search)b.) I am hiding the system generated login in fields and creating my own (with different names)What I was originally trying to do is use the same baseline field names to submit the form on my own - which worked when I was actually clicking something. That worked because I had the revaluing function tied to the onblur function. The problem ended up being the fact that the act of pressing the return carriage does not register as an onblur action (which sort of makes sense and sort of doesn't). With out the return carriage register as an onblur, the password was never getting submitted.So, I have these three function being called now:

<script language="javascript" type="text/javascript">function loginSubmit() {	document.getElementById('LoginGoButton').click();}function initPage() {	window.document.onkeydown=findKey;}function findKey(evt) {	var evt = (evt) ? evt : ((window.event) ? event : null);	if (evt.type == 'keydown') {  var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;  if(charCode==13) { 	 // alert ("You just pressed the return key"); 	 // document.getElementById('eWebForm').submit(); 	 TextBoxLoginName.value = uname.value; 	 TextBoxPassword.value = pword.value; 	 document.getElementById('LoginGoButton').click();  }	}}</script>

accompanied by:

Username:<br /><input name="uname" id="uname" class="content-login-box" width="10" onblur="TextBoxLoginName.value = uname.value" /><br />Password:<br /><input type="password" name="pword" id="pword" class="content-login-box" width="10" onblur="TextBoxPassword.value = pword.value" onkeypress="initPage()" /><input type="image" name="haxsubmit" id="haxsubmit" title="Login" src="_images/_bkstore/submit-generic.gif" border="0" onclick="loginSubmit()" />

What this now effectively does is not actually submit the form when I click the image or press the return key. I am now simply using javascript to click the system generated form button but only after I populate it with my form field values.So, its kinda of that "can't beat 'em, join 'em" mentality. If I can submit teh form myself on behalf of the page, then I just force feed it my information and and kick it in the arse to get it going.Too much work!Now, I gotta figure out why my search part is submitting to the login page and not to the search results page - yet another story.:)Thanks to all for the help and contributions, they all became part in the solution somewhere. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...