submit the form using ajax

后端 未结 8 865
栀梦
栀梦 2020-12-05 10:17

I\'m developing an application (a kind of social network for my university). I need to add a comment (insert a row in a specific database). To do this, I have a HTML form in

相关标签:
8条回答
  • 2020-12-05 10:57

    It's much easier to just use jQuery, since this is just a task for university and you do not need to save code.

    So, your code will look like:

    function sendMyComment() {
        $('#addComment').append('<input type="hidden" name="video_id" id="video_id" value="' + $('#video_id').text() + '"/><input type="hidden" name="video_time" id="video_time" value="' + $('#time').text() +'"/>');
        $.ajax({
            type: 'POST',
            url: $('#addComment').attr('action'),
            data: $('form').serialize(), 
            success: function(response) { ... },
        });
    
    }
    
    0 讨论(0)
  • 2020-12-05 10:59

    You can add an onclick function to your submit button, but you won't be able to submit your function by pressing enter. For my part, I use this:

    <form action="" method="post" onsubmit="your_ajax_function(); return false;">
        Your Name <br/>
        <input type="text" name="name" id="name" />
        <br/>
        <input type="submit" id="submit" value="Submit" />
    </form>
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题