By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

form.submit()

P: 5
Hello all,
I've had a look around but couldn't find an answer to my question.
I'm trying to submit a form using the javascript .submit() function but it doesn't seem to want to work. I'm using Ajax to get a value from a database and put it into a hidden field called destination and then want to submit the form.

Here is my code. The function is called by a button input.

Expand|Select|Wrap|Line Numbers
  1. function getDestination()
  2. {
  3.     var dest = $('destination');
  4.     var form = $('hotel_search');
  5.     var destUrl = 'getDestination.php';
  6.     var params = 'destinationName=' + $('destinationName').value;
  7.  
  8.     var ajax = new Ajax.Request
  9.     (
  10.         destUrl,
  11.         {
  12.             method: 'post',
  13.             postBody: params,
  14.             onSuccess:
  15.                 function(transport)
  16.                 {
  17.                     var response = transport.responseText || '';
  18.                     dest.value = response;
  19.                     form.submit();
  20.                 },
  21.             onFailure:
  22.                 function()
  23.                 {
  24.                     alert('Something went wrong...');
  25.                 }
  26.         }
  27.     );
  28. }
Jan 21 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Show the relevant part of your form.
Jan 22 '08 #2

P: 5
Here is a stripped down version of the form

[HTML]
<form id="hotel_search" name="hotel_search" action="searchresults.php" method="post" onsubmit="return validateSearch();">
<input type="hidden" id="destination" name="destination" value="" />
<input type="text" id="destinationName" name="destinationName" value="" />
<input type="button" onclick="getDestination();" id="submit" name="submit" value="Search">
</form>
[/HTML]
Jan 22 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Does the destination value get set as expected?

Are there any errors?
Jan 22 '08 #4

P: 5
Hi, the destination value does get set. it all works fine except the form.submit(); part of the javascript.
Jan 22 '08 #5

acoder
Expert Mod 15k+
P: 16,027
Hmm.. everything seems fine.

Is it possible for you to post a link to a page where I can see the problem?
Jan 22 '08 #6

P: 5
unfortunately no I don't have a host.
I've put in alerts to the script

Expand|Select|Wrap|Line Numbers
  1. var response = transport.responseText || '';
  2. alert("We have a response");
  3. dest.value = response;
  4. alert("destination has it's value"); // It seems to stop here
  5. form.submit();
  6. alert("Form should now have submitted");
  7.  
I'm gonna try doing this

Expand|Select|Wrap|Line Numbers
  1. var response = transport.responseText || '';
  2. alert("We have a response");
  3. dest.value = response;
  4. alert("destination has it's value");
  5. $('hotel_search').submit();
  6. alert("Form should now have submitted");
  7.  
Edit: No that didn't work either
Jan 23 '08 #7

acoder
Expert Mod 15k+
P: 16,027
If you haven't got a link to show, attach the files here.
Jan 23 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.