Solved

prototype and populate form fields

Posted on 2006-06-22
8
329 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding a Javascript click event to an HTML element given a class dynamically 5 36
simple javascript form validation 2 18
Google Maps API and PHP 25 27
Jquery 10 19
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. …
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

776 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