We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Wait for getJSON to populate form hidden fields

yami_rider
yami_rider asked
on
Medium Priority
1,841 Views
Last Modified: 2013-11-08
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

Comment
Watch Question

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".
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.