Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wait for getJSON to populate form hidden fields

Posted on 2009-07-09
2
Medium Priority
?
1,783 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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…

609 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