Solved

prototype and populate form fields

Posted on 2006-06-22
8
327 Views
Last Modified: 2008-01-09
I need some help with what I thought was going to be simple but turned out to be less then simple, alright for some this could be done in their sleep.

I need to update several form fields using prototype library.  I have attempted once but oddly enough when I made the request it would just load the page that I was querying.

So anyway after the person exits a certain field I need it to take the input from that field and query a page and then return the results and populate the fields.

js..
function ajaxGetFields()
      {
            var custNum = $F('custnum');
            var url = 'http://localhost/cs.php';
            var pars = 'cn=' + custNum;
            
            var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: showResponse });

      }

      function showResponse(originalRequest)
      {
            //put returned XML in the textarea
            $('result').value = originalRequest.responseText;
      }

...

<input type="text" id="custnum" name="_custnum" tabindex="1" value="" onchange="ajaxGetFields()">
leaves this field

<input type="text" id="bill_to_name" name="bill_to_name" tabindex="1" value="">
<input type="text" id="bill_to_add1" name="bill_to_add1" tabindex="1" value="">
<input type="text" id="bill_to_add2" name="bill_to_add2" tabindex="1" value="">
...
0
Comment
Question by:iceman19330
  • 5
  • 3
8 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 16960503
$('result').value

What element is being populated here?  You will need to parse the data you are receiving and put it in your form fields.  How is the data being returned(the format)?
0
 

Author Comment

by:iceman19330
ID: 16960604
right now its an echo statement from a php page, but I was thinking of doing xml return.  No elements are being updated.
0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 500 total points
ID: 16960648
$('result').value is being updated:
$('result').value = originalRequest.responseText;

On one of my pages on our intranet, I send out a bunch of database records and use @DELIM@ as a field delimiter, so my response is:
record1@DELIM@record2@DELIM@ etc...
Then I use javascript to split the string into an array:
myArray = responseText.Split('@DELIM@');
$F('mytextfield1') = myArray[0];
$F('mytextfield2') = myArray[1];

etc etc etc.

It all depends on how complicated your return results are.  It should be a simple format as possible.
0
 

Author Comment

by:iceman19330
ID: 16960689
I can do it however I want :), XML I thought was logical just not sure how to parse it on the return.  I will try the delimiter approach.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:Morcalavin
ID: 16960728
XML is great, but I found in many cases, I was returning hundreds of characters, when the only part I wanted was a small section of it.  It's also more troublesome to parse, depending on what info it contains.
0
 

Author Comment

by:iceman19330
ID: 16961081
function ajaxGetFields()
      {
            var custNum = $F('custnum');
            var url = 'http://localhost/cn.php';
            var pars = 'cn=' + custNum;
            alert("customer number: " + custNum);
            var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: showResponse });

      }

      function showResponse(originalRequest)
      {
            //put returned XML in the textarea
            alert("response1: " + originalRequest.responseText);
            $('result').value = originalRequest.responseText;
            myArray = originalRequest.responseText.Split('|');
            alert("response1: " + myArray);
            $F('bill_to_name') = myArray[0];
            $F('bill_to_add1') = myArray[1];
            $F('bill_to_add2') = myArray[2];
            $F('bill_to_city') = myArray[3];
            $F('bill_to_state') = myArray[4];
            $F('bill_to_zip') = myArray[5];
      }

I can see the response back from here alert("response1: " + originalRequest.responseText) after that point its gone, the next alert doesnt even go off.
0
 

Author Comment

by:iceman19330
ID: 16961364
Okay I got the split to work Split is case sensitive, now I guess I have to figure out why its not going into the fields.
0
 

Author Comment

by:iceman19330
ID: 16961484
Got it

needed to change the $F('bill_to_name') = myArray[0]; to $('bill_to_name').value = myArray[0];

:)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now