Solved

Wait for getJSON to populate form hidden fields

Posted on 2009-07-09
2
1,753 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

0
Comment
Question by:yami_rider
2 Comments
 
LVL 1

Expert Comment

by:Discomonkey
ID: 24816769
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
 

Accepted Solution

by:
yami_rider earned 0 total points
ID: 24818089
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now