0

I'm trying to submit form data without refreshing the page using jQuery. I've added all the following code in correct places but the page refreshes and nothing happened, neither it echo's the success data nor it inserts any data into sql db. Here is what I'm trying to do;

HTML:

        $profile_comments = '
        <form action="#" method="post">
            <table border="0" cellspacing="'.$theme['borderwidth'].'" cellpadding="'.$theme['tablespace'].'" class="sideboxes_tborder">
                <tr>
                    <td class="sideboxes_thead">Post Comments</td>
                </tr>
                <tr>
                    <td class="sideboxes_trow">
                        <input type="text" class="textbox_comment" name="message" id="pc_message" tabindex="1" autocomplete="off" />
                    </td>
                </tr>
                <tr>
                    <td class="sideboxes_trow" align="left">
                        <input type="hidden" name="uid" value="'.$memprofile['uid'].'" />
                        <input type="hidden" name="from_uid" value="'.$mybb->user['uid'].'" />
                        <input type="submit" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">
                    </td>
                </tr>
                <tr>
                    <td class="sideboxes_trow"><div id="show_profile_comments" style="overflow: auto; max-height: 313px;"></div></td>
                </tr>
            </table>
        </form>';

jQuery:

jQuery.noConflict();

jQuery(document).ready(function($)
{
    $("#comment_submit").click(function()
    {

            var message = $( "#pc_message" ).val(),

            if (message == '')
            {
                alert( "Message is missing!!" );
                return;
            }

            $.ajax(
            {
                type : "post",
                dataType: "html",
                url : "pro_profile.php?action=do_comment",
                data : "message=" + message,
                success : function(response)
                {
                    $('#show_profile_comments').html(response);
                }
                document.getElementById('pc_message').value = '';
                document.getElementById('pc_message').focus();

                if (response.error)
                {
                    alert(response.error);
                }
            });
    });
});

PHP:

if ($_POST['action'] == "do_comment")
{
    $uid = intval($mybb->input['uid']);
    $insert_array = array(
        "uid" => $uid,
        "from_uid" => intval($mybb->input['from_uid']),
        "approved" => '1',
        "message" => $db->escape_string($mybb->input['message']),
        "dateline" => TIME_NOW
    );
    $db->insert_query("pp_comments", $insert_array);

    $query = $db->simple_select("pp_comments", "*", "uid='{$uid}'");
    $c = $db->fetch_array($query);

    echo $c['message'];
}

Please help!

4
  • in your jQuery part, did you get value into message variable and if its empty did it show alert message? Commented Oct 26, 2013 at 12:20
  • No it doesn't alerting if the message input box is empty either :s Commented Oct 26, 2013 at 12:23
  • Give your form an id="msgSubmit" and in your jquery replace var message = $( "#pc_message" ).val(), if (message == '') { alert( "Message is missing!!" ); return; } with var datastring = $("#msgSubmit").serialize(); and then alert this datastring. and let me know you are getting msg or not? because most of this happens with me if i use $ multiple time nothing happens. Commented Oct 26, 2013 at 12:29
  • No it doesn't alert either.. :( Commented Oct 26, 2013 at 12:50

5 Answers 5

1

if this should be on success

document.getElementById('pc_message').value = '';

document.getElementById('pc_message').focus();

change your JS code to this

jQuery.noConflict();

jQuery(document).ready(function($)
{
    $("#comment_submit").on('click', function()
    {

            var message = $( "#pc_message" ).val(),

            if (message == '')
            {
                alert( "Message is missing!!" );
                return;
            }

            $.ajax(
            {
                type : "post",
                dataType: "html",
                url : "pro_profile.php?action=do_comment",
                data : "message=" + message,
                success : function(response)
                {
                    $('#show_profile_comments').html(response);
                    document.getElementById('pc_message').value = '';
                document.getElementById('pc_message').focus();
                },
            error : function(response)
                {
                    alert(response.responseText);
                }
            });
            
            return false;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Just because you are using click event on submit buttton

replace

<input type="submit" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">

with

<input type="button" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">

4 Comments

Its not a issue Deepak, I've tried your suggestion but it doesn't works.
No it doesn't alerting if the message input box is empty either :s
ok remove form from you code <form> remove opening form and closing form you don't need it. then check its alerting or not
I've removed opening and closing form tags and it doesn't alerting either.
0

use .on

$("#buttonId").on('click',function(e){
    e.preventDefault();
  });

e.preventDefault()

Description: If this method is called, the default action of the event will not be triggered.

it's ok either button or submit

<input type="submit" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">

Comments

0

change your button from submit type to button type

<input type="button" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">

or prevent Default form submit

$("#yourFormName").submit(function(e){
    e.preventDefault();
  });

Comments

0

Just changed the type for the button

<input type="button" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">

or just make it as a button:

<button type="button" class="button" id="comment_submit" name="submit">Post Comment</button>

or in the <form> if it has an id:

jQuery(function(){
    jQuery("#yourform").submit(function(e) {       
      e.preventDefault();
    });
});

or in the form tag

<form type="post" onclick="return false">

7 Comments

What doesnt work? that it is the way to stop a page from submit a form
I've tried both ways, by using input type="button" as well as <button ..> neither of them works. I've also tried with the edited jquery you suggested but unfortunately it doesn't work either.
can you post the rest of the html? because that it is a really odd behavior :S
I can't since this comment box here doesn't allow me to post more then 550 characters. Anyway the HTML is same as I posted in question above.
try onclick="return false" in the form tag
|

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.