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);
<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" />
<script type="text/javascript">
$(document).ready(function ()
			setAutoComplete("email_to", "results", "mail/findUser.php?name=");	
function dataCallback(data)
	var toid = document.getElementById('to_id').value;
	toid.value = data[0].id;

Open in new window

Who is Participating?
yami_riderConnect With a Mentor Author 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.
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".
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.