Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Ajax Form Script Only Runs Once
Old 03-03-2011, 04:53 PM Ajax Form Script Only Runs Once
Average Talker

Posts: 19
Trades: 0
I'm using Ajax for my comments form, but I can only get the script to run once without reloading the page.

Here's the form:

Code:
<form method="post" action="javascript:postComment('<?php echo $usrFieldName; ?>','<?php echo $bdyFieldName; ?>')">
  <fieldset>
    <input type="text" name="<?php echo $usrFieldName; ?>" id="<?php echo $usrFieldName; ?>" />
    <textarea name="<?php echo $bdyFieldName; ?>" id="<?php echo $bdyFieldName; ?>" rows="5" cols="15"></textarea>
    <input type="submit" name="submit" id="submit" value="Post Your Comment" />
  </fieldset>
  <fieldset id="status">
  </fieldset>
</form>
In order to prevent spam I randomly generate the field names using a technique I found, so I pass the names to my Ajax script.

The script looks like this:

Code:
function postComment(usrFieldName,bdyFieldName) {

  // get field values
  var cUsername = escape(document.getElementById(usrFieldName).value);
  var cBody = escape(document.getElementById(bdyFieldName).value);

  // disable submit button and display status message
  $('#submit').attr('disabled',true);
  $('#status').html('Please Wait');

  if (window.XMLHttpRequest) {
    comment=new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    comment=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (comment==null) {
    $('#status').html('<strong>Error:</strong> Your browser does not support this script');
    return;
  }
  comment.open('POST', 'comment-handler.php', true);
  comment.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  comment.onreadystatechange = function() {
    if (comment.readyState == 4 && comment.status==200) {
      
      // if there are no errors on the comment handler page . . .
      if (comment.responseText == "comment posted") 
        $('#status').html('Comment Posted').delay(1000).fadeOut(400);

      // otherwise, echo the error list
      else {
        $('#status').html(comment.responseText);
        $('#submit').attr('disabled',false);
      }
    }
  }
  comment.send(usrFieldName+"="+cUsername+"&"+bdyFieldName+"="+cBody);
}
When I submit the form once it works perfectly, but any subsequent attempts without reloading the page produce no result at all.

If there aren't any errors I just hide the form anyway (to prevent spam), but if there's an error (required field left empty, for instance), I want the user to be able to fix his/her mistake and resubmit without having to reload the page.
Kent O'Matic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-03-2011, 05:00 PM Re: Ajax Form Script Only Runs Once
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
The xmlhttpobject is NOT released on close, you have to make it reusable

http://www.devx.com/tips/Tip/31066
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-08-2011, 06:38 AM Re: Ajax Form Script Only Runs Once
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
On seeing your code, particularly this part,

HTML Code:
 // disable submit button and display status message
  $('#submit').attr('disabled',true);
  $('#status').html('Please Wait');
It is clear that your are using jQuery. Then you are you going for a raw method of ajax code instead of using $.ajax or other useful methods available within jquery.

Use $.ajax and i suppose your problem will be sloved.
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Ajax Form Script Only Runs Once
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34780 seconds with 12 queries