AJAX example question to fill form fields

Discussion to talk about software related topics only.
BillC
Posts: 72
Joined: Tue Oct 13, 2009 6:22 am
Location: Buckinghamshire, UK

Re: AJAX example question to fill form fields

Post by BillC »

OK thats easy just google "javscript form input value", the first one that came up for me was http://www.javascript-coder.com/javascr ... alue.phtml

Bill
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: AJAX example question to fill form fields

Post by tod »

Just so I don't give you irrelevant or redundant advice can you confirm that you're using The Ajax Version 2 framework that I wrote and that you watched the video that was linked from there?
sblair
Posts: 162
Joined: Mon Sep 12, 2011 1:54 pm

Re: AJAX example question to fill form fields

Post by sblair »

Yes, I did use the AJAX version 2 referenced in the web video. It's all being done using the JSON objects and your code with minimal changes at this point to basically just shove in all the variables I needed pushed back through Ajax.

Thanks!
Scott
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: AJAX example question to fill form fields

Post by tod »

It sounds like you might just be stuck on the syntax for a form element. So assume your Brightness field has an ID tag of "myBrightness" you just get the node via the id and set the value property. Since you're using Prototype you can use this

Code: Select all

$("myBrightness").value = yourJsonObject.brightness;
There are also other ways to get to form elements via Javascript, something like "document.form[fieldname].value =" for one, but I tend to put unique ID tags on everythng to make my life easy.
[EDIT]
I am assuming your Ajax request is specifying a handler to process the JSON object once it comes back. Using the video example at around 4:30 you'll see that I'm in my handler shell.CreateSampleData, inside the if where I check that the JSON object isn't null, you'll see me using syn.AddTextToCleanNodeViaId(). You should have something similar; you just want to use the above code for setting the field value instead of adding text to a node.

A couple of general pieces of advice. jsfiddle can be very useful for messing around with snippets of javascript code. Stackoverflow can get you answers to JavaScript questions within minutes. Make sure you search first, most programming questions in the known universe have already been asked and answered. If I don't find an answer from a search, I start asking the question, BUT, before I submit I look at all the suggestions for answers that StackOverflow provides. It's perfectly OK to ask your question here too, but you should expect slower response times.
Post Reply