简体   繁体   English

使用JavaScript提交HTML格式的多个表单

[英]Submitting multiple forms in HTML with JavaScript

I'm relatively new to HTML and JavaScript and i'm currently modifying a company osTicket. 我对HTML和JavaScript比较陌生,我目前正在修改osTicket公司。

I'm trying to submit 2 forms in the same page (i'm aware that HTML does not support this) through delaying the second submission with JavaScript, but i think there's something wrong with my code or way of thinking. 我试图在同一页面中提交2个表单(我知道HTML不支持这个)通过延迟第二次提交JavaScript,但我认为我的代码或思维方式有问题。

Here is the code: 这是代码:

<script type="text/javascript">
    function x() {
        setTimeout(function() {
            <?php // Just normal validation, checking if has administrator privileges or not
            if(!defined('OSTSCPINC') || !$thisstaff || !$thisstaff->canEditTickets() || !$ticket) die('Access Denied');

            $info=Format::htmlchars(($errors && $_POST)?$_POST:$ticket->getUpdateInfo());
            if ($_POST)
                $info['duedate'] = Format::date($cfg->getDateFormat(),
                strtotime($info['duedate']));

            echo '<form id="reply" action="tickets.php?id=' . $ticket->getId() . '#reply" name="reply1" method="post" enctype="multipart/form-data">';
            csrf_token();

            echo '<input type="hidden" name="do" value="update">'; // Fields that are mandatory for the form but don't want them to show
            echo '<input type="hidden" name="a" value="edit">';
            echo '<input type="hidden" name="id" value="' . $ticket->getId() . '">';
            echo '<input type="hidden" name="user_id" id="user_id" value="' . $info['user_id'] . '">';
            echo '<input type="hidden" name="source" value="' . $info['source'] . '">';
            echo '<input type="hidden" name="topicId" value="' . $info['topicId'] . '">';
            echo '<input type="hidden" name="slaId" value="' . $info['slaId']  . '">';
            echo '</form>';?>
            document.forms['reply1'].submit(); // The second form
        }, 2000);
    }
</script>

<form id="reply" action="tickets.php?id=<?php echo $ticket->getId();?>#reply" name="reply" method="post" enctype="multipart/form-data">
    <!-- a lot of code that it is working properly -->

    <input class="btn_sm" type='submit' value="<?php echo __('Post Reply');?>" onclick="x()">
</form>

The first if in PHP is just to prevent normal users from submitting this form, i'm logged in with administration access. PHP中的第一个if只是为了阻止普通用户提交此表单,我已使用管理访问权限登录。 All of the HTML and PHP is code that was already written and should work normally (except for syntax erros) and i also tested by submitting each form individually and works as expected. 所有的HTML和PHP都是已编写的代码并且应该正常工作(语法错误除外),我还通过单独提交每个表单并按预期工作进行测试。

Also, the first form submits normally with no errors, but the second seems like it doesn't get executed. 此外,第一个表单正常提交没有错误,但第二个表单似乎没有执行。 I'm open to improvement in the code and advises, thanks ! 我愿意改进代码并提出建议,谢谢!

In jQuery 在jQuery中

$('form').submit(function(e){ // on a form submit
    e.preventDefault(); //do not submit
    serializedData = $("form").serialize(); //serialise all forms to JSON
    $.ajax({
        type: "POST",
        url: tickets.php,
        data: serializedData,
        success: function(){
            // do on success
        },
        dataType: json
    });
});

without: 无:

var request = new XMLHttpRequest();
request.open('POST', 'tickets.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
var forms = document.getElementsByTagName('form');
var data = JSON.stringify([].slice.call(forms));
request.send(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM