【问题标题】:To make html form run two actions让 html 表单运行两个动作
【发布时间】:2014-07-01 15:25:30
【问题描述】:

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

如果用户在表单上单击提交,该表单运行 test.phpform.php 时变量仍然完好无损?

如果不是,那么当用户在表单上单击提交时,它只运行test.php 然后test.php 运行form.php 时变量仍然完好无损。

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      表单只能有一个动作,如果你想将数据传递到不同的页面,那么你可以通过调用 ajax 函数来做到这一点..

      【讨论】:

        猜你喜欢
        • 2013-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        相关资源
        最近更新 更多