简体   繁体   English

PHP的形式不会用JavaScript提交

[英]php form wont submit with javascript

I have a dashboard page, and for the easiest solution I've made the entire page a form (as there are several drop downs scattered across the whole page). 我有一个仪表板页面,为了最简单的解决方案,我已将整个页面设置为表单(因为整个页面上分散了多个下拉菜单)。 I want to implement a feature that can submit the form every 30 minutes, be it with JavaScript, jQuery or anything else, but when I've tried it just refuses to execute the code, so I tried going back to something basi such as submitting the form when the drop-down is changed via "OnChange". 我想实现一个可以每30分钟提交一次表单的功能,无论是使用JavaScript,jQuery还是其他任何方式,但是当我尝试过时,它只是拒绝执行代码,因此我尝试回到诸如提交之类的基础上通过“ OnChange”更改下拉列表时的表单。

Here is an example snippet of a seperate page with some code from my dashboard page. 这是一个单独页面的示例片段,其中包含来自我的仪表板页面的一些代码。 This in itself should be working but I just can't see why it won't execute the code, maybe I'm missing something obvious? 这本身应该可以工作,但我看不出为什么它不执行代码,也许我遗漏了一些明显的东西? Can you help me fix this: 您能帮我解决这个问题吗?

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function submit_my_form(myfield)
{
   myfield.form.submit();
}
function submitForm() {
    document.getElementById("branchForm").submit();
}
</script>
</head>
<body>
<?php   
$branch_array = array(
        array(1,"BRANCH 1",1, "http://BRANCH.BRANCH1:1"),
        array(2,"BRANCH 2",1, ""),
        array(3,"BRANCH 3",3, "http://branch3:3"),
        );
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST" name="branchForm" id="branchForm">
    <select name="query" id="query" class="select" onChange="submit_my_form(this);">
    <?php
    foreach ($branch_array as $x) { // populate select box with branches available from array

        echo '<option value ="'.$x[0].'"';
        if (isset($_POST['query']) && $_POST['query'] == $x[0]) 
        {
            echo ' selected="selected" >'.$x[0].". ".$x[1].'</option>'; 
        } else { 
            echo ' option="'.$x[0].". ".$x[1].'">'.$x[0].". ".$x[1].'</option>'; 
            }
    }
    ?>
    </select>
        <input type="submit" name="submit" value="Refresh" class="btnRefresh" />
        <input type="button" value="go" name="click" onClick="submitForm();" />
    </form>
    <?php echo "<br/>Output: {$_POST['query']}"; ?>
</body>
</html>

Nothing happens when the drop-down is changed, and nothing happens when the "go" button is pressed. 更改下拉列表时什么也没有发生,而按下“ go”按钮时什么也没有发生。

Since you don't mind using jQuery, here you go: 由于您不介意使用jQuery,因此您可以执行以下操作:

var form = $("#formId");
$("select[name=selectField]").on("change", function(e){
    form.trigger("submit");
});

http://jsfiddle.net/zt8zz1j5/ http://jsfiddle.net/zt8zz1j5/

All it essentially does is listen for change on the select element, and simply triggers the submit event on the form. 它本质上所做的只是监听select元素上的更改,并简单地触发表单上的Submit事件。 Of course you'll need to change the names and IDs to fit your code, but I'm sure you can handle that :), and remember to include jQuery in the document. 当然,您需要更改名称和ID以适合您的代码,但我确定您可以处理:),并记住在文档中包括jQuery。

And for the button it's the same thing. 对于按钮,它是同一回事。 You listen for the 'click' event and then trigger 'submit' on the form. 您监听“单击”事件,然后在表单上触发“提交”。

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

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