通过user1_id和newmsg提交表单发送,无需刷新页面


Submit form sending through user1_id and newmsg without page refresh

如果不使用Ajax刷新页面,我该如何提交以下表单?我需要通过"toid"发送user1_id,并从文本区域"newmsg"发送内容。

形式

<form action="insert.php" method="POST" class="form_statusinput">
<input type="hidden"  name="toid" value="<?php echo $user1_id ?>">
<span class="w">
<textarea class="input" name="newmsg" id="newmsg" placeholder="Say something" autocomplete="off"></textarea>
</span>
<button type="submit" value="Submit">Feed</button>
</form>

1)向表单添加一个ID,比如"myform"。

2) 然后你可以从这个表单中获取所有字段,并使用AJAX发送它(不要忘记包括jQuery):

        var form_data = $("#myform").serialize(); 
        $.ajax( 
        { 
            url: 'script.php', 
            type: 'POST', 
            cache: false, 
            data: form_data, 
            success: function(message) 
            { 
                ...
            }, 
            error: function(message) 
            { 
                ...
            } 
        }); 

如果jQuery是一个选项,那就很容易了。

请参阅jQuery.post():http://api.jquery.com/jQuery.post/

// Example: send form data using ajax requests
$.post("test.php", $("#testform").serialize());

根据您需要对返回值做什么,有很多选项,在这种情况下,最好只阅读文档。