简体   繁体   English

表单提交按钮工作,但不在链接中提交()

[英]Form Submit Button Works, but not Submit() in Link

I've done this so often before on different websites, but can't get it to work now. 我之前在不同的网站上经常这样做,但现在无法让它工作。 I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. 我有一个简单的表单,使用提交按钮完美地发布,但由于特定原因,我实际上需要通过URL链接提交。 I'm using submit() . 我正在使用submit() The form submits, but the data isn't posting. 表单提交,但数据未发布。
What am I missing? 我错过了什么?

<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) { 
    echo $_POST['information'];
    echo '</br>Info successfully posted.'; 
    }
?>

    <form action="test.php" method="post" id="fooform"> 
        Hello World.<br>
        Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
        <input type="submit" name="bar" value="Send"><br>
        <a href="test.php" onclick="SubmitForm('fooform');">Confirm and Post</a><br>
        <a href="test.php" onclick="this.form.submit();">Post Directly </a>
    </form>

    <script type="text/javascript">
        function SubmitForm(formId) {
            var oForm = document.getElementById(formId);
            alert("Submitting");
            if (oForm) { oForm.submit(); }
            else { alert("DEBUG - could not find element " + formId); }
            }
    </script>
</body>
</html>

The form starts to submit, then the href of the link is followed, and this cancels the form submission. 表单开始提交,然后跟踪链接的href ,这将取消表单提交。

If you are using old-style onclick attributes, then return false; 如果您使用的是旧式onclick属性,则return false; at the end to prevent the default action. 最后阻止默认操作。

You would, however, be better off using a submit button (you are submitting a form). 你会,但是,使用一个提交按钮( 提交表单)会更好。 You can use CSS to change its appearance. 您可以使用CSS来更改其外观。

Try this code : 试试这段代码:

<html>
    <body>
        <?php
        if (isset($_POST['bar'])) {
            echo 'testing button<br>';
        }
        if (isset($_POST['information'])) {
            echo $_POST['information'];
            echo '</br>Info successfully posted.';
        }
        ?>

        <form action="test.php" method="post" id="fooform"> 
            Hello World.<br>
            Select checkbox: <input type="checkbox" id="information" name="information" value="yes">

            <input type="submit" name="bar" value="Send"><br>
            <a href="#" onclick="SubmitForm('fooform');">Confirm and Post</a><br>
            <a href="#" onclick="document.getElementById('fooform').submit();">Post Directly </a>
        </form>

        <script type="text/javascript">
                function SubmitForm(formId) {
                    var oForm = document.getElementById(formId);
                    alert("Submitting");
                    if (oForm) {
                        oForm.submit();
                    }
                    else {
                        alert("DEBUG - could not find element " + formId);
                    }
                }
        </script>
    </body>
</html>

try to submit form with form id in jquery 尝试在jquery中提交带有表单id的表单

<a class="submit">Post Directly </a>

$('a.submit').click(function(){

   $('#fooform').submit();
}) 

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

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