Wait for getJSON to populate form hidden fields

I have two hiddent form fields that need to be populated after a getJSON query call. Since getJSON is async this is causing issues when I submit my form and need to populate hidden fields prior with teh results from JSON.

How can I have a form that when the user submits it calls my JSON function gets the information it needs and sets the hidden values.

Keep in mind JSON is async, so I can't just set the values because by the time JSON returns the form is already submitted.

This is what I have done so far, but in dataCallBack it does not have any access to the document elements and therefore setting this heer fails.
<script type="text/javascript">
function QueryData()
{
	
 
	$.getJSON('mail/findUser.php', {mail_to: $('#email_to').val()}, dataCallback);
	
 
}
</script>
<form name="email_form" id="email_form" onsubmit="return QueryData()" action="/mail/sendMailComplete.php" method="get">
	<?php if($_REQUEST['ToID']) : ?>
		Send an Email to <?=$ToScreenName;?>:<br />
		<input type="hidden" name="to_id" value="<?php print $ToID ?>" />
		<input type="hidden" name="entity" value="<?php print $_REQUEST['ToType'] ?>" />
	<?php else : ?>
		Send an Email to: <input type="text" name="email_to" id="email_to" /><br />
		<input type="hidden" name="to_id" id="to_id" />
		<input type="hidden" name="entity" id="entity" />
	<?php endif ?>
	<input type="hidden" name="send_type" value="new" />
	Subject: <input type="text" name="subject" value="<?=$Subject;?>" /><br />
	<textarea name="body" cols="20" rows="5"></textarea>
	<input type="submit" value="send" name="submit" />
</form>
<script type="text/javascript">
 
$(document).ready(function ()
{
			setAutoComplete("email_to", "results", "mail/findUser.php?name=");	
			
});
 
 
 
function dataCallback(data)
{
	alert(data[0].id);
	var toid = document.getElementById('to_id').value;
	toid.value = data[0].id;
}
 
 
</script>

Open in new window

yami_riderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DiscomonkeyCommented:
Why make a Async call when you submit the form?  Why not call "mail/findUser.php" in the same request as "/mail/sendMailComplete.php"?  If you have a conditional problem in "mail/findUser.php", you can return the user to the current page with errors before parsing "/mail/sendMailComplete.php".
0
yami_riderAuthor Commented:
Hi Discmonkey,

Thanks for your reply. Can you elaborate on "same request" if I am understanding you correctly you are suggesting that when the form submits and sendMailComplete.php is called that within sendMailComplete.php I call findUser.php? Is that a correct assessment?

The reason async is in there originally is because I have form fields that have a autocomplete feature. The problem is this is only getting the 'email_to' and I need 'to_id' and 'entity' which indeed I can query but there is really no where I can set them because of the fact that it is an async request.

Unless there is a way that I can set them within the setAutoComplete method but when that method is called it is calling methods in a completely separate file and most likely does not have access to any of the form elements.

If possible could you break down the process flow of what you are trying to say maybe even pseudo code in rough detail.

Thanks again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.