By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,661 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,661 IT Pros & Developers. It's quick & easy.

JavaScript Form Field Value Update Dilemma

P: n/a
Although I'm making an ajax call, this is really a javascript question
(although it could be even more of an HTML or DOM question... not
exactly sure)

I'm doing an ajax call to a remote php file... it returns a value fine,
and even enters a value into a text field on my form. The problem is,
right after the function call to the ajax function, I do an alert of
what the field value is, and it doesn't recognize the field value has
been changed.

Two caveats:

1 - It will recognize the last call's change
(if I run it a 2nd time, it'll see the 1st call changes)
onBlur="ajaxCall(document.form1);alert(document.fo rm1.ajaxField.value);">

2 - It'll recognize the current call's change if I set a 2 second
timeout
onBlur="ajaxCall(document.form1);setTimeout('check Fields(document.form1)',2000);">

It seems, although I can see the text field getting updated,
programmatically speaking, it isn't reflecting it until everything is
done... including any alerts and such.

It's weird seeing the empty alert popup, when I can see the value in
the text field underneath it.

Keith D Commiskey
http://kdcinfo.com

Jan 7 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
KDCinfo wrote:
1 - It will recognize the last call's change
(if I run it a 2nd time, it'll see the 1st call changes)
onBlur="ajaxCall(document.form1);alert(document.fo rm1.ajaxField.value);">

2 - It'll recognize the current call's change if I set a 2 second
timeout
onBlur="ajaxCall(document.form1);setTimeout('check Fields(document.form1)',2000);">
Well what exactly does the function ajaxCall do? If it does an
asynchronous request to the server setting up some event handler
processing the response to the request then it is quite normal that the
change has not been done after the call to the function as the function
just starts the requests and is then finished. When the response arrives
the browser calls the event handler. If any script runs before the
response arrives and has been processed then you can't expect to have
any change.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 7 '07 #2

P: n/a
Martin Honnen wrote:
Well what exactly does the function ajaxCall do? If it does an
asynchronous request to the server setting up some event handler
processing the response to the request then it is quite normal that the
change has not been done after the call to the function as the function
just starts the requests and is then finished. When the response arrives
the browser calls the event handler. If any script runs before the
response arrives and has been processed then you can't expect to have
any change.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Thanks Martin,

I believe you're correct in that it is an "asynchronous request to the
server"

My ajax call is this:
http.open("GET", url + '?param1='+escape(variable), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);

The php file then executes a mysql query, and echoes what I need. Then,
back in the main file, that echo is then written to the document form
field (which does display it).

So, if as you said, that php file is sent to execute, but the main file
carries on at the same time, that would certainly make sense. I tried
putting in an onChange event handler on the field being updated, but
apparently that doesn't apply to programmatical changes to the field.

I'm guessing a ittle delay window with a progress bar (for a generic
count of say 2 seconds) might be suitable, hoping the database doesn't
hiccup and require 3 seconds at some point...

Perhaps there's a way for the remote php file to notify the local file
that it's done?
Perhaps there's a way that field can detect it has been updated?
Perhaps a non-asynchronous request (a linear/sequential request) - if
that exists or is possible?

P.S. This is my first ajax call, so its 'clicking' more and more as I
progress :)

Thanks again!

Keith D Commiskey
http://kdcinfo.com
http://giftsforyou.biz

Jan 7 '07 #3

P: n/a
KDCinfo wrote:
Perhaps there's a way for the remote php file to notify the local file
that it's done?
Perhaps there's a way that field can detect it has been updated?
Perhaps a non-asynchronous request (a linear/sequential request) - if
that exists or is possible?
Never mind my thought about synchronous requests:

Synchronous Requests == BAD | Ajax Blog
http://ajaxblog.com/archives/2005/05...s-requests-bad

Keith D Commiskey
http://kdcinfo.com
http://giftsforyou.biz

Jan 8 '07 #4

P: n/a
KDCinfo wrote:
[...]
So, if as you said, that php file is sent to execute, but the main file
carries on at the same time, that would certainly make sense. I tried
putting in an onChange event handler on the field being updated, but
apparently that doesn't apply to programmatical changes to the field.
Onchange fires when the field loses focus if the value has changed from
when it got focus - it is likely in this case that the field will lose
focus before the change occurs. In general, onchange is one of the
less reliable events to use.
>
I'm guessing a ittle delay window with a progress bar (for a generic
count of say 2 seconds) might be suitable, hoping the database doesn't
hiccup and require 3 seconds at some point...
A progress bar might be OK, but guessing the time to display it isn't.

Usually just a 'loading...' graphic is used since it is difficult to
give meaningful feedback on the request's progress.
>
Perhaps there's a way for the remote php file to notify the local file
that it's done?
Use one of the (many) AJAX libraries that provide a callback when the
call has completed. Try AjaxToolbox:

<URL: http://www.ajaxtoolbox.com/ >

which provides things like a callback and activity monitoring (for your
progress bar).

Perhaps there's a way that field can detect it has been updated?
Perhaps a non-asynchronous request (a linear/sequential request) - if
that exists or is possible?
A synchronous call defeats the purpose of AJAX, doesn't it? ;-) But
anyhow, you can submit synchronous requests if you want - see
AjaxToolbox above.
--
Rob

Jan 8 '07 #5

P: n/a
RobG said the following on 1/7/2007 8:32 PM:
KDCinfo wrote:
[...]
>So, if as you said, that php file is sent to execute, but the main file
carries on at the same time, that would certainly make sense. I tried
putting in an onChange event handler on the field being updated, but
apparently that doesn't apply to programmatical changes to the field.

Onchange fires when the field loses focus if the value has changed from
when it got focus -
value="some value"
user types in new value
user changes it back to "some value"

The value has been "changed from when it got focus" but onchange won't
fire :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 8 '07 #6

P: n/a
RobG wrote:
KDCinfo wrote:
[...]
Perhaps there's a way for the remote php file to notify the local file
that it's done?

Use one of the (many) AJAX libraries that provide a callback when the
call has completed. Try AjaxToolbox:

<URL: http://www.ajaxtoolbox.com/ >

which provides things like a callback and activity monitoring (for your
progress bar).
--
Rob
THANKS for that reference and link Rob! That is now the next step on my
AJAX journey.
Keith D Commiskey
http://kdcinfo.com
http://giftsforyou.biz

Jan 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.