3

This is really frustrating I would appreciate some help with this. I have a div, called comments and a form inside of that div. What I want to do is post a form to the current page and have it load inside of the div without reloading the entire thing. Here is my current code:

<div id="comments">
<form action="#" method="post" onsubmit="return false;" >
<input type="hidden" name="txtname" value="test">
<textarea id="wysiwyg" name="wysiwyg" rows="5" cols="50"></textarea>
<input type="submit" name="post" id="post" value="Submit">
</form>
<script type="text/javascript">
EDIT: Read edit below for current code
</script>        
</div>

When I submit, the alert fires, but the page does not load. It works fine if I make the event as follows:

$("#comments").load("comments.asp");

It's not liking the posting of data. I have used .load before but never to post data. I got the above code from these very same forums.

I'm honestly not sure of the purpose of 'name' and 'tel' - do I refer to those variables or the form variable names when processing the code? This is in ASP classic.

What's wrong with the above code, how can I get it to send data from the forum via POST? Thanks!

EDIT: I am now using the following code:

$("#post").submit(function(event){
    var $form = $(this),
        $inputs = $form.find("input, select, button, textarea"),
        serializedData = $form.serialize();
    $inputs.attr("disabled", "disabled");

    $.ajax({
        url: "/comments.asp",
        type: "post",
        data: serializedData,
        success: function(response, textStatus, jqXHR){
            console.log("comment posted");
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log(

                textStatus, errorThrown
            );
        },
        complete: function(){
            // enable the inputs
            $inputs.removeAttr("disabled");
        }
    });

    event.preventDefault();
});

And now it's using properly getting the form handled...however it goes to comments.asp. How can I make all the action happen in a certain div (comments div?)

Katherine Perotta
  • 125
  • 1
  • 5
  • 15

3 Answers3

4

It seems to me you are blending a bunch of different techniques in a way that is not entirely coherent.

$.post is a shortened version of $.ajax (see here).

$.load takes a url and sticks it into a <div> or other DOM Element (see here).

If I understand it correctly (and I may not!), you're not really wanting to load the form, but put values into the form fields. $.load is an odd way to do this. (It may work, but I do it another way.)

If you're using $(#...).submit, you can also leave out a whole bunch of stuff in your form. The following should work fine.

<form id="form_id">
...
<input type="submit" value="Submit">
</form>

My method is: (1) have a hardcoded HTML form (or build it by AJAX), (2) get the values from the DB (or wherever) using $.post (or $.ajax), (3) stick the values into the form using .val() (or equivalent - whatever is right for the input type) and the DOM id of that input, and then (4) use .submit (in a manner similar to yours). You will need to add preventDefault as the others have suggested.

You're also muddying the waters using #post as the DOM id. You really want to give the form itself the ID, and then use $(#form_id).submit(... I can't test it now, but having the submit on the input field may cause some grief. The official example attaches the .submit to the form id.

I'm also not sure the <div> with id 'comments' really does much. I have a container id like your 'comments', but that's because I build forms by AJAX and stick them into the container. If you don't need to do that, the id 'comments' is unnecessary to the whole procedure.

Nick
  • 5,995
  • 12
  • 54
  • 78
  • Thanks for the response, yeah im quite confused at this point. Here's what im trying to do: I want to display a comment box, which allows a user to add a comment. When they click 'submit', I want the comment to submit, the page to reload (with a confirmation message, so I need to be able to access that data), and then the same form box show up again, so they can make another comment. All of this needs to happen in the same DIV. Basically I want to handle a form all inside of a single div. – Katherine Perotta Apr 15 '12 at 03:37
  • You can achieve all this without a page reload using AJAX. Do you want to display each submitted comment? So to start with, there's only the comment box. Then after the first comment is added, the comment is displayed, a confirmation message is given, and the box is under the comment. Then when another comment is added, both comments are displayed, a confirmation for the second comment is given, and the box is still there etc... (All within the comments div.) Is this the sort of effect you want? – Nick Apr 15 '12 at 03:41
  • Nick, displaying the comments are from a database, which I already have another method for displaying. Basically, the only thing I need to do is be able to post the comment without reloading the entire page. The SQL part of the comment posting is handled on the same page (comments.asp). At this point, im pretty sure im making this far more complicated than it needs to be. – Katherine Perotta Apr 15 '12 at 03:49
  • By "post the comment", do you mean "stick the comment up on the page" or "stick the comment into the database"? I presume the former. I'll change computers, and create a jsFiddle for you. – Nick Apr 15 '12 at 03:52
  • I mean stick to database - it will automatically be displayed on the page refresh. I think im close to figuring out a workaround, im utilizing the .load method and just sending the comment via GET. While this is not ideal, the comments are quite small and this is a restricted website. – Katherine Perotta Apr 15 '12 at 04:15
  • OK. Misunderstanding. Anyway, this achieves the display without the page refresh: http://jsfiddle.net/jB3vv/ UPDATE: I've put my method of $.post in the updated fiddle: http://jsfiddle.net/jB3vv/1/ – Nick Apr 15 '12 at 04:29
1

Your text box element dont have an id with value txtname. But in your script you are trying to access using # (which is supposed be with an id context). So add an id element to your input box.

<input type="hidden" name="txtname" id="txtname"  value="test">

And as expascarello said, You need to stop the default behaviour of the submit button . Other wise it will do the normal form posting so you wont be able to feel the ajax effect. Use preventDefault

$(function(){
   $("#post").click(function(e) {
     e.preventDefault()
     alert("clicked");
        $("#comments").load("comments.asp", { 
         'name': $("#wysiwyg").val(), 
         'tel': $("#txtname").val()
        });
  });
});
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

You are not cancelling the clicking of the button so the form is submitting and resetting the page.

$("#post").click(function(evt) {
    evt.preventDefault();
    ...

jQuery event.preventDefault()

The load() method does a get and not a post.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • ok im reading up on .post as well - does it still have the same effect as .load - will it still load that page (i have a confirmation message on that page to be loaded) – Katherine Perotta Apr 15 '12 at 03:01