0

I putted a jQuery script on my chat so the message sends without page refreshing.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">

    $(document).ready(function(){
            $("#msgsending").validate({
               type: "POST",
                    debug: false,

                    submitHandler: function(form) {
                            // do other stuff for a valid form
                            $.post('index.php', $("#msgsending").serialize(), function(data) {

                            });
                    }
            });
    });
    </script>

And form

 <form id="msgsending" name="form" method="post" action="" > 
<input name="message" id="message" type="text" maxlength="350" />

<input name="message" id="message" type="text" maxlength="350" />

<input name="submit" type="submit" value="Send!" />

</form>

The code works perfectly. (index is the script that insert the message + username in the database)

But now I have a issue.

The text don't delete on the message input after submitting. I've google'd alot of pages and all the codes didn't work. (Somes worked but message isn't sending)

What do you recommand me?

2
  • 1
    You are using id=messageon two inputs. jQuery assumes only one element with that id, mening that it only accesses the first. Commented Feb 20, 2013 at 15:28
  • I'm actually only using one. But I accidently putted two while writing the question. Commented Feb 20, 2013 at 15:32

3 Answers 3

1

$('#message').val(''); should clear the input's value, and I'm guessing you'd do this after the message was sent, something like:

$(document).ready(function(){
    $("#msgsending").validate({
       type: "POST",
          debug: false,
          submitHandler: function(form) {
               // do other stuff for a valid form
               $.post('index.php', $("#msgsending").serialize(), function(data) {
                    $('#message').val('');
               });
          }
    });
});

This will only work on the first element with the ID #message, as ID's are unique and you can't have two elements with the same ID.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I'm a beginner at jQuery and I didn't though of doing that.
0

Instead of

$.post('index.php', $("#msgsending").serialize(), function(data) {

});

you could do something like:

var data = $("#msgsending").serialize();
$('#message').val('');

$.post('index.php', data, function(data) {

});

to empty the message field before sending the request OR as @adeneo suggest, you can empty the field in the success callback of the AJAX.

Comments

0

You can place any code within the callback function of .post() in order to clear out or reset the form.

You also have type: "POST" listed as an option for .validate(). You should remove this as it's not a valid option/parameter for this plugin.

Since you're doing ajax, you'll need a return false at the end of your submitHandler in order to prevent a page redirection.

Optionally, you can also replace $('#msgsending').serialize() with $(form).serialize() or form.serialize() since form is passed into the submitHandler callback function.

$(document).ready(function(){
    $("#msgsending").validate({
        type: "POST", // <-- REMOVE THIS... not a valid plugin option
        debug: false,
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('index.php', $(form).serialize(), function(data) {
                $('#message').val(''); // clears out #message
                // any other code to clear out or reset the form
            });
            return false; // prevent page redirection
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.