Solved

Wait for getJSON to populate form hidden fields

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now