是忽略代码的浏览器还是服务器?

时间:2010-07-17 10:52:48

标签: php jquery

我做了这个表格:

<form id="form" name="msgform" method="" action="">
<input type="text" size="40" id="msg" name="message"/>
<input type="submit" id="button" name="clicker" value="click" />
</form>

和这个jquery脚本:

$(document).ready(function(){

$("#button").click(function(){

$("#form).submit(function(){


var submision= $("#form).val();

$.post("txt/process.php", submision, function(data){
alert(data);

});

});
});
});

这是process.php文件:

<?php
echo $_POST['message'] . "";
?>

现在,当我单击按钮时,表单被提交,但它使用GET方法发送它,因为我可以在地址栏中看到它,但它永远不会被发送到php文件,我检查是否名称是正确的,如果我指定POST方法,它仍然没有转到PHP文件。 是忽略代码的服务器还是浏览器?或者我做错了什么?

感谢

3 个答案:

答案 0 :(得分:1)

请找到以下代码,它有效,请完成文档,它会告诉您错误是在做什么。


$(document).ready(function(){
    $("#button").click(function(){
        $("#form").submit(function(){
            /* var submision= $("#form).val();
                THIS DOESN'T WORK TO GET ALL OF THE ELEMENTS IN 
                                FORMAT TO PASS TO $.post EVENT, 
                We can do this as I did in following example 
                        */
            $.post("txt/process.php", { msg: $("#msg").val() }, function(data){
                alert(data);
            });
            /* Also you didn't put return false statement just at the end
                           of submit event which stops propagating this event further.  
                           so it doesn't get submitted as usually it can be without ajax, 
                           So this stops sending the form elements in url. This was because 
                           by default if you define nothing in method property for form 
                           then it consider it as GET method. 
                        */
            return false;
        });
    });
});

请告诉我,您正面临任何问题。

答案 1 :(得分:0)

您无需在按钮的单击处理程序中注册表单的提交事件。由于它是一个提交按钮,它将自动尝试提交您注册相应处理程序的表单:

$(function() {
    $('#form').submit(function() {
        // Get all the values from the inputs
        var formValues = $(this).serialize();
        $.post('txt/process.php', formValues, function(data) {
            alert(data);
        });
        // Cancel the default submit
        return false;
    });
});

答案 2 :(得分:0)

$("#form).submit(function(){

查看此选择器是否缺少“

$("#form").submit(function(){