Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

prototype and populate form fields

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
iceman19330
Asked:
iceman19330
  • 5
  • 3
1 Solution
 
MorcalavinCommented:
$('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
 
iceman19330Author Commented:
right now its an echo statement from a php page, but I was thinking of doing xml return.  No elements are being updated.
0
 
MorcalavinCommented:
$('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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
iceman19330Author Commented:
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
 
MorcalavinCommented:
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
 
iceman19330Author Commented:
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
 
iceman19330Author Commented:
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
 
iceman19330Author Commented:
Got it

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

:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now