要使html表单运行两个动作

时间:2014-07-01 06:37:56

标签: php html forms form-submit input-type-file

是否可以使用一个提交按钮提交两个表单?

就像用户点击表单上的提交一样,该表单会运行test.phpform.php且变量仍然完好无损?

如果没有,那么当用户点击表单上的提交时,只有test.php才能运行,然后test.php运行form.php且变量仍然完整。

2 个答案:

答案 0 :(得分:1)

我不认为这可以在正常的表单提交上进行,但您可以尝试在两个表单上按需使用AJAX请求。 (这只是一个例子,没有经过测试,只是一个指南或想法。)。

<!-- forms -->
<fieldset><legend>Form #1</legend>
    <form id="form_1" action="test.php">
        <label>Username: <input type="text" name="username" /></label>
        <label>Password: <input type="text" name="password" /></label>
    </form>
</fieldset>
<br/>
<fieldset><legend>Form #2</legend>
    <form id="form_2" action="form.php">
        <label>Firstname: <input type="text" name="fname" /></label>
        <label>Lastname: <input type="text" name="lname" /></label>
    </form>
</fieldset>

<button id="submit" type="button">Submit</button>
<!-- the forms is just an example -->
<!-- it would be weird to separate such fields in to different forms -->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    $('#submit').on('click', function(){

        $.ajax({
            url: $('#form_1').attr('action'),
            data: $('#form_1').serialize(),
            type: 'POST', // or whatever get
            dataType: 'JSON', // or whatever xml script html
            success: function(response) {

            }
        });

        $.ajax({
            url: $('#form_2').attr('action'),
            data: $('#form_2').serialize(),
            type: 'POST', // or whatever get
            dataType: 'JSON', // or whatever xml script html
            success: function(response) {

            }
        });
    });

});
</script>

答案 1 :(得分:0)

表单只能有一个操作,如果你想将数据传递到另一个页面,那么你可以通过调用ajax函数来实现。