如何在不刷新页面的情况下自动提交这个表单?

3

这个都在一个php文件中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
    <title>JQuery Form Example</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script><!--need at least one instance of jquery-->
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script><!--ajax to use with jquery-->
    <script type="text/javascript">
    $(document).ready(function(){//when the document has loaded
        $("#myform").validate({//Q: what is validate?, myform gets validated
            debug: false,
            rules: {
                name: "required",//name and email are the only things passed
                email: {
                    required: true,
                    email: true//I guess this defines the input as an email
                }
            },
            messages: {//at what point is this message displayed?
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
            },
            submitHandler: function(form) {//submit the form
                // do other stuff for a valid form
                $.post('process.php', $("#myform").serialize(), function(data) {//Q:what is serialization? myform gets serialized and assigned an action
                    $('#results').html(data);//what is this?
                });
            }
        });
    });

    window.onload=function( )
    {
    }


    </script>
    <style>
    label.error { width: 250px; display: inline; color: red;}
    </style>
</head>
<body>

<form name="myform" id="myform" action="" method="POST">  
<!-- The Name form field -->
    <label for="name" id="name_label">Name</label>  
    <input type="text" name="name" id="name" size="30" value="Charlie"/>  
    <br>
<!-- The Email form field -->
    <label for="email" id="email_label">Email</label>  
    <input type="text" name="email" id="email" size="30" value="obesemuleaccount@yahoo.com"/> 
    <br>
<!-- The Submit button -->
    <input type="submit" name="submit" value="Submit"> 
</form>
<!-- We will output the results from process.php here -->
<div id="results"><div>
</body>
</html>

<!--ok, so I want to first try some stuff out to get a feel for how the form works-->
<!--upload the form to the site somewhere-->
<!--access and play around with it-->
<!--then figure out how to submit to multiple php forms-->

所以我正在使用JQuery和Ajax,并且需要自动提交表单。 我尝试使用document.myform.submit等方法,但似乎不起作用。当我不使用ajax时,它可以正常工作,但这会导致页面刷新到php文件的目标位置。 我认为我只是缺少一些语法。我搜索了一些类似于这个链接的线程: Ajax自动表单提交 但我更多地是一个业余设计师,所以我真的不太明白他们在说什么。我不想把修复我的代码的任务交给别人,但我相信改变很小,而且我已经熬了一整晚来解决这个问题。非常感谢任何帮助我的人!请在您的指示中非常详细,最好直接在代码中提供修复。 编辑:请不要对我的评论笑得太厉害,我现在(经过大量谷歌搜索)更好地理解了代码,那是几个小时前的修订。 双重编辑:这里有一个名为process.php的样本php脚本,不过您需要编辑它以查看autosubmit是否有效。如上所述,您可以使用MySQL数据库完成此操作。
print "Form submitted successfully: <br>Your name is <b>".$_POST['name']."</b> and your email is <b>".$_POST['email']."</b><br>";
4个回答

1
你可以使用jQuery的$('form').submit()方法来检查表单何时被提交,然后在完成所有AJAX操作后,return false;以防止默认提交。

1

我认为这个方案可以以最小的努力实现

<script>

$(document).ready(function(){
        submit_form();
});

function submit_form () {
    $.post('process.php', $("#myform").serialize(), function(data) {
        $('#results').html(data);
    });
}

</script>

问:什么是验证?myform被验证了

答:使用验证方法,确保在提交表单之前字段中的数据有效。

问:这个消息在什么时候显示?

答:当验证未通过时。例如,您在几行之前指定了名称为“必填项”,电子邮件必须是“电子邮件”。

问:什么是序列化?myform被序列化并分配一个操作

答:序列化读取表单中所有字段的值,并构造适用于GET和POST的查询字符串。在您的情况下,在序列化后,POST数据将是name=Charlie&email=obesemuleaccount@yahoo.com"

问:这是什么?

答:“data”作为参数传递给回调函数。在这种情况下,process.php的输出将被写入到#('#results')的innerHTML中。


哇,谢谢!我认为所有的答案都可以,而且我喜欢jquery表单插件。我之前就在使用它。不过这个答案最简洁,并且教会了我更多关于序列化的知识。我知道表单是有效的,只是没有自动提交,下次我会尽量更具体、更清醒地提问。谢谢! - obesechicken13

1

这个 jQuery 插件应该可以工作 http://malsup.com/jquery/form/

非常简单

$('#myform').submit(function()
{
    $('#myform').ajaxForm();
    return false;
});

你的表单将在不刷新页面的情况下提交。


1
你的脚本正常工作。我测试过了,它很好用。 要自动提交表单,你必须在window.onload函数中添加这一行代码:
$("#myform").submit();

它将在页面加载时自动提交您的表单。

为了测试您的页面并查看表单提交结果,我建议您首先将页面提交到一个简单的HTML文件中(包含一些单词...),然后再转向PHP文件。

根据您要提交页面的HTML文件的名称更改您的submitHandler: (在我的情况下,文本文件是target.html,仅包含单词Hi)

submitHandler: function(form) {//submit the form
    // do other stuff for a valid form
    $.post('target.html', $("#myform").serialize(), function(data) {//Q:what is serialization? myform gets serialized and assigned an action
        alert(data);
        $('#results').html(data);//what is this?
    });
}

在页面加载时,您将看到一个警报包含您的HTML页面内容和div结果也将填充此内容。
希望它有所帮助!
请评价。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接